Using %USERNAME% environment variable in Windows batch file with command-line ftp
Solution 1
Here is another batch file solution with code similar to code written by Martin Prikryl with three enhancements.
%USERPROFILE%
is used instead ofC:\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 driveC:
or in a different directory thanC:\Users
which is of course possible too.%SystemRoot%\System32\ftp.exe
is used in the batch file instead of justftp
to make this batch file work also if by chance there is an ftp.* file with a file extension listed in environment variablePATHEXT
in current directory or any other directory in environment variablePATH
and not being theftp
executable in Windows system directory.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
CODY FERRELL
Updated on June 14, 2022Comments
-
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-codingCody
, when used withftp
?-
SomethingDark over 6 yearsHow 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 over 6 yearsI posted the wrong one, this is the hard-coded version, I do have a batch script to echo everything saved.
-
Compo over 6 yearsI think I'd be tempted to use,
CD .\wwwhome\Logs
orCD wwwhome\Logs
instead ofcd \wwwhome\Logs\
. I may even be further tempted by considering the/D
option withCD
. -
CODY FERRELL over 6 yearsmay I ask why? trying to learn
-
-
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 over 6 yearsI guess that you named your batch file
ftp.bat
. So the last line re-executes the batch file itself and not theftp.exe
. -
CODY FERRELL over 6 yearsAlso, is there anyway to rename the file as lets say
%random%
after or before the upload?