Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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,

Is the naming sty;e consistent?
A few file names may help.
=====================================
If at first you don't succeed, you're about average.M2

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.imgThere is a big similarity here, i.e. the date is always _YYYY-MM-DD_
Thank you very much for your help!

::==
@echo off
setLocal EnableDelayedExpansionfor /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

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,

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

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!

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

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...

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

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 EnableDelayedExpansionfor /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

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.imgThank you again for your time, and responses!
Nick

"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

![]() |
![]() |
![]() |

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