parse text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

parse text file

#1 Post by joecepy » 06 Jan 2015 15:23

Hello,

I'm hoping someone can assist with the following. I have a psftp text file contains a lot of gibberish.

Code: Select all

Remote directory is now /xjp/utility/CapEx/Inbound/Program
psftp> ls *.csv
Listing directory /xjp/utility/CapEx/Inbound/Program
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 program2.csv
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 program5.csv
psftp> cd /xjp/utility/CapEx/Inbound/Program/ProgFinancial/
Remote directory is now /xjp/utility/CapEx/Inbound/Program/ProgFinancial
psftp> ls *.pdf
Listing directory /xjp/utility/CapEx/Inbound/Program/ProgFinancial
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 Financial_Analysis_program2.pdf
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 Financial_Analysis_program5.pdf
psftp> cd /xjp/utility/CapEx/Inbound/Project/
Remote directory is now /xjp/utility/CapEx/Inbound/Project
psftp> ls *.csv
Listing directory /xjp/utility/CapEx/Inbound/Project
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 project1.csv
psftp> cd /xjp/utility/CapEx/Inbound/Project/ProjFinancial/
Remote directory is now /xjp/utility/CapEx/Inbound/Project/ProjFinancial
psftp> ls *.pdf
Listing directory /xjp/utility/CapEx/Inbound/Project/ProjFinancial
-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 Financial_Analysis_project1.pdf
psftp> quit


I want to search for following line of string in a text file that contains "-rw-r--r--" and parse out the filename so it can vary from project1.csv or Financial_Analysis_project1.pdf, etc.

for example I'd read in the following line of string

Code: Select all

-rw-r--r--    1 iqtrack  pcfd            0 Jan  6 15:06 project1.csv


and parse out

Code: Select all

project1.csv


Is this doable or am I reaching?

thanks

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: parse text file

#2 Post by joecepy » 06 Jan 2015 16:06

found solution. I was racking my brain but it was simpler that I expected.

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

Re: parse text file

#3 Post by Squashman » 06 Jan 2015 18:10

joecepy wrote:found solution. I was racking my brain but it was simpler that I expected.

And your solution was?

joecepy
Posts: 27
Joined: 28 Sep 2012 13:58

Re: parse text file

#4 Post by joecepy » 06 Jan 2015 20:24

Squashman wrote:
joecepy wrote:found solution. I was racking my brain but it was simpler that I expected.

And your solution was?


Code: Select all

@ECHO OFF &SETLOCAL
for /f "delims=" %%a in ('^<20150601_150617.scp.log find "-rw-r--r--"') do call:DOit "%%~a"
goto:Eof

:doit
setlocal
set "string=%~1"
set "STring=%string:*-rw-r--r--=%"
for /f "Tokens=8" %%b in ("%string%") do echo(%%b >> transferFile.log
exit /b

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

Re: parse text file

#5 Post by Squashman » 06 Jan 2015 20:49

You can do this and it will be much much much faster. Using CALL slows down your batch file by a lot. Granted you only have a handful of files but if you were trying to parse a couple hundred lines you will notice a difference in speed.

Code: Select all

@ECHO OFF
for /f "tokens=9 delims= " %%a in ('find "-rw-r--r--" 20150601_150617.scp.log') do >>transferFile.log echo %%~a

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

Re: parse text file

#6 Post by Squashman » 06 Jan 2015 20:52

Slight edit. Just in case your file names actually have spaces.

Code: Select all

@ECHO OFF
for /f "tokens=8* delims= " %%a in ('find "-rw-r--r--" 20150601_150617.scp.log') do >>transferFile.log echo %%~b

Post Reply