2017-06-20 246 views
1

我試圖將XML文件從FTP位置複製到應用程序服務器 - 但不是所有文件。新文件在ftp位置每隔半小時存檔一次。我只需要根據時間戳和日期傳輸新文件。使用Windows批處理文件從FTP服務器下載今天的文件

我目前使用以下2個文件從FTP位置複製所有文件。

批處理文件:

ftp -i -s:D:\ftp_commands.txt -n <host name> 

文本文件(ftp_commands.txt):

user <username> <password> 
cd <source path> 
lcd <destination path> 
mget * 
bye 

誰能幫我複製基於時間戳或日期的文件?

+0

我不認爲這是可能的與Windows的''ftp'工具... – aschipfl

+0

是的,如果時間戳是文件名的一部分,這可能是可能的,但我不知道用ftp獲取最後修改日期的方法。 – SomethingDark

+0

你是什麼意思*»只傳輸新文件«*?你想複製一定的最大年齡的文件,或者你需要一定數量的最新文件嗎?後者可以很容易地使用'ftp.exe'實現... – aschipfl

回答

1

使用Windows批處理文件和內置FTP客戶端(ftp.exe)實現這是一個非常複雜的任務。

使用PowerShell或其他更強大的語言會更容易。


使用功能更強大的FTP客戶端甚至更容易。

例如WinSCP FTP client支持時間限制。

用的WinSCP,一個批處理文件來下載今天的文件是微不足道的:

winscp.com /ini=nul /log=todays.log /command^
    "open ftp://username:[email protected]/"^
    "get /remote/path/*>=%%TIMESTAMP#yyyy-mm-dd%% C:\local\path\"^
    "exit" 

它使用%TIMESTAMP% syntaxfile mask with a time-constraint

參見:

(我的WinSCP的作者)

+0

你說*這是一個非常複雜的...... * *,這是否意味着它可以用'ftp.exe'實現? – aschipfl

+1

@aschipfl [「批處理文件」是圖靈完整語言](https://stackoverflow.com/q/11126539/850848),所以任何事情都是可能的 - 運行'ftp.exe'一次以列出遠程目錄 - 解析列表在一個非常複雜的批處理文件中挑選今天的文件 - 基於此,生成一個新的'ftp.exe'腳本來下載選擇的文件。 –

+1

我明白了,謝謝!所以在將來使用術語[*»不可能«*]時,我必須更加小心(https://stackoverflow.com/questions/44663446/download-only-todays-files-from-ftp-server-using- windows-batch-file/44668702#comment76313668_44663446)... – aschipfl

0

由於用戶Martin Prikrylcomment指出的那樣,可以使用Windows的本地FTP命令ftp.exe來實現自己的目標,雖然它可能不那麼容易。所以我不得不接受挑戰...

這是一個腳本,下載給定數量的最新(最新)文件。這可以修改爲考慮時間戳而不是計數,但由於日期/時間格式可能取決於FTP主機和/或本地機器,並且我不知道在執行FTP命令dir時接收的格式是什麼,我決定先去伯爵。所以這裏是:

@echo off 
setlocal EnableExtensions DisableDelayedexpansion 

rem // Define constants here: 
set "_FTP_LIST=%TEMP%\ftp_list_%RANDOM%.txt" & rem // (FTP script for listing files) 
set "_FTP_RECV=%TEMP%\ftp_recv_%RANDOM%.txt" & rem // (FTP script for getting files) 
set "_FTP_LTMP=%TEMP%\ftp_list_%RANDOM%.tmp" & rem // (file to store remote file list) 
set "_FTP_HOST=<host name>"  & rem // (name of FTP host) 
set "_FTP_USER=<username>"   & rem // (user name to login to the FTP host) 
set "_FTP_PASS=<password>"   & rem // (pass word to login to the FTP host) 
set "_FTP_RSRC=<source path>"  & rem // (path to remote source location) 
set "_FTP_LDST=<destination path>" & rem // (path to local destination location) 
set "_REVERSE=" & rem // (set to any value to get the oldest not the newest files) 
set /A "_COUNT=1" & rem // (number of most recent or newest remote files to receive) 

rem // Check if revert flag is set, force sort order to be in decreasing age in case: 
if defined _REVERSE (set "REV=r") else (set "REV=") 

rem // Build FTP script for listing remote files sorted by age: 
> "%_FTP_LIST%" (
    rem // Check whether use name is given: 
    if defined _FTP_USER (
     rem // Avoid auto-login: 
     set "SWITCH=-n" 
     rem // Write command to login: 
     setlocal EnableDelayedExpansion 
     echo user "!_FTP_USER!" !_FTP_PASS! 
     endlocal 
    ) else (
     rem // Attempt to login anonymously: 
     set "SWITCH=-A" 
    ) 
    rem // Write command to change to the desired remote location: 
    echo cd "%_FTP_RSRC%" 
    rem // Write command to list remote files sorted by increasing age: 
    echo ls -t%REV% "%_FTP_LTMP%" 
    rem // Write command to leave the FTP host: 
    echo bye 
) 

rem /* Execute FTP script to list remote files sorted by age and write result to a 
rem temporary file, which is going to be read and parsed later: */ 
ftp -i -v %SWITCH% -s:"%_FTP_LIST%" "%_FTP_HOST%" 

rem // Build FTP script for downloading the newest remote files: 
> "%_FTP_RECV%" (
    rem // Check whether use name is given: 
    if defined _FTP_USER (
     rem // Write command to login: 
     setlocal EnableDelayedExpansion 
     echo user "!_FTP_USER!" !_FTP_PASS! 
     endlocal 
    ) 
    rem // Write command to change to the desired remote location: 
    echo cd "%_FTP_RSRC%" 
    rem // Write command to change to the desired local location: 
    echo lcd "%_FTP_LDST%" 
    rem // Reset index used to extract the listed remote files: 
    set /A "INDEX=0" 
    rem /* Read the temporary file containing the list of remote files sorted by age, 
    rem loop through them and dynamically build the download commands: */ 
    for /F usebackq^ delims^=^ eol^= %%L in ("%_FTP_LTMP%") do (
     rem // Increment index: 
     for /F %%K in ('set /A "INDEX+1"') do (
      set /A "INDEX=%%K" 
      rem /* Check whether index already reached given count of remote files and 
      rem if not, write command to download a single file: */ 
      if %%K LEQ %_COUNT% echo get "%%L" 
     ) 
    ) 
    rem // Write command to leave the FTP host: 
    echo bye 
) 

rem // Ensure that the local destination directory exists: 
md "%_FTP_LDST%" 2> nul 

rem // Execute FTP script to download the newest remote files: 
ftp -i -v %SWITCH% -s:"%_FTP_RECV%" "%_FTP_HOST%" 

rem // Clean up temporary files: 
del "%_FTP_LIST%" "%_FTP_RECV%" "%_FTP_LTMP%" 

endlocal 
exit /B 
+0

謝謝@aschipfl!這看起來不錯。我用您的代碼替換了我的代碼並添加了所需的詳細信息。但是,它不起作用。你能否檢查它是否在你的系統上運行。 – Misscurious

+0

你的意思是「它不工作」?請詳細說明。我在我的系統上檢查過它,它按預期工作...... – aschipfl

相關問題