I am trying to write a batch file that can take user input of an FTP link, parse out the user, password and domain, write them to a file and the call the file as input for the FTP command. I have successfully written it, up until someone had a password with an !
in it.
The FTP links are system generated and may contain special characters (ie: !
, ^
, *
).
I am using setlocal delayedexpansion
as I read the full string (ie: ftp://userid:password@some.domain.com
) in a for loop using the /
, :
, and @
as delimiters.
I have tried to get creative and do endlocal delayedexpansion
after parseing the output, but then the tokens don't work because I am ending the expansion. I have also tried writing each token to its own file and then assigning the content of the file as a variable with no luck.
I think there should be a method to do this, but I keep hitting a wall.
Here is a snippet of my code:
@ECHO OFF
setlocal enableextensions enabledelayedexpansion
echo Example FTP link:
echo.
echo "ftp://xxxxxxxx:yyyyyyyy@some.where.com"
echo.
set /p ftplink= Please enter full FTP link:
echo.
set /p ftpfile= Please provide the file name you want to download:
:FTPbat
:: Generate a bat file for FTP retrieval
for /f "tokens=1-4 delims=/:@" %%a in ("%ftplink%") do (
set ftpdomain=%%~d
set ftpuser=%%~b
set ftppass=%%~c
)
echo open %ftpdomain%> get.ftp
echo %ftpuser%>> get.ftp
echo %ftppass%>> get.ftp
echo binary>> get.ftp
echo hash>> get.ftp
echo mget %ftpfile%>> get.ftp
echo disconnect>> get.ftp
echo quit>> get.ftp
:GetFTPFile
CLS
FTP -i -s:get.ftp
Here is an example FTP link:
ftp://username:pas!word@somewhere.com
Unfortunately, the special character can be anywhere in the password string or potentially in the username. Also, the username and password are not a uniform length (ie: 8 chars each). I found that out the hard way.
Any and all help is greatly appreciated.