?
Solved

Advanced Batch Question - FINSTR on text file

Posted on 2011-10-18
3
Medium Priority
?
442 Views
Last Modified: 2012-05-12
Hi All,

I am writing a batch script that connets to an FTP server via the Windows command line client and puts between 1 to 5 files on there.

Here is the code I have so far to connect:

 
:: Let's write all the information we need to connect to the FTP server. Write the information to a .txt file in 
:: %TEMP% and all that when connecting.
:_WRITE_FTP_FILE
SETlocal enableextensions
ECHO open ftp.server > %TEMP%\ftp.txt 
:: We need to SET a variable here, as if we simply ECHO the username into the .txt file it will have a trailing
:: white space which means authentication will fail. The for loop strips the white space at the end.
SET _username=<username>
for /f "tokens=* delims= " %%a in ('ECHO %_username%') do SET _password=%%a
  ECHO %_username%>> %TEMP%\ftp.txt
:: Same as above; the for loop strips the trailing white space from the password.
SET _password=<password>
for /f "tokens=* delims= " %%a in ('ECHO %_password%') do SET _password=%%a
  ECHO %_password%>> %TEMP%\ftp.txt
  endlocal

:: Setting the variables for the day, month and year so these can be ECHOed to the .txt file and the directory
:: structure recreated.
SET _DATE=%date%
SET _YEAR=%_DATE:~10,4%
ECHO mkdir %_YEAR%>> %TEMP%\ftp.txt
ECHO cd %_YEAR%>> %TEMP%\ftp.txt
SET _MONTH=%_DATE:~7,2%
ECHO mkdir %_MONTH%>> %TEMP%\ftp.txt
ECHO cd %_MONTH%>> %TEMP%\ftp.txt
SET _DAY=%_DATE:~4,2%
ECHO mkdir %_DAY%>> %TEMP%\ftp.txt
ECHO cd %_DAY%>> %TEMP%\ftp.txt
:: Set the transfer mode to Binary and upload our files!
ECHO type binary >> %TEMP%\ftp.txt
ECHO put "C:\Daily Insight Report Files\Metro Insights_Slide1*" >> %TEMP%\ftp.txt
ECHO put "C:\Daily Insight Report Files\Metro Insights_Slide2*" >> %TEMP%\ftp.txt
ECHO put "C:\Daily Insight Report Files\Regional Insights_Slide1*" >> %TEMP%\ftp.txt
ECHO put "C:\Daily Insight Report Files\Regional Insights_Slide2*" >> %TEMP%\ftp.txt
ECHO put "C:\Daily Insight Report

Open in new window


So it writes information needed to connect to the ftp server in a text file, and also uses some variables to pull todays date so that it can create file structure on that server. It is looking for 5 potential files; they are:

Metro Insights_Slide1  ----->   ] Metro* goes together
Metro Insights_Slide2  ----->

Regional Insights_Slide1 -----> ] Regional* goes together
Regional Insights_Slide2

National Insights_Slide1

I then use the following command to connect to the FTP server and output the result to a log file:

 
ftp -s:%TEMP%\ftp.txt >%TEMP%\CSDIR_ftpconnect_log.txt

Open in new window


I am using FINDSTR to do some error checking and reporting. For example, I search that file for '230' and if it is found I know they have successfully logged in.

What I need to do however, is display a summary of what files were copied over. There are 3 options:

Metro Files
Regional Files
National

Here is what is written to the text file when (for example) all 5 files are written:

 
ftp> put "C:\Daily Insight Report Files\Metro Insights_Slide1*" 
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 144893 bytes sent in Seconds Kbytes/sec.
ftp> 0.0113172.09put "C:\Daily Insight Report Files\Metro Insights_Slide2*" 
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 97966 bytes sent in Seconds Kbytes/sec.
ftp> 0.0116327.67put "C:\Daily Insight Report Files\Regional Insights_Slide1*" 
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 101916 bytes sent in Seconds Kbytes/sec.
ftp> 0.0116986.00put "C:\Daily Insight Report Files\Regional Insights_Slide2*" 
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 117034 bytes sent in Seconds Kbytes/sec.
ftp> 0.0113003.78put "C:\Daily Insight Report Files\National Insights_Slide1*" 
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
ftp: 99162 bytes sent in Seconds Kbytes/sec.
ftp> 0.0116527.00quit

