Using %USERNAME% environment variable in Windows batch file with command-line ftp

12,467

Solution 1

Here is another batch file solution with code similar to code written by Martin Prikryl with three enhancements.

  1. %USERPROFILE% is used instead of C:\Users\%username% which makes this batch file solution work also on Windows XP and on machines on which the user's profile directory is not on drive C: or in a different directory than C:\Users which is of course possible too.

  2. %SystemRoot%\System32\ftp.exe is used in the batch file instead of just ftp to make this batch file work also if by chance there is an ftp.* file with a file extension listed in environment variable PATHEXT in current directory or any other directory in environment variable PATH and not being the ftp executable in Windows system directory.

  3. The ISO file name is renamed before upload with including a random decimal number between 0 and 32767 as asked for with a comment.

The command lines of enhanced batch file:

:RandomIsoName
set "RandomName=mini_%RANDOM%.iso"
if exist "%USERPROFILE%\Desktop\ISO's\%RandomName%" goto RandomIsoName
ren "%USERPROFILE%\Desktop\ISO's\mini.iso" "%RandomName%"

(
echo open hostname
echo username
echo password
echo cd \wwwhome\Logs\
echo put "%USERPROFILE%\Desktop\ISO's\%RandomName%"
echo bye
)>ftp.txt
%SystemRoot%\System32\ftp.exe -s:ftp.txt

Solution 2

You have to generate the ftp script using that variable:

echo open hostname>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo cd  \wwwhome\Logs\>>ftp.txt
echo put "C:\Users\%username%\Desktop\ISO's\mini.iso">>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt
Share:
12,467
CODY FERRELL
Author by

CODY FERRELL

Updated on June 14, 2022

Comments

  • CODY FERRELL
    CODY FERRELL almost 2 years
    ftp
    open ftp.drivehq.com 
    username
    password
    cd  \wwwhome\Logs\
    put "C:\Users\Cody\Desktop\ISO's\mini.iso"
    bye
    exit
    

    How do you use %USERNAME% instead of hard-coding Cody, when used with ftp?

    • SomethingDark
      SomethingDark over 6 years
      How are you generating and storing these FTP commands? If you've just hard-coded them in a textfile, yeah, it's not going to work; FTP isn't batch. If you write a batch script to echo everything to a text file, it will work.
    • CODY FERRELL
      CODY FERRELL over 6 years
      I posted the wrong one, this is the hard-coded version, I do have a batch script to echo everything saved.
    • Compo
      Compo over 6 years
      I think I'd be tempted to use, CD .\wwwhome\Logs or CD wwwhome\Logs instead of cd \wwwhome\Logs\ . I may even be further tempted by considering the /D option with CD.
    • CODY FERRELL
      CODY FERRELL over 6 years
      may I ask why? trying to learn
  • CODY FERRELL
    CODY FERRELL over 6 years
    @MartinPrikryl I accepted your answer without testing the script. For some reason it just keeps overwriting the ftp.txt file repeatedly and never executing ftp -s:ftp.txt
  • Martin Prikryl
    Martin Prikryl over 6 years
    I guess that you named your batch file ftp.bat. So the last line re-executes the batch file itself and not the ftp.exe.
  • CODY FERRELL
    CODY FERRELL over 6 years
    Also, is there anyway to rename the file as lets say %random% after or before the upload?