Solved Batch Script Copy files FTP server check if exists then skip

August 1, 2012 at 12:31:20
Specs: Windows 7
batch script, I am creating a batch script and am a little confused, I need the script to copy files from my local folder to ftp server, check if any of the files we are copying to already exists in the ftp server, if it does then copy only the files that do not exist on server

I so far have the following which takes the files and copys to server but need help with the later part

@Echo off
Set _UserName=test
Set _Password=test
Set _LocalFolder=C:\Users\charlie\Documents
Set _RemoteFolder=ITEMS
Set _Filename=*.txt
Set _ScriptFile=ftp1
:: Create script
>>"%_ScriptFile%" Echo open %_FTPServerName%
>>"%_ScriptFile%" Echo %_UserName%
>>"%_ScriptFile%" Echo %_Password%
>>"%_ScriptFile%" Echo lcd %_LocalFolder%
>>"%_ScriptFile%" Echo cd %_RemoteFolder%
>>"%_ScriptFile%" Echo binary
>>"%_ScriptFile%" Echo prompt
>>"%_ScriptFile%" Echo mput %_Filename%
>>"%_ScriptFile%" Echo disconnect
:: Run script
ftp -s:"%_ScriptFile%"
Del "%_ScriptFile%

See More: Batch Script Copy files FTP server check if exists then skip

Report •

August 2, 2012 at 00:24:33
✔ Best Answer
Here's a better explanation for my post:
if you're like me, you have basic login script for ftp, f/e: "login"
step 1, make login script ("test") but incl. "ls" for "bare filenames" format
USER username
PASS pwd
CD target-dir
step 2: run ftp, using script "test" to generate list on local client:
ftp -n -s:test>ftp.out
step 3: get rid of junk from the list (assuming 12 lines header-junk)
@echo off & setlocal
for /f "tokens=* skip=12" %%a in (ftp.out) do (
if "%%a" equ "226 Transfer complete." goto :eof
>>remotdir echo %%a
goto :eof
step 4: in batch, use "remotdir" as basis for checking fname presence on remote
@echo off & setlocal
copy /y login newftp.sct
for /f "tokens=*" %%a in ('dir /b | findstr /v /L /g:remote') do >> newftp.sct echo put %%a
>> newfpt.sct echo quit
echo ok, now ftp will run
pause >nul
ftp -n -s:newfpt.sct
this "should" do what you want, lacking knowledge or experience with pwrshell myself.
pwrshell might let you do this in one "ftp session", but in a pinch, this might work.

Report •

August 2, 2012 at 10:34:12
If you're running this script on Windows 7, it might be easier to use PowerShell instead. I have not attempted it, and PowerShell doesn't automate as much as FTP.EXE, but it would allow you to read the directory listing and make a decision during the same FTP session.

How To Ask Questions The Smart Way

Report •

Related Solutions

Ask Question