FTP Batch Enhancement

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

FTP Batch Enhancement

#1 Post by dbutts » 02 Oct 2012 07:59

Hi all,
I'm using the wonderful FTP Upload batch file that I got from dostips.com located at http://www.dostips.com/DtTipsFtpBatchSc ... lyNewFiles

It does a great job at uploading only the files that haven't been uploaded... but... I need it to do one more thing... After it has uploaded "file1.zip" and "file2.zip", we need it to MOVE those files to the "uploaded" folder (e.g., c:\temp\uploaded\)

I've tried several different things using the variables in the original bat but just can't seem to make it work..

Ideas? Any help appreciated. I want it to work using the code below if possible, or a call to another bat would be acceptable.

ORIGINAL code

Code: Select all

@Echo Off
Setlocal Enabledelayedexpansion

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 upload files that don't exist in remote folder
Set "FtpCommand=mput"
For %%A In (%FileList%) Do set "Exist["%%~A"]=Y"
For /F "Delims=" %%A In ('"dir /b "%localdir%"|Findstr %FindStrArgs%"') Do (
    If Not defined 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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: FTP Batch Enhancement

#2 Post by abc0502 » 02 Oct 2012 08:09

all what you have to do is adding this command:

Code: Select all

move /y "*.zip" "C:\temp\uploaded\"

but note that it will rewrite zip files if there was same zip file name in the uploaded folder
and also will move all zip files, you can replace the * with the file name and repeat this line with different file names to move

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#3 Post by dbutts » 02 Oct 2012 08:17

Well, I had already tried that.. I added it to the end of the bat after the 'disconnect' and 'bye' lines..

It just hangs the bat.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: FTP Batch Enhancement

#4 Post by Squashman » 02 Oct 2012 08:25

dbutts wrote:Well, I had already tried that.. I added it to the end of the bat after the 'disconnect' and 'bye' lines..

It just hangs the bat.

Because you can't put it there. You need to put it before the GOTO :EOF line.

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#5 Post by dbutts » 02 Oct 2012 08:39

AHHHHHHH

I see!

Working fine now! Thanks so much for your help!

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#6 Post by dbutts » 09 Oct 2012 06:37

OK.. one more enhancement request

I'd to log the results in a text file (e.g., ftplog.txt) the date, time and file names that were uploaded & copied.. Date & time I've got ok.. I just can't make it grab the right variable for the filenames... e.g., "filename1.txt, filename2.txt were uploaded successfully and were copied to the \uploaded folder."

Any help greatly appreciated.



Code: Select all

@Echo Off
Setlocal Enabledelayedexpansion

Echo.  >>s:\crashdumps\ftplog.txt
Echo.=================== >>s:\crashdumps\ftplog.txt
echo.%Date% >>s:\crashdumps\ftplog.txt
echo.%Time% >>s:\crashdumps\ftplog.txt

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

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 upload files that don't exist in remote folder
Set "FtpCommand=mput"
For %%A In (%FileList%) Do set "Exist["%%~A"]=Y"
For /F "Delims=" %%A In ('"dir /b "%localdir%"|Findstr %FindStrArgs%"') Do (
    If Not defined 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"
move /y "*.zip" "s:\crashdumps\uploaded\"
echo Copied files to \uploaded >>s:\crashdumps\ftplog.txt
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 20121010 :$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 1.2.3.4
username
password

!Title Preparing...
cd "mydirectory"
lcd s:\mydirectory
binary
hash

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

!Title Disconnecting...
disconnect
bye





foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FTP Batch Enhancement

#7 Post by foxidrive » 09 Oct 2012 06:45

AFAICS there is no check that determines that the files were transferred successfully. It just assumes they were.

So it seems that you can change this line:

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

to this

For %%A In (%FtpCommand%) Do (
Echo.%%A
>>file.log echo. Files transferred %%A
)

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#8 Post by dbutts » 09 Oct 2012 06:58

thanks for super fast reply!

here is the code i used

Code: Select all

For %%A In (%FtpCommand%) Do (Echo.%%A>>ftplog.txt was uploaded)


but it returned:

Tue 10/09/2012
8:39:35.00
mput was uploaded
"filename.zip" was uploaded

how do i get rid of the 'mput was uploaded'

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FTP Batch Enhancement

#9 Post by foxidrive » 09 Oct 2012 08:02

This might work.

For %%A In (%FtpCommand%) Do (if /i not "%%A"=="mput" >>ftplog.txt Echo.%%A was uploaded)



I have to say that the script is too 'clever' and makes it more difficult to follow the logic on a casual glance. But I haven't tested it.
I also think it will fail to process extensionless files.

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#10 Post by dbutts » 09 Oct 2012 09:01

thanks for the suggestion but that didn't work..

i agree though.. this script is very clever. Whoever wrote it is pretty sharp.

i wonder if there is a way to log results w/o modifying the original

Code: Select all

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: FTP Batch Enhancement

#11 Post by foxidrive » 09 Oct 2012 09:21

dbutts wrote:thanks for the suggestion but that didn't work..


What was in the log file? According to what you showed was being entered in the log file above then it should work.

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#12 Post by dbutts » 09 Oct 2012 09:33

the bat bailed out...

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: FTP Batch Enhancement

#13 Post by Squashman » 09 Oct 2012 09:41

The only real way to log what files were uploaded successfully would be to capture the output of the FTP command. That is how we do it where I work. For all the automated FTP scripts we have we still have someone go in and manually check that all the files Transferred Successfully.

Code: Select all

ftp -i -s:"%temp%\%~n0.ftp" >FTPLOG.log


You could parse the log file with another command to see if there were any transfer failures.

dbutts
Posts: 10
Joined: 02 Oct 2012 06:41

Re: FTP Batch Enhancement

#14 Post by dbutts » 09 Oct 2012 10:00

OK.. that's more what i'm looking for..

where can i safely add this to the existing script?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: FTP Batch Enhancement

#15 Post by Squashman » 09 Oct 2012 10:07

dbutts wrote:OK.. that's more what i'm looking for..

where can i safely add this to the existing script?

Where the FTP command is in your script already! :roll:

Post Reply