Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Operating System - Microsoft
cancel
Showing results for 
Search instead for 
Did you mean: 

DOS Batch file for FTP and rename

SOLVED
Go to solution
Printaporn_1
Esteemed Contributor

DOS Batch file for FTP and rename

Hi,
How can I develop a dos .bat file to ftp
and rename all *.txt file to *.old.

Thanks in advance,
enjoy any little thing in my life
9 REPLIES
Denver Osborn
Honored Contributor

Re: DOS Batch file for FTP and rename

You didn't mention what version of windoze.... anywho, this may work for you.

@echo off
echo open 192.168.0.1>%TEMP%\ftp.txt
echo USER username passwd>>%TEMP%\ftp.txt
echo lcd c:\my_dir>>%TEMP%\ftp.txt
echo cd /tmp>>%TEMP%\ftp.txt
echo mget file*.txt>>%TEMP%\ftp.txt
echo quit>>%TEMP%\ftp.txt
echo bye>>%TEMP%\ftp.txt
FTP -i -n -s:%TEMP%\ftp.txt
del %TEMP%\ftp.txt
cd c:\my_dir
ren *.txt *.old


Cut/paste the example to your batch file, change username/passwd/ip...etc to your info and run it.

If you need to upload the files to the ftp after they're renamed, use the same as abov... just modify it so that it does an mput.

Hope this helps,
-Denver
Printaporn_1
Esteemed Contributor

Re: DOS Batch file for FTP and rename

Hi,

Thanks Denver for your post.
But my requirement is FTP some files to host
after finish FTP we will rename those file that we just FTP. To ensure that it was sucessful ftp.
We don't want to rename at local then ftp.

Any more advise is welcome.
thanks in advance,
enjoy any little thing in my life
Denver Osborn
Honored Contributor
Solution

Re: DOS Batch file for FTP and rename

If the ftp server supports RNTO RNFR then you should be able to rename a file.

@echo off
echo open 192.168.0.1>%TEMP%\ftp.txt
echo USER username passwd>>%TEMP%\ftp.txt
echo lcd c:\my_dir>>%TEMP%\ftp.txt
echo cd /tmp>>%TEMP%\ftp.txt
echo mput file*.txt>>%TEMP%\ftp.txt
echo ren file1.txt>>%TEMP%\ftp.txt
echo file1.old>>%TEMP%\ftp.txt
echo quit>>%TEMP%\ftp.txt
echo bye>>%TEMP%\ftp.txt
FTP -i -n -s:%TEMP%\ftp.txt
del %TEMP%\ftp.txt

The ftp server I tested with supported RNTO/RNFR (use remotehelp to verify). Although I could rename a file, I wasn't able to use wildcards with ren through ftp.

If you need to rename all *.txt files to *.old after they've been uploaded to ftp server, then maybe there is a way you could automate the list creation to put the entries in the %TEMP%\ftp.txt file for you.... unfortunately I'm a unix ggek and not sure of any dos utils to help build/format the file from a dir listing to make it look like

echo ren file1.txt
echo file1.old
echo ren file2.txt
echo file2.old
echo ren .... etc...


Hope this helps,
-Denver

Antoniov.
Honored Contributor

Re: DOS Batch file for FTP and rename

Hi
Do you know name of files to download?
If yes user rename command.
You could:
a) Load files from remote as suggested by Denver into local temp folder.
b) Then you rename on FTP server
@echo off
echo open 192.168.0.1>%TEMP%\ftp.txt
echo username >>%TEMP%\ftp.txt
echo password >>%TEMP%\ftp.txt
rem here it is a uniqueline
for %%i in (*.*) do echo rename %%i *.old >> %TEMP%\ftp.txt
rem end of line
echo bye >> %TEMP%\ftp.txt
FTP -i -n -s:%TEMP%\ftp.txt
del %TEMP%\ftp.txt

Bye
Antoniov

Antonio Maria Vigliotti
Ron Kinner
Honored Contributor

Re: DOS Batch file for FTP and rename

I have a Perl program that I modified which may do about what you want. The original program was written to check a website and download any new files. It now looks in a certain folder on its own harddrive to see if there are any new files, if it finds any, then it ftps them to the server. If the ftp of a file is successful then it moves it to a Sent folder. It also purges the Sent folder every time it runs of any files which are over X days old. It's a lot more reliable than DOS.

You would have to install Perl on your PC. It's free but takes about 6 Meg of space. If you are interested let me know and I will dig it out.

Ron
Denver Osborn
Honored Contributor

Re: DOS Batch file for FTP and rename

thanks to Antionov for the "for %%i" info about dos.... gave me an idea to create the file list (may depend on your ver of Windows/Dos cmd). Antioniov's ren would rename the file from file1.txt to file1.txt.old instead of file1.old. This change below to the for %%i will make the ren from file1.txt to file1.old...


@echo off
echo open 192.168.0.1>%TEMP%\ftp.txt
echo USER username passwd>>%TEMP%\ftp.txt
echo lcd c:\my_dir>>%TEMP%\ftp.txt
echo cd /tmp>>%TEMP%\ftp.txt
echo mput file*.txt>>%TEMP%\ftp.txt
for %%i in (*.txt) do echo ren %%~ni.txt %%~ni.old >>%TEMP%\ftp.txt
echo quit>>%TEMP%\ftp.txt
echo bye>>%TEMP%\ftp.txt
FTP -i -n -s:%TEMP%\ftp.txt
del %TEMP%\ftp.txt



-Denver
Ganesh Babu
Honored Contributor

Re: DOS Batch file for FTP and rename

Hi Denver,

echo mput file*.txt>>%TEMP%\ftp.txt
for %%i in (*.txt) do echo ren %%~ni.txt %%~ni.old >>%TEMP%\ftp.txt

i think the above 2 lines should look like

echo mput file*.txt>>%TEMP%\ftp.txt
for %%i in (file*.txt) do echo ren %%i.txt %%i.old >>%TEMP%\ftp.txt


Ganesh
Printaporn_1
Esteemed Contributor

Re: DOS Batch file for FTP and rename

Thanks ! the script really work for me with little modification.
enjoy any little thing in my life
jonathan2
Visitor

Re: DOS Batch file for FTP and rename

You can very easily develop a dos .bat file to ftp and rename all *.txt file to *.old. by simply using Batch Rename Files Tool. You can easily found hier BatchRenameFiles.org. It is very powerful and simple to use software.