3

I'm connecting to an FTP server of my website using cmd.exe (on Windows 7). However, when I want to rename a file on the remote host, I'm able to only rename it to a static text (first word, if there's multiple words separated by space)

What I want to do is renaming the file to the output of a command (dynamic). For example the output of the command date is

The current date is: Sat 06/18/2016

I want the name of the file to be the result of the date command.

Thanks in advance!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Qada
  • 33
  • 4

1 Answers1

1

Start by collecting the command output to a variable.

Then, use the variable to generate the ftp.exe script on the fly.

FOR /F "tokens=* USEBACKQ" %%F IN (`date /t`) DO (
  SET OUTPUT=%%F
)

echo open ftp.example.com>ftp.txt
echo user>>ftp.txt
echo password>>ftp.txt
echo rename current_name %OUTPUT%>>ftp.txt
echo bye>>ftp.txt

ftp -s:ftp.txt
Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks, it worked, but when it renames the file, I only get the first word (before the first _space_) of the result (example: The date is 19/06 -> The). I tried deleting spaces and taking only numbers from the variable and adding them to a new variable and then sending the request, but failed. Any idea? – Qada Jun 19 '16 at 03:18
  • Edit: solved it. Was using the replace character command in the wrong way.. Now I can remove spaces and the file name will be one part. If you have any idea how to upload with spaces, it will be awesome! If you don't, no problem.. Thanks again, that helped a lot and more than I wanted. – Qada Jun 19 '16 at 03:29
  • The `date /t` should not print the "The date is" prefix. Anyway, just wrap the name to double-quotes, like `echo rename current_name "%OUTPUT%">>ftp.txt` – Martin Prikryl Jun 19 '16 at 14:49
  • That worked.. It's strange how I tried the same thing (adding double-quotes) but it didn't work, maybe an additional code was making troubles.. Now everything works fine as I wanted! Thanks Matrin! – Qada Jun 19 '16 at 20:48