1

I'm creating a bat file that will write me a script that will be called by another bat.

What I wrote:

echo @echo off>>myscript-NTRE_TN_F
echo open 127.0.0.1>>myscript-NTRE_TN_F
echo user USER PASS>>myscript-NTRE_TN_F
echo prompt n>>myscript-NTRE_TN_F
echo ascii>>myscript-NTRE_TN_F
echo lcd C:\>>myscript-NTRE_TN_F

SET MAX_FILES=200
SET /S FILE_COUNT=0

for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
    IF !FILE_COUNT! LSS %MAX_FILES% (
        echo mget %%a>>myscript-NTRE_TN_F
        echo echo %%a>>C:\Downloaded_Files.txt>>myscript-NTRE_TN_F
    )
    SET /A FILE_COUNT+=1
)

echo bye>>myscript-NTRE_TN_F

My issue is in the echo echo %%a part.
My output now is:

@echo off
open 127.0.0.1
user USER PASS
prompt n
ascii
lcd C:\
mget FILE1
echo FILE1
mget FILE2
echo FILE2
bye

I would like an OUTPUT like this:

@echo off
open 127.0.0.1
user USER PASS
prompt n
ascii
lcd C:\
mget FILE1
echo FILE1>>C:\Downloaded_Files.txt"
mget FILE2
echo FILE2>>C:\Downloaded_Files.txt
bye

I found something similar in the topic here: Why gives my script an other output with the echo command?

I don't know how to manage the variable.
Thanks in advance for your help.

Community
  • 1
  • 1
  • 3
    Why are you writing `@echo off`, which is a Command Prompt command, into an FTP script? – Klitos Kyriacou Dec 15 '16 at 09:53
  • Yes you are totally right... this is what I'm discovering now trying the script... I have to understand how to do it because I need to write in a file tracing which file I've already downloaded. By the way thanks for your comment! I think I figured out. – MarcaSolkanar Dec 15 '16 at 12:31
  • just for information I changed the echo and put outside the "myscript": echo %%a>>C:\Downloaded_Files.txt SET /A FILE_COUNT+=1 – MarcaSolkanar Dec 15 '16 at 12:40

2 Answers2

2

(example)

    echo echo %%a^>^>C:\Downloaded_Files.txt>>myscript-NTRE_TN_F

The caret ^ signifies that the following character be reproduced literally.

Magoo
  • 77,302
  • 8
  • 62
  • 84
1

I suggest using this batch code:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "ScriptFile=myscript-NTRE_TN_F"
>"%ScriptFile%" echo @echo off
>>"%ScriptFile%" echo open 127.0.0.1
>>"%ScriptFile%" echo user USER PASS
>>"%ScriptFile%" echo prompt n
>>"%ScriptFile%" echo ascii
>>"%ScriptFile%" echo lcd C:\

SET "MAX_FILES=200"
SET "FILE_COUNT=0"

for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
    IF !FILE_COUNT! GEQ %MAX_FILES% goto Finish
    >>"%ScriptFile%" echo mget %%a
    set "ScriptLine=>>C:\Downloaded_Files.txt echo %%a"
    >>"%ScriptFile%" echo !ScriptLine!
    SET /A FILE_COUNT+=1
)

:Finish
>>"%ScriptFile%" echo bye
endlocal

The redirection operator can be also put at beginning of a line which makes it easier to read what is written into the file.

Inside the FOR loop the IF condition is changed to exit the loop on reaching value of MAX_FILES.

And the line to output is first assigned to a variable enclosed in double quotes to get >> interpreted as string and the value of this variable is output using delayed expansion with redirection to file.

Please read also answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for an explanation why using double quotes around variable=value.

See also the Microsoft article Using command redirection operators

Another even better solution is:

@echo on
setlocal EnableExtensions EnableDelayedExpansion
set "ScriptFile=myscript-NTRE_TN_F"
(
    echo @echo off
    echo open 127.0.0.1
    echo user USER PASS
    echo prompt n
    echo ascii
    echo lcd C:\
)>"%ScriptFile%"

SET "MAX_FILES=200"
SET "FILE_COUNT=0"

for /F "usebackq tokens=1,2 delims=," %%a IN ("C:\filelist-NTRE_TN_F.txt") DO (
    IF !FILE_COUNT! GEQ %MAX_FILES% goto Finish
    echo mget %%a
    set "ScriptLine=>>C:\Downloaded_Files.txt echo %%a"
    echo !ScriptLine!
    SET /A FILE_COUNT+=1
)>>"%ScriptFile%"

:Finish
echo bye>>"%ScriptFile%"
endlocal

All lines output to STDOUT within the two command blocks are redirected into the script file to create.

By the way: SET /S FILE_COUNT=0 resulted on execution in error message

The syntax of the command is incorrect.

The command SET does not support an option /S, just /A for an arithmetic expression and /P for prompting user for value if command extensions are enabled in current command process environment.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thank you for your suggestion and of expecially for the way you teach to me (newbie) how and why code in this way. I'm using the suggestion so I accept your answer! I just modified two lines to exclude the echo in the FTP script because I didn't need it inside. Thank you again!!! Cheers – MarcaSolkanar Dec 15 '16 at 12:36