Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Batch File that writes certain contents of an FTP Log to another Text File.

Posted on 2005-05-14
4
Medium Priority
?
376 Views
Last Modified: 2008-03-10
I am in urgent need of a Batch File to do a certain task. Everyday reports are uploaded via FTP to a server into different directories. At the end of the FTP process, a FTP log is generated. I need help with a Batch file that checks the FTP log and lists down into a seperate txt file or a Word document,  the directory that the reports were uploaded into and all the files that were uploaded into this directory.

Below is a sample of the FTP log file generated:

The directories that these reports are uploaded to are like PO301001, PO302001, PO303001, PO304001 and so on.

The reports that are uploaded to these directories are in .pdf format.

The report names are like CTPD540_CMF-_20050511.pdf, CTPD550_CMF-_20050511.pdf
CTPD810_CMF-_20050511.pdf, CTPD961_200505.pdf, CTPD962_200505.pdf.

The date (20050511) at the end of these reports change on a daily basis.

What is needed is that only the directory name of where the files are FTP to is needed and the name of the report to be written to a seperate text file, for example:
PO301001
CTPD540_CMF
CTPD550_CMF
CTPD961
CTPD962

I urgently need help with this. I need a Batch File, not in VBS. I have a better understanding of Batch Files.


FTP LOG________________________________________________

Start of BO Reports File Transfer at  8:22:08 Thu 20050512
----------------------------------------------------------
ftp> Connected to 192.168.12.163

open 192.168.12.163
220 Microsoft FTP Service
ftp> user reports reports
331 Password required for reports.
230 User reports logged in.
ftp> ftp>
Interactive mode Off .

ftp> prompt
ftp>
bin

200 Type set to I.
ftp> Local directory now C:\MDB_BATCH_BO_REPORTS\Result_Report.

ftp> lcd C:\MDB_BATCH_BO_REPORTS\Result_Report
cd PO301001
250 CWD command successful.
ftp> cd WED
250 CWD command successful.
ftp> mdelete *.*
200 Type set to I.
ftp> mput CTPD*_PAL*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD540_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 15440 bytes sent in 0.05Seconds 328.51Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD550_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 15671 bytes sent in 0.03Seconds 489.72Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD810_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 15824 bytes sent in 0.03Seconds 510.45Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD970_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 15447 bytes sent in 0.02Seconds 965.44Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD971_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 15179 bytes sent in 0.03Seconds 489.65Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD974_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 15450 bytes sent in 0.03Seconds 498.39Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD975_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 95248 bytes sent in 0.13Seconds 761.98Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD976_PAL-_20050511.pdf.
226 Transfer complete.
ftp: 24996 bytes sent in 0.00Seconds 24996000.00Kbytes/sec.

ftp> mput CTPD930*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD930_20050511.pdf.
226 Transfer complete.
ftp: 60880 bytes sent in 0.00Seconds 60880000.00Kbytes/sec.

ftp> mput CTPD960*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD960_200505.pdf.
226 Transfer complete.
ftp: 102677 bytes sent in 0.09Seconds 1104.05Kbytes/sec.

ftp> mput CTPD961*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD961_200505.pdf.
226 Transfer complete.
ftp: 18476 bytes sent in 0.00Seconds 18476000.00Kbytes/sec.

ftp> mput CTPD962*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD962_20050511.pdf.
226 Transfer complete.
ftp: 16475 bytes sent in 0.00Seconds 16475000.00Kbytes/sec.

ftp> mput CTPD963*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD963_20050511.pdf.
226 Transfer complete.
ftp: 15885 bytes sent in 0.00Seconds 15885000.00Kbytes/sec.

ftp> cd ..
250 CWD command successful.
ftp> cd ..
250 CWD command successful.
ftp> ftp>
ftp>
ftp>
bin
200 Type set to I.
ftp> Local directory now C:\MDB_BATCH_BO_REPORTS\Result_Report.

