Using variables in ftp connection by cmd

2019-02-15 14:53发布

问题:

How I can use variables in ftp? This is what have i done: file a.bat:

set file=test ftp -s:b.txt IP

file b.txt

user password get %file% quit

And log shows that there isn't any %file%.

回答1:

ftp does not understand Windows environment variables. You have to generate your script file each time like this:

set file=test
set ftpscript=%TEMP%\ftp.txt
echo user> %ftpscript%
echo password>> %ftpscript%
echo get %file%>> %ftpscript%
echo quit>> %ftpscript%
ftp -s:%ftpscript% IP    

Here I have defined a temporary script in the temp directory so it does not pollute current directory.



回答2:

This an example for uploading mutiple files:

@echo off
Title Multiple file Upload by Hackoo on adrive
Color 0A
::****** Settings for FTP ************
Set FTPSERVER=ftp.adrive.com
Set USER=Your Login
Set Password=Your Password
Set LocalFolder=C:\Hackoo
Set RemoteFolder=/backupFolder
::************************************
::--- FTP commands below here ---
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo lcd %LocalFolder%
>> ft.do echo MkDir %RemoteFolder%
>> ft.do echo cd %RemoteFolder%
>> ft.do echo mput "*.*"
>> ft.do echo disconnect
>> ft.do echo bye
::************************************
ftp -s:ft.do
del ft.do
Pause

This an example in batch just for testing that can list only files from a folder located on a public ftp server like ftp.microsoft.com in order to create a list.txt file to download it after, so give a try and tell us the result.

@echo off
mode con cols=85 lines=22 & Color A
::***********************************
Set FTPSERVER=ftp.microsoft.com
Title Lister les fichiers et les dossiers sur un serveur FTP (%FTPSERVER%) by Hackoo
Set USER=anonymous
Set Password=anonymous@anonymous.com
Set DossierFTP=/bussys/winsock/winsock2/
Set DownloadFolder=winsock2
::*******************************************************
Goto Lister
:Lister
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
>> ft.do echo ls -h TLIST.txt
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9B
echo Download la liste
pause
Goto Download
::*********************************************************
:Download
> ft.do echo Open %FTPSERVER%
>> ft.do echo %USER%
>> ft.do echo %Password%
>> ft.do echo prompt n
>> ft.do echo bin
>> ft.do echo cd %DossierFTP%
for /F %%f in (TLIST.txt) do ( >> ft.do echo get %%f) 
>> ft.do echo bye
ftp -s:ft.do
del ft.do
CLS
Color 9A
pause
echo Deplacer la liste
Goto Deplacer
::*********************************************************
:Deplacer
Set Source=%~dp0
Set Destination=%Source%%DownloadFolder%
if not exist %DownloadFolder% MD %DownloadFolder%
for /F %%f in (TLIST.txt) do (move "%Source%%%f" "%Destination%")
pause


回答3:

The reason the %-artifact didn't work in the BAT file was that that token was never seen and replaced. Just as in a UNIX shell script, $-substitutions are performed when those lines are collected and tokenized. Since the line containing the %file% was input to FTP, it was collected and parsed by FTP, not by the "DOS" shell.

In UNIX, you can solve this problem by piping the directives through FTP. That is to say, while this (where $1 is a filename parameter supplied on the command line):

ftp << FTP_CMDS  
user joe  
pass joesPassword  
get $1  
quit  
FTP_CMDS

wouldn't work, THIS:

(echo user joe;  
&nbsp;echo pass joesPassword;  
&nbsp;echo get $1;  
&nbsp;echo quit ) | ftp

WOULD work.