Open in new window


So, if it is possible, what I need is to be able to determine exactly what files have been successfully copied over (perhaps by somehow looking for the file name and then the 226 File receive OK) and displaying a summary.

So in this example we have copied all the files over, so I would like to print to the screen:

The following files have been successfully uploaded:

Metro
Regional
National

Of course, if ony Regional* files were copied I would like to display only that.

If anyone could help out that would be most appreciated. Please do not hesitate to ask for any more code or information or make any suggestions.

Cheers!
0
Comment
Question by:agbnielsen
3 Comments
 
LVL 16

Accepted Solution

by:
AlexPace earned 1400 total points
ID: 36990552
... trying to think outside the box here ... what if you delete everything before the uploads begin so you are starting with a blank slate... then you do your uploads... then you get a directory listing... so anything in the directory listing must have been something that you uploaded!  Tada!  

Anyway, I guess I am bored because here is your logic implmented as a Robo-FTP script that sends an email to report which files were uploaded and attaches a copy of the directory listing returned by te remote server as a sort of "receipt" showing the files were transferred.

SETEXTRACT month = %date "-" 1
SETEXTRACT day = %date "-" 2
SETEXTRACT year = %date "-" 3
SET year = "20" + year
SET files_sent = ""
FTPLOGON "ftp.mydomain.com" /user="UserID" /pw="Secret!"
FTPMAKEDIR year
FTPCD year
FTPMAKEDIR month
FTPCD month
FTPMAKEDIR day
FTPCD day
WORKINGDIR "C:\Daily Insight Report Files\"
:step_one
SET file_spec = "Metro Insights_Slide1*"
SENDFILE file_spec
IFERROR GOTO step_two
SET files_sent = files_sent + file_spec + %crlf 
:step_two
SET file_spec = "Metro Insights_Slide2*"
SENDFILE file_spec
IFERROR GOTO step_three
SET files_sent = files_sent + file_spec + %crlf 
:step_three
SET file_spec = "Regional Insights_Slide1*"
SENDFILE file_spec
IFERROR GOTO step_four
SET files_sent = files_sent + file_spec + %crlf 
:step_four
SET file_spec = "Regional Insights_Slide2*"
SENDFILE file_spec
IFERROR GOTO step_five
SET files_sent = files_sent + file_spec + %crlf 
:step_five
SET file_spec = "National Insights_Slide1*" 
SENDFILE file_spec
IFERROR GOTO check_files_sent
SET files_sent = files_sent + file_spec + %crlf 
:check_files_sent
IFNSTRCMP file_sent "" GOTO done
SET file_sent = "None."
:done
DELETE "sitelist.txt"  ** remove existing directory listing file
FTPLIST                ** create new directory listing file
FTPLOGOFF
SET from = "Robo-FTP Service"  
SET email = "robot@acme-widget.com"
SET subj = "Automated upload report"
SET body = "The following file(s) were uploaded: " + %crlf + files_sent
SET attach = "sitelist.txt"  ** directory listing file as attachment
CREATEMAIL from email subj body attach
SENDMAIL "120.33.13.10" "Help Desk" "helpdesk@acme-widget.com"

Open in new window


I made this for my own twisted amusement so don't feel like you need to try it.  If you do want to try it you'd need to change the login credentials on line 6 and the email details near the bottom around line 46 and then run it in Robo-FTP.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 600 total points
ID: 36991136
Good idea Alex.  If that was possible it would be best, don't have time to write it at the moment mind.

I guess it would be roughly:

First script:
Delete all existing files
Upload all your files
Disconnect

Second script:
Do a dir listing, redirected into a file

Check dir listing with file sizes and names against local DIR of same files using a for loop.

Robo-ftp makes things easier for stuff like this but if you need/want to stick with ftp.exe you might want to look at some of the other ways of using it in a script without the external txt file being created, I like method #3 best for most jobs:

http://scripts.dragon-it.co.uk/links/batch-ftp-scripting

Steve
0
 

Author Closing Comment

by:agbnielsen
ID: 37004396
Thanks Alex, didn't even think of that and I can definately use your logic to complete what I need.

And thanks for the info Dragon, better to have it without the text file for sure.

Cheers
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn how to count occurrences of each item in an array.
Simple Linear Regression
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question