1.

Solve : Using the FOR command?

Answer»

Hello,

Looking to create a batch file to take a file and PARSE out a CERTAIN value. The files all have a similiar pattern like this...

00000,,\6442\Vol002\001\00000.tif,Y,,,0
000001,,\6442\Vol003\001\002\000001.tif,Y,,,0
000002,,\6442\Vol003\001\000002.TIF,Y,,,0
000003,,\6442\Vol003\005\004\000003.TIF,,,,

What I need is to be able to output to a text file the file name only. For example "00000.tif" for the first line of the example above.

The path of the filename are not the same and may have less or more subfolders than others, but everything else is the same.

How can I output a text file with only the file names using DOS.

Would the FOR command be useful? If so, can someone assist with a script?

Thanks guys.

00000,,\6442\Vol002\001\00000.tif,Y,,,0

is each "path" always preceded by some digits and 2 commas, and always followed by one (or more?) comma(s?)

The FOR command, and some variable modifiers, might be useful, once the above question is answered.


Yes, it can be done with the FOR command.
However, parsing strings is much easier in a programming language that does that sort of thing. It can be do in C, C++ Pascal, Java, Pearl, Ruby. And more.

There is an old program called QBASIC that is on the Windows 98 disc, I believe.
You could run it inside a batch file and it would read the file and spit out the names found between the lat "\" and the first ", " perhaps with just ten lines of code more or less.

But if you really want to us the FOR, go ahead. It gives me a headace.Quote

with just ten lines of code more or less.

How about one?

command does "that sort of thing" very well. It was designed for it. It LOVES chewing up csv files.

Took me 7 minutes. And 5 of those I was MAKING coffee.

Presuming the answer to my question above is "yes"...

Code: [Select]@echo off
set inputfile=input.txt
set outputfile=output.txt
if exist "%outputfile%" del "%outputfile%"
for /f "tokens=1-2* delims=," %%A in ( ' type "%inputfile%" ' ) do echo %%~nxB>>"%outputfile%"

input.txt:

Code: [Select]00000,,\6442\Vol002\001\00000.tif,Y,,,0
000001,,\6442\Vol003\001\002\000001.tif,Y,,,0
000002,,\6442\Vol003\001\000002.TIF,Y,,,0
000003,,\6442\Vol003\005\004\000003.TIF,,,,
output.txt:

Code: [Select]00000.tif
000001.tif
000002.TIF
000003.TIF


AAwwkk !
I have a headache!
It took your 7 Mins.

It will take me 7 days to try and understand that!
for /f

TREAT the dataset (what is in the brackets) as a series of one or more lines

"tokens=1-2* delims=,"

Treat each such line as being split (by commas) into at least 2 tokens (chunks)

%%A

Assign the first chunk of each line to the explicit loop variable %%A. The second chunk will therefore be the implicit loop variable %%B. The asterisk in the tokens/delims block means that all of the rest of the line (if there is any) will be the implicit loop variable %%C.

in ( ' type "%inputfile%" ' )

The dataset is the output of 'type inputfile'.

do echo %%~nxB

Take the second chunk of the line, (a path and filename), and extract just the file name and extension, and echo it...

>>"%outputfile%"

... to the output file

and repeat until no more lines left.
Quote from: Geek-9pm on February 02, 2009, 03:27:52 PM


If it's any help, that's what Perl does to me.
Dias DE verano,

Yes you are right on your first assumption. I will put it to the test on a very large file that I have. I very much appreciate your speedy reply to my post today.

thatguy.It works great. Sorry for the confusion. Thanks so much!


Discussion

No Comment Found