ftp> lcd C:\MDB_BATCH_BO_REPORTS\Result_Report
cd PO302001
250 CWD command successful.
ftp> cd WED
250 CWD command successful.
ftp> mdelete *.*
200 Type set to I.
ftp> mput CTPD*_CMF*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD540_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 15938 bytes sent in 0.00Seconds 15938000.00Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD550_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 15671 bytes sent in 0.05Seconds 333.43Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD810_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 16374 bytes sent in 0.06Seconds 264.10Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD970_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 15447 bytes sent in 0.05Seconds 328.66Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD971_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 15179 bytes sent in 0.06Seconds 244.82Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD974_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 16236 bytes sent in 0.05Seconds 345.45Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD975_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 95410 bytes sent in 0.19Seconds 507.50Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD976_CMF-_20050511.pdf.
226 Transfer complete.
ftp: 35836 bytes sent in 0.00Seconds 35836000.00Kbytes/sec.

ftp> mput CTPD*_LAZ*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD540_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15440 bytes sent in 0.05Seconds 328.51Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD550_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15671 bytes sent in 0.06Seconds 252.76Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD810_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15826 bytes sent in 0.05Seconds 344.04Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD970_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15447 bytes sent in 0.06Seconds 245.19Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD971_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15179 bytes sent in 0.03Seconds 489.65Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD974_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15450 bytes sent in 0.02Seconds 965.63Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD975_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 95152 bytes sent in 0.17Seconds 553.21Kbytes/sec.

200 PORT command successful.
150 Opening BINARY mode data connection for CTPD976_LAZ-_20050511.pdf.
226 Transfer complete.
ftp: 15151 bytes sent in 0.00Seconds 15151000.00Kbytes/sec.

ftp> mput CTPD930*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD930_20050511.pdf.
226 Transfer complete.
ftp: 60880 bytes sent in 0.00Seconds 60880000.00Kbytes/sec.

ftp> mput CTPD960*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD960_200505.pdf.
226 Transfer complete.
ftp: 102677 bytes sent in 0.09Seconds 1092.31Kbytes/sec.

ftp> mput CTPD961*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD961_200505.pdf.
226 Transfer complete.
ftp: 18476 bytes sent in 0.00Seconds 18476000.00Kbytes/sec.

ftp> mput CTPD962*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD962_20050511.pdf.
226 Transfer complete.
ftp: 16475 bytes sent in 0.00Seconds 16475000.00Kbytes/sec.

ftp> mput CTPD963*.pdf
200 PORT command successful.
150 Opening BINARY mode data connection for CTPD963_20050511.pdf.
226 Transfer complete.
ftp: 15885 bytes sent in 0.00Seconds 15885000.00Kbytes/sec.

ftp> cd ..
250 CWD command successful.
ftp> cd ..
250 CWD command successful.
ftp> ftp>
cd ..
250 CWD command successful.
ftp> cd ..
250 CWD command successful.
ftp> ftp>
ftp>
quit
221  
 
End of BO Reports File Transfer at 12:11:34 Thu 20050512
----------------------------------------------------------


Thank You.

0
Comment
Question by:ben1211
  • 3
4 Comments
 
LVL 29

Expert Comment

by:Michael Pfister
ID: 14004972
I'd rather do that in a real programming language. No error checking here and the ">"-sign must be filtered out before you can start processing the log in a batch ...
Here is what I've tried:
Note the back-quotes in the first for command!

-----------------------------------------------
@echo off
Set OutFile=Result.txt
Set LogFile=ftp.log
for /f "usebackq delims=" %%a in (`findstr /v /C:">" "%LogFile%"`) Do Set Line=%%a& Call :Process
Goto :EOF
:Process
SET Check=%Line:~0,4%
If not Defined Check Goto :EOF
If /I "%Check%" == "cd P" Goto :Directory
If /I "%Check%" == "150 " Goto :File
Goto :EOF
:Directory
SET Directory=%Line:~3,99%
Echo %Directory%>> %OutFile%
Goto :EOF
:File
SET File=%Line:~44,99%
For /f "delims=-." %%b in ("%File%") do echo %%b>> %OutFile%
Goto :EOF
-----------------------------------------------

Result.txt:

