Using variables in ftp connection by cmd

后端 未结 3 1571
逝去的感伤
逝去的感伤 2021-01-07 00:55

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 %

相关标签:
3条回答
  • 2021-01-07 01:39

    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
    
    0 讨论(0)
  • 2021-01-07 01:43

    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.

    0 讨论(0)
  • 2021-01-07 01:45

    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.

    0 讨论(0)
提交回复
热议问题