0

I use a batch file to create another batch file with some part extracted from a file, it create a long file.

I need to add only one an exit command in the end of file.

Actually the batch files created do not close after finish the work.

Code File to create another batch file:

 :: 'Create Batch File'
 @echo off
     (for /f "usebackq delims=" %%a in ("D:\Programmi Installati\list.csv") > do (
        echo PING -n %%~NXa localhost ^>NUL 2^>^&1
        echo/
        echo/
     )) > "D:\Programmi Installati\new.bat"

Actual result of new.bat (wrong):

PING -n 10 localhost >NUL 2>&1
exit

PING -n 15 localhost >NUL 2>&1
exit

PING -n 20 localhost >NUL 2>&1
exit

PING -n 25 localhost >NUL 2>&1
exit

Aspected result of new.bat (desired):

PING -n 10 localhost >NUL 2>&1


PING -n 15 localhost >NUL 2>&1


PING -n 20 localhost >NUL 2>&1


PING -n 25 localhost >NUL 2>&1
exit
double-beep
  • 5,031
  • 17
  • 33
  • 41
placidomaio
  • 111
  • 1
  • 13
  • It should be `do` not `> do`. – Compo Jan 01 '19 at 20:03
  • Hi the bat file working well, the unique bug is how to add at the end of file generated exit . The batch file create another batch file with estraction and loop and working well, but i do not find a solution to add at the end (only at the end 1 times only) exit. – placidomaio Jan 01 '19 at 20:16
  • 3
    Where do you `echo` exit? Please provide just a piece of `list.csv` for us to be able to reproduce your problem. Edit your question to fix typo mentioned by Compo. Also, is it working if you put after the `for` loop `(echo exit)>>"D:\Programmi Installati\new.bat"`? Please provide us more information, but please do not comment it. Instead, edit your question. – double-beep Jan 01 '19 at 20:30
  • @double-beep Your solution working well it's exactly the solution at my problem add 1 exit after the loop at the end of the batch file create (echo exit)>>"D:\Programmi Installati\new.bat" Thanks you solved my problem. – placidomaio Jan 01 '19 at 21:41
  • the `exit` is not the only bug in your code. What Compo said is also one. And don't use `ping` for delaying, it'll sleep for one less second than expected [How to sleep for 5 seconds in Windows's Command Prompt? (or DOS)](https://stackoverflow.com/q/1672338/995714), [How do I make a batch file wait / sleep for some seconds?](https://superuser.com/q/48231/241386) – phuclv Jan 02 '19 at 01:35
  • @phuclv Thanks for suggestion i'll optimize the code of the script. can you add more info about this because i do not know this error "It should be do not > do", thanks – placidomaio Jan 02 '19 at 03:15

1 Answers1

0

Actually, except the typo in your code you have missed to echo exit in the end of your code block. Your code should look like the following after the fixes:

@rem 'Create Batch File'
@echo off

(for /f "usebackq delims=" %%a in ("D:\Programmi Installati\list.csv") do (
    echo PING -n %%~NXa localhost ^>NUL 2^>^&1
    echo/
    echo/
))>"D:\Programmi Installati\new.bat"
echo exit >> "D:\Programmi Installati\new.bat"
double-beep
  • 5,031
  • 17
  • 33
  • 41