Tough 1! Search for specific fields in .txt's

Microsoft Windows xp professional w/serv...
February 11, 2011 at 07:02:57
Specs: Windows XP
All -- So I feel I am well versed in scripting....but not to this extent. Here is what I need:

OUTPUT = RESULTS.CSV file with the header info (HEADER = COL1, COL2, COL3)

FILES = One file type based on the unique name portion of the file but resides in unique dir's for each given site code.

\\SERVER\SITECODE1\UNIQUEFILETYPE\[multiple file names] "CCYYMMDD.SITE.FILETYPE.TXT" (so the CCYYMMDD is the date of the file creation and makes the file unique)

I then need to search for all SITECODEX's with the same

Then once the FOR loop finds all of the files I need to look in the file and pull the following column information for EACH row. Every row is delimited the same with "|" (pipes) and the fields are all the same.


Then I need to pull specific fields into the created RESULTS.CSV


Soooo...I know I can use the following commands:

A) FOR /f "tokens=1-4 delims=\" %%a in ('dir /b/s/ad') DO () ---- which can pull back all the directories for me to parse IF statements through it

B) Then I thought I could use this.... FOR /F "tokens=21 delims=*" %%a IN ('findstr "XXX" %%a.%%b') --- BUT EVERY field has different I am not sure how to pull back the field without giving the command a FINDSTR....

C) I am unsure how I would write to the CSV from there...soooo

I need help!

See More: Tough 1! Search for specific fields in .txts

Report •

February 11, 2011 at 10:34:59
I'm not with you on the file naming but to get columns 1,3 &5 from a | delim file:

:: ==========================================
:: tcerveny.bat Fri 11-02-2011 23:56:37.45
@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=1,3,5 delims=|" %%a in (myfile) do (
>> newfile echo.%%a,%%b,%%c

Life is too important to be taken seriously.


Report •

February 11, 2011 at 11:24:46
WOW--That was it!!

Always OVERTHINKING the easiest!!! Thanks

Report •

February 12, 2011 at 05:29:19
Now, I wanted to say that up until the variable max within the FOR /F statements...I was good with the logic; however just came across an issue where any file that has over 52 columns this of course will not work...and had to go to the Powershell forum.

I feel like I am cheating on you guys here by doing that. :-(

Report •

Related Solutions

February 12, 2011 at 10:20:27
Hi Mechanix2Go,

I dont get it;

"@echo off > newfile"

Why "> newfile"?

Happy is ONE who says I am an OttoMAN.

Report •

February 13, 2011 at 13:56:34
I think this is like a header M2Go put in everyfile to say Hey this batch script is created by M2go.
i too see it in every script.

notice the line >> newfile echo.%%a,%%b,%%c

He is writing the output in newfile without extensionN :).

so my guess is in first line,lets say the header line by echo off >Newfile he is checking several things.

1) the user has write permission in current directory, If not it will error out in first line itself.
2) instead of " if exist newfile delete myfile" @echo off >newfile will make sure the destination file exist

3) It will make sure the destination file is blank as @echo off >newfile will write a blank file .

M2go what you say ?

Subhash Chandra.

Report •

February 13, 2011 at 23:53:11
"3) It will make sure the destination file is blank as @echo off >newfile will write a blank file."

That's what I dont get, because there is no such file as 'newfile' when run this command :/

Happy is ONE who says I am an OttoMAN.

Report •

February 14, 2011 at 00:02:50
if you run @echo off >Newfile on command line it will create a file name "newfile" in the same folder and that file will be a blank text file.
since it creates a new file named "Newfile" its not required to have that file in current directory.

Subhash Chandra.

Report •

Ask Question