Computing.Net > Forums > Programming > batch script to parse filenames

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

batch script to parse filenames

Reply to Message Icon

Name: nburrell
Date: April 27, 2007 at 09:21:08 Pacific
OS: Windows 2003 Server
CPU/Ram: Pentium Dual Core / 2 Gb
Product: Shuttle
Comment:

Hey everyone, I am very new at this... I hope I can explain what I am looking for. I need to write a batch script. I have a filename, with a date in the filename. I need to extract the date, and save it to a variable. The date is in the form of YYYY-MM-DD. I need to save the date to any variable. I am having no luck. Thank you millions in advance,




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: April 27, 2007 at 10:41:48 Pacific
Reply:

Is the naming sty;e consistent?

A few file names may help.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: nburrell
Date: April 27, 2007 at 11:16:03 Pacific
Reply:

No the names are not consistent, unfortunately, but there are only 2 types.
Example 1: ortho_2006-12-06_test.img
Example 2: ortho_p117r40_2007-06-13_test.img

There is a big similarity here, i.e. the date is always _YYYY-MM-DD_

Thank you very much for your help!


0

Response Number 3
Name: Mechanix2Go
Date: April 27, 2007 at 13:07:23 Pacific
Reply:

::==
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=2,3 delims=_" %%a in ('dir /b *.img') do (
echo %%a|find "200" > nul
if errorlevel 1 (
set myvar=%%b
) else (
set myvar=%%a)
echo myvar is !myvar!
)
::==


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: nburrell
Date: April 27, 2007 at 13:30:14 Pacific
Reply:

Thank you so very much for all of your help! This is looking like it will do the trick, but I am getting an error:

'find' is not recognized as an internal or external command, operable program or batch file...

Any idea?

Thank you again,


0

Response Number 5
Name: Mechanix2Go
Date: April 27, 2007 at 13:46:23 Pacific
Reply:

FIND belongs here:

C:\winnt\system32\find.exe

So either it's missing or your path is goofed up. Do this:

dir/s/b \winnt\find.exe



=====================================
If at first you don't succeed, you're about average.

M2



0

Related Posts

See More



Response Number 6
Name: nburrell
Date: April 27, 2007 at 13:54:12 Pacific
Reply:

on MS 2003 server, It is saying the system cannot find the file specified.

I am trying to find some more info online... Thank you so much for all of your help and patience with a beginner!


0

Response Number 7
Name: Mechanix2Go
Date: April 27, 2007 at 13:59:20 Pacific
Reply:

There may be a copy somewhere else. Try this:

dir/s/b \find.exe

If no go, get one from your CD.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 8
Name: nburrell
Date: April 27, 2007 at 14:00:51 Pacific
Reply:

Found 'find'; this is awesome! Thank you so very much!


0

Response Number 9
Name: nburrell
Date: April 27, 2007 at 14:12:37 Pacific
Reply:

Is there any way this could be expanded to work if there were multiple filename conventions, i.e. the only constant was the YYYY-MM-DD string in the filename? I was just informed there could be numerous types, but they would all have that constant...
Just when I thought I was in the clear...


0

Response Number 10
Name: Mechanix2Go
Date: April 27, 2007 at 14:34:52 Pacific
Reply:

As long as the different 'tokens' are separated by the '_' as in your example names, it can be worked out.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 11
Name: Mechanix2Go
Date: April 28, 2007 at 00:04:18 Pacific
Reply:

This will accomodate up to 5 tokens. You can strecth to fit.

::== par4date.bat
:: get date str into var from filename with _ delim
:: the next section generates files to work with
@echo off
type nul > ortho_2006-12-06_test.img
type nul > ortho_p117r40_2007-06-13_test.img
type nul > 2007-01-03_test.img

@echo off
setLocal EnableDelayedExpansion

for /f "tokens=1-5 delims=_" %%a in ('dir /b *.img') do (
for %%L in (%%a %%b %%c %%d %%e) do (
echo %%L|find "200" > nul & if not errorlevel 1 set myvar=%%L
)
echo myvar is !myvar!
)
::== done



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 12
Name: nburrell
Date: April 28, 2007 at 08:53:32 Pacific
Reply:

This looks amazing! Thank you so much for all of your help, I owe you a big one. Sorry to be so dense, but would I just add new lines under here to accomodate new filename types?
type nul > 2007-01-03_test.img

Thank you again for your time, and responses!
Nick


0

Response Number 13
Name: Mechanix2Go
Date: April 29, 2007 at 01:11:33 Pacific
Reply:

"but would I just add new lines under here to accomodate new filename types?
type nul > 2007-01-03_test.img"

Yes, but thid id just for testing. You don't want to mess with yout real files.


=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch script to parse filenames

batch script to replace filenames h www.computing.net/answers/programming/batch-script-to-replace-filenames-h/16301.html

Batch file to parse log www.computing.net/answers/programming/batch-file-to-parse-log/16414.html

batch script to parse text file www.computing.net/answers/programming/batch-script-to-parse-text-file/16793.html