Solved Batch FTP script: list, rename, get & delete?

June 14, 2011 at 14:57:06
Specs: Windows 2003
I'm trying to create a Windows batch script to do the following:

Login to a remote FTP server
Change remote directories
Get the list of files in that directory
Rename any text file with a 78_ prefix in the filename and a .txt extension to a .tmp extension
Download any and all .tmp files
Delete any and all .tmp files
Then locally, rename those .tmp files back to .txt.

Here's what I have so pieced together so far. I don't believe I'm that far off but my knowledge of batch scripting is clearly not the best. Any help would be appreciated! Thanks!


@echo off

setlocal enableextensions
setlocal enabledelayedexpansion

REM Build the "myscript" batch script file

REM add command to connect to the remote FTP server
echo open>>myscript

REM add username and password
echo user me pass1234>>myscript

REM set local directory to c:\temp
echo lcd c:\temp>>myscript

REM set remote directory to \outgoing
echo cd outgoing>>myscript

REM disable user prompts
echo prompt n>>myscript

REM set mode to ascii
echo ascii>>myscript

REM output a list of all files in the directory
echo ls 78_* list.txt>>myscript

REM rename the extension of each file in the list from .txt to .tmp
for /f "usebackq tokens=*" %%i in ('c:\temp\list.txt') do echo rename %%i %%i*.tmp>>myscript

REM download any .tmp files
echo mget 78_*.tmp>>myscript

REM delete any .tmp files
echo mdelete 78_*.tmp>>myscript

REM quit FTP
echo bye>>myscript

REM run DOS FTP command using newly created "myscript" script file
ftp.exe -n -s:myscript

REM cleanup and delete the myscript file and the list file
del myscript
del list.txt

REM locally rename the .tmp extension back to .txt
ren *.tmp *.txt

See More: Batch FTP script: list, rename, get & delete?

June 14, 2011 at 18:14:52
✔ Best Answer
You'll probably have to build and execute two ftp scripts:

the first one to populate list.txt, and
the second one to rename/mget/delete the files.

As it is now, the FOR loop executes before the ftp script, so list.txt will be empty, and really not do you much good at all!

You may want to perform a CD /D "c:\temp" prior to executing the ftp script or batch file, so you can be assured that when you perform the "ls 78_* list.txt" ftp command that the output get saved in the proper location. Depending on how and where the batch gets called, "list.txt" may end up in %WinDir%\System32\ or in %UserProfile%\. (I'll let you issue the SET command with no arguments in a command window to see those expanded System Variables :-P)

Other than that, I don't see any other issues. Well documented. Nice work!

When your only tool is a hammer, every problem looks like a nail.

Report •

June 15, 2011 at 07:40:07
Excellent! That's just the info I needed. My solution isn't pretty but it works. I now have one "main" batch file that calls the two FTP batch files. The first logs in and gets a list of files and quits. The second then logs in, renames the files from the list, then downloads and deletes them from the FTP server. Finally, the main batch files takes over to rename the newly downloaded files locally and then cleans up the temporary FTP script files and the list.txt.

Report •

June 15, 2011 at 09:19:26
Thanks for the follow up and glad to hear it! As far as how "pretty" it is, as long as it does the job is all that should matter. Keep in the habit of documenting what's going on. It will pay off as you start writing more complex scripts/code/whatever. There's nothing worse than revisiting an undocumented piece of work, and not knowing what in the world is going on. Even if you wrote it yourself 3 days, 3 weeks, 3 months, 3 years ago!

When your only tool is a hammer, every problem looks like a nail.

Report •
Related Solutions

Ask Question