2013-09-24 84 views
0

在FTP目錄中的文件我有一個由每5分鐘調度窗口服務運行的批處理文件。批處理文件爲特定文件掩碼執行mget:* .ext。 後mget它確實mdel * .ext。 最近我遇到了一個問題:當執行mget和mdel之間創建文件z.ext時,該文件被刪除,但它不在mget中。哪個課程有意義。 因此,現在我正在尋找適當的windows ftp命令,它們將循環遍歷文件並通過文件執行mget和mdel文件。 這可能嗎?遍歷使用bat文件

回答

1

FTP scriptng是非常有限的,但我認爲你可以使用this approach。基本上,你必須運行兩次ftp,首先收集下載的文件列表,然後第二次下載(和刪除)每個文件(加上大量的cmd腳本),你仍然沒有得到任何錯誤處理和ftp服務器並不完全知道它們的容錯性。

從上面的網站上的相關的例子是:

@Echo Off 

REM -- Define File Filter, i.e. files with extension .txt 
Set FindStrArgs=/E /C:".txt" 

REM -- Extract Ftp Script to create List of Files 
Set "FtpCommand=ls" 
Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp" 
Rem Notepad "%temp%\%~n0.ftp" 

REM -- Execute Ftp Script, collect File Names 
Set "FileList=" 
For /F "Delims=" %%A In ('"Ftp -v -i -s:"%temp%\%~n0.ftp"|Findstr %FindStrArgs%"') Do (
    Call Set "FileList=%%FileList%% "%%A"" 
) 

REM -- Extract Ftp Script to download files that don't exist in local folder 
Set "FtpCommand=mget" 
For %%A In (%FileList%) Do If Not Exist "%%~A" Call Set "FtpCommand=%%FtpCommand%% "%%~A"" 
Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp" 
Rem Notepad "%temp%\%~n0.ftp" 

For %%A In (%FtpCommand%) Do Echo.%%A 

REM -- Execute Ftp Script, download files 
ftp -i -s:"%temp%\%~n0.ftp" 
Del "%temp%\%~n0.ftp" 
GOTO:EOF 


:extractFileSection StartMark EndMark FileName -- extract a section of file that is defined by a start and end mark 
::     -- [IN]  StartMark - start mark, use '...:S' mark to allow variable substitution 
::     -- [IN,OPT] EndMark - optional end mark, default is first empty line 
::     -- [IN,OPT] FileName - optional source file, default is THIS file 
:$created 20080219 :$changed 20100205 :$categories ReadFile 
:$source http://www.dostips.com 
SETLOCAL Disabledelayedexpansion 
set "bmk=%~1" 
set "emk=%~2" 
set "src=%~3" 
set "bExtr=" 
set "bSubs=" 
if "%src%"=="" set src=%~f0&  rem if no source file then assume THIS file 
for /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do (
    if /i "%%B"=="%emk%" set "bExtr="&set "bSubs=" 
    if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B) 
    if /i "%%B"=="%bmk%" set "bExtr=Y" 
    if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y" 
) 
EXIT /b 


[Ftp Script 1]:S 
!Title Connecting... 
open example.com 
username 
password 

!Title Preparing... 
cd public_html/MyRemoteDirectory 
lcd c:\MyLocalDirectory 
binary 
hash 

!Title Processing... %FtpCommand% 
%FtpCommand% 

!Title Disconnecting... 
disconnect 
bye 

可能是更好的做它在Python或其他一些腳本環境(假設你沒有部署問題。)