How do I rename a file to user input and then FTP it, all from a batch file?
April 15, 2008 1:58 PM   Subscribe

I want to create a batch file compatible with Windows XP and Vista that will do the following: 1) Get user input (name) 2) Rename an existing file to the input name string 3) Upload the renamed file to an anonymous FTP server that takes neither username nor password (no prompting)

I don't know what the differences are between XP and Vista in terms of command line--I did read somewhere that Vista reintroduced CHOICE, but I don't want to use that if XP users don't have it. Would /p still work? I'm completely stuck on how to get the input string into a new filename.

For the FTP part, I would like the batch file to show that it's being uploaded and to wait, or something to that effect.

(Do both OSes have the same ftp.exe? But I don't know if users will have it on their systems, and it may be better to find a free command line FTP app that can upload to an anonymous FTP server without username/password. Suggestions?)

Sorry if you think there are obvious answers elsewhere, but my Google-fu isn't helping much. Thanks in advance!
posted by Ky to Computers & Internet (8 answers total) 3 users marked this as a favorite
Call the input string as a parameter to the batch file (%1 %2 ...)
The command line FTP on XP and Vista is the same. You can create a file with a list of the ftp commands (-s:filename), or anonymous (-A).
posted by mphuie at 3:12 PM on April 15, 2008

Best answer: I've done the ftp step a lot. Your batch file would contain a line like this:
  FTP -s:C:\misc\login.ftp
and C:\misc\login.ftp would contain this (plug in appropriate values for username and password):
  CD /adirectory
  PUT C:\tmp\ebillpmt.rpt ebillpmt.txt
As the file is being transferred, a hashmark ('#') will be displayed every time a "chunk" (1K, 512 bytes, etc.) is transferred. You could end your batch file with something like ECHO Finished! if you wanted.

As far as the input goes, as I recall, there's no DOS command for reading from the keyboard. You could, as mphuie suggested, start the batch file with a parameter and refer to it in your batch file as %%1. Send me a message if you'd like more info – I'll be glad to help.
posted by davcoo at 4:04 PM on April 15, 2008

Best answer: I don't know if this works in Vista, but to read a string from the terminal in XP into a variable use set /p. Like this:

set /p filename=Enter filename:
echo Filename is %filename%

The left side of the equals sign is the variable name, the right side is the prompt string.

It probably isn't important to you, but the /p modifier doesn't exist in Win 95/98 and may or may not work in 2000, but I think all flavors of DOS prior to XP had choice so you can use that instead.
posted by harkin banks at 5:42 PM on April 15, 2008

Slick, harkin banks!
posted by davcoo at 5:58 PM on April 15, 2008

Response by poster: harkin banks, I found here that Vista does have SET /P (although probably "enhanced"), so that's good for me. :)

For reference, this is what I hacked together thanks to all of your tips (I'm using the 7zip command-line freeware), and it seems to work great:

@echo off
set /p filename=What is your first name (then press Enter)?
7z a -tzip directorytobeaddedtoarchive\
FTP -s:remote.ftp -A
echo Finished uploading your project set!


CD /putstuffhere
PUT *.zip


I don't suppose there's any way to suppress/silence the FTP information spit into the command window besides the hash marks? Oh well. I'm just happy it works! Thanks again.
posted by Ky at 11:39 PM on April 15, 2008

A couple of comments:

1) the way you have your ftp script written, it will upload every .zip file in your working directory to the ftp server. This might not be the best approach. One way to to specify the name of the file to upload is to generate remote.ftp on the fly, like this:

echo OPEN > remote.ftp
echo BINARY >> remote.ftp
echo HASH >> remote.ftp
echo CD /putstuffhere >> remote.ftp
echo PUT >> remote.ftp
echo BYE >> remote.ftp

Having the batch file generate remote.ftp also means that you can delete it once you're done with it, which may be desirable.

2) ftp.exe has a verbose mode, which by default is on (sending the command VERBOSE toggles it on/off). I've never turned it off, but turning it off might quiet things down some.
posted by harkin banks at 5:08 PM on April 16, 2008

If you can live without the hashes then adding '>nul' to the end of the FTP command will stop anything appearing on screen - you could just write a note with ECHO before the ftp command to warn people to wait and another afterwards to tell them it is complete (ideally you'd be able to tell from the ERRORLEVEL whether ftp worked but I'm not sure if ftp sets an ERRORLEVEL other than 0 ever).
posted by koshmar at 5:50 PM on April 16, 2008

Response by poster: Thanks for the additional comments!

harkin banks: The batch file is designed to produce a single custom-named zip in a specific folder with a handful of other specific files, so there should be no danger in multiple ZIPs. :) The in-batch generation is pretty slick, though, so I'll look into that.

Good tip too, koshmar.
posted by Ky at 10:44 AM on April 21, 2008

« Older How to share a short clip from a DVD with others.   |   How to render image like in google picasaweb? Newer »
This thread is closed to new comments.