2013-12-19 10 views
0

我可以使用下面的腳本刪除本地文件,如果它們不再存在於遠程目錄中嗎?目前這下載所有新文件,但不會刪除ftp服務器上不再存在的文件?FTP刪除本地文件,如果它們不再存在於遠程服務器

@Echo Off 

REM -- Define File Filter, i.e. files with extension .txt 
set FindStrArgs=/x "..*\...*" 
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 

回答

0

如果可能,請使用ftp客戶端,如lftp。它將爲您提供選項,以便在一行代碼中鏡像兩個ftp位置(請參閱鏡像選項)。還檢查了標誌:

--only-newer 
--parallel=10 
--delete 
+0

謝謝,我需要這是通過使用當前腳本在批處理文件。我需要在100臺電腦上運行這個腳本 – user1342164

相關問題