PO301001
CTPD540_PAL
CTPD550_PAL
CTPD810_PAL
CTPD970_PAL
CTPD971_PAL
CTPD974_PAL
CTPD975_PAL
CTPD976_PAL
CTPD930_20050511
CTPD960_200505
CTPD961_200505
CTPD962_20050511
CTPD963_20050511
PO302001
CTPD540_CMF
CTPD550_CMF
CTPD810_CMF
CTPD970_CMF
CTPD971_CMF
CTPD974_CMF
CTPD975_CMF
CTPD976_CMF
CTPD540_LAZ
CTPD550_LAZ
CTPD810_LAZ
CTPD970_LAZ
CTPD971_LAZ
CTPD974_LAZ
CTPD975_LAZ
CTPD976_LAZ
CTPD930_20050511
CTPD960_200505
CTPD961_200505
CTPD962_20050511
CTPD963_20050511
0
 
LVL 29

Accepted Solution

by:
Michael Pfister earned 2000 total points
ID: 14004992
Sorry, I've missed one point. This should do what you want. Please not that it won't work beyond year 2999 ;-)))

@Echo off
Set OutFile="Result.txt"
Set LogFile="Ftp.log"
If not exist %LogFile% Goto :EOF
If exist %OutFile% del %OutFile%
for /f "usebackq delims=" %%a in (`findstr /v /C:">" %LogFile%`) Do Set Line=%%a& Call :Process
Goto :EOF
:Process
SET Check=%Line:~0,4%
If not Defined Check Goto :EOF
If /I "%Check%" == "cd P" Goto :Directory
If /I "%Check%" == "150 " Goto :File
Goto :EOF
:Directory
SET Directory=%Line:~3,99%
Echo %Directory%>> %OutFile%
Goto :EOF
:File
SET File=%Line:~44,99%
For /f "tokens=1,2 delims=_-." %%b in ("%File%") do Set File1=%%b& Set File2=%%c
SET Check=%File2:~0,1%
Echo %Check%
If "%Check%" == "2" (
      Echo %File1%>>%OutFile%
) Else (
      Echo %File1%_%File2%>>%OutFile%
)
Goto :EOF
0
 

Author Comment

by:ben1211
ID: 14006012
mpfister,

Hi! Thank you for the code, but could you please help me by explaining the code to me please, especially the FOR statements? In that way, I get to learn and understand what I'm doing instead of just getting the job done. Would be very grateful if you could help me out. And could you tell me why this code will not work beyond 2999? Where in your coding is this limitation?

You mentioned that you would rather do this using a real programming language. Would you use VBS? If so, could you give me the code and some explanation on it, please? Again, I would be very grateful.

Thank You

for /f "usebackq delims=" %%a in (`findstr /v /C:">" %LogFile%`) Do Set Line=%%a& Call :Process

For /f "tokens=1,2 delims=_-." %%b in ("%File%") do Set File1=%%b& Set File2=%%c
0
 
LVL 29

Expert Comment

by:Michael Pfister
ID: 14017373
for /f "usebackq delims=" %%a in (`findstr /v /C:">" %LogFile%`) Do Set Line=%%a& Call :Process

This was the most tricky one: it executes the findstr that filters all lines with a ">" in them. The output (without the ">" lines) is piped to the for statement, which sets each return line to the variable "Line" and calls the "Process" subroutine. usebackq is neccessay to have " and ´ quotes mixed.

SET File=%Line:~44,99%
For /f "tokens=1,2 delims=_-." %%b in ("%File%") do Set File1=%%b& Set File2=%%c

I snip off the starting part of the line "150 Opening BINARY mode data connection for " to get just the file name.
The for statement splits the filename in parts according to the delimiters "_-."

I.e.  CTPD974_CMF-_20050511.pdf. will be  split in  CTPD974 CMF 20050511 pdf and the first two values will be assigned to the variables File1 and File2.

The limitation comes from how I distinguish file name like CTPD970_LAZ-_20050511.pdf from CTPD930_20050511.pdf. Therefore I check the first digit of "File2" if it starts with "2":

SET Check=%File2:~0,1%
If "%Check%" == "2"  ...

Since you posted that this number is corresponing to the date my routine will fail if you come to year 3000 ;-).

Sorry, I'm not a real VBS pro....

Hope it helps,

Michael
PS: is your question here http://www.experts-exchange.com/Operating_Systems/Q_21421905.html answered?





0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

810 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