PARSE a filename and REN

Microsoft Windows xp professional w/serv...
January 13, 2010 at 19:09:11
Specs: Windows XP
After searching the forum and reading through X amount of other REN's I can't find one even similar. If you know of one after reading this, or can create something outside of a .BAT, then I am ALL EARS!!

I have a DIR ("MyDocs") and within that I have many files that are all named in the same format ("MMDDYYXX") with varying extensions (".ABC", ".TXT", ".MTR")

I need to take the filename, like this example with a mock UNC:
...and parse the date out (first 6), OMITTING the XX (ALWAYS 2 addtl characters follow), then adding the CC (century, in this case 20, for the century 2000; there will be NO 1900 century dates), as well as add NEW STATIC variables to the filename.

These NEW STATIC portions of the filename can either be PROMPTED (SET /P)or HARD CODED into the logic (my current logic below is all HARD CODED/STATIC). Plus, I need to lose the original extension and add a new one (.TXT). The end result would be the following:
HERE is what I have thus any whiz will see...I do not know how to break out the first portion of the RENAME hence the lack of a VARIABLE.


SET pos3=123DD
FOR /F "tokens=1-2 delims=." %%a IN ('dir "S:\MyDocs" /b /a-d') DO (>>parse_REN.BAT ECHO REN %%a.%%b .%pos2%.%pos3%.%pos4%.TXT)

BONUS -- Now, I would LOVE to be able to check inside the FILE first, to see if the file is internally formatted as I expect, and if not, add yet another position in the filename (call it "pos5"). This pos5 would ALWAYS be hard coded to = "PAPER" IF the first line in the file,and the first 4 characters <>, =!, IS NOT(does not equal) "SAS*"

So when that is the case the filename would be as follows...still with the same parts as the first my first section, but with the added position and the word "PAPER" as shown below....

Now, let me first ANYONE who reads this, and takes the challenge on...THANK YOU for just DOING that. Most of us LOVE doing this, or why else would we be here, but I do know I am taking your time for something I NEED...and your time and energy is MUCH appreciated!!!

See More: PARSE a filename and REN

Report •

January 13, 2010 at 20:32:59
try this, no guarantees/not tested (my conf. index is not big rt now)

ECHO PUSHD "S:\MyDocs" >parse_REN.BAT & SETLOCAL enabledeayedexpansion
FOR /F "tokens=1-2 delims=." %%a IN ('dir "S:\MyDocs" /b /a-d') DO (
set xx=%%a
set /a nn=0
set pos5=
for /f "tokens=1 delims=*" %%c in (%%a) do (
if !nn! equ 0 (
if "%%c" equ "SAS" set pos5=.PAPER
set /a nn=1
set xx=20!xx:~0,6!

>>parse_REN.BAT ECHO REN %%a.%%b !xx!.%pos2%.%pos3%.%pos4%%pos5%.TXT)


Report •

January 13, 2010 at 20:55:55
At first try, it did not function, as it only wrote to the secondary .BAT the PUSHD line with nothing further.

I will dig a little bit and see what may be going wrong. The quick first fix is..
enabledeayedexpansion SHOULD BE enabledeLayedexpansion


Thanks for the help, this may put me in the right direction.

Report •

January 13, 2010 at 21:24:58
sorry about that. my fingers becoming spastic, smallmotor control is crapped (re the enabled blah blah).
i think the percents need to be ! in foll.:
>>parse_REN.BAT ECHO REN %%a.%%b !xx!.%pos2%.%pos3%.%pos4%%pos5%.TXT)

change to:
>>parse_REN.BAT ECHO REN %%a.%%b !xx!.%pos2%.%pos3%.%pos4%!pos5!.TXT)

since it's inside the loop... I'm sure there's more problems coming up, awaiting developments. sorry again.

Report •

Related Solutions

January 14, 2010 at 03:49:22 I realized in my original post that I did not state the DATE convert should be..

...and parse the date out (first 6), OMITTING the XX (ALWAYS 2 addtl characters follow), then adding the CC (century, in this case 20, for the century 2000; there will be NO 1900 century dates), as well as add NEW STATIC variables to the filename.

These NEW STATIC portions of the filename can either be PROMPTED (SET /P)or HARD CODED into the logic (my current logic below is all HARD CODED/STATIC). Plus, I need to lose the original extension and add a new one (.TXT). The end result would be the following:

The ORIG file name is MMDDYYXX.* and needs to be CCYYMMDD.*

That was my fat fingering!!

Report •

January 14, 2010 at 06:56:42
no problemo, should just need to change this line:
set xx=20!xx:~0,6!

make it:
set xx=20!xx:~4,2!!xx:~0,4!

you had it right in you o.p, i just didn't pick up on it.
("and the hour was getting late" said the joker to the thief)

ps, i keep forgetting this shortcut, but i saw it in m2's post,
allowing removal of the entire inner for-loop:
for /f "tokens=1 delims=*" %%c in (%%a) do (
if !nn! equ 0 (
if "%%c" equ "SAS" set pos5=.PAPER
set /a nn=1

can be done by:
set xx=
set /p xx=<%%a
if !xx:~0,4! equ "SAS*" set pos5=.PAPER

let me know if problems

Report •

January 14, 2010 at 09:49:12
I believe when I stripped the inner FOR loop I may have missed removing all ()..which I am going through now...but here is what I am getting....the parse_REN.bat is being created with the first line being the PUSHD....then it echo's to the screen original filename minus extension just like this

C:\Documents and Settings\admin\Desktop>(SET xx=12310900 SET /A nn=0 SET pos5=set xx=set /p xx= !
xx:~0,4! EQU "SAS" set pos5=.PAPER set xx=20!xx:~4,2!!xx:~0,4! ECHO REN 12310900.ABC !xx!...!pos5!
.TXT 0<12310900if 1>>parse_REN.BAT )
The system cannot find the file specified.


Report •

January 14, 2010 at 09:51:55
The FOR loop as I believe it to be correct, but obviously is not...

FOR /F "tokens=1-2 delims=." %%a IN ('dir "C:\Documents and Settings\admin\Desktop" /b /a-d') DO (SET xx=%%a SET /A nn=0 SET pos5=set xx=set /p xx=<%%aif !xx:~0,4! EQU "SAS" set pos5=.PAPER set xx=20!xx:~4,2!!xx:~0,4! >>parse_REN.BAT ECHO REN %%a.%%b !xx!.%pos2%.%pos3%.%pos4%!pos5!.TXT)

Report •

January 14, 2010 at 11:27:28
let's try a "clean boot", i think the waters have gotten muddied. things are flying around in a chaotic nimbus!
your for-loop came into my clipboard as all on one line!
and this is purely my fault: xx has gotten used for two entirely different functions that need a separate variable.
I'll just put the script as i tested it:
@echo off & setlocal enabledelayedexpansion
::first, make this NOT an echo, but a statement:
PUSHD "S:\MyDocs"
@echo off>parse_ren.bat
SET pos3=123DD

:: don't need the fullpath since you're in the target directory
FOR /F "tokens=1-2 delims=." %%a IN ('dir /b /a-d') DO (
SET xx=%%a
SET pos5=
:: here's where i messed up by using xx instead of some else
set nn=
set /p nn=<"%%a.%%b"
echo.first line is: !nn!
set nn=!nn:~0,4!
:: if you use 0,4, be sure to include the *, else if you leave off
:: the *, make nn to !nn:~0,3! but i assumed you wanted *
if "!nn!" EQU "SAS*" set pos5=.PAPER
set xx=20!xx:~4,2!!xx:~0,4!
:: be sure not to put dot between pos4 and pos5 as its already there.
>>parse_ren.bat ECHO REN %%a.%%b !xx!.%pos2%.%pos3%.%pos4%!pos5!.TXT
::done... examine the file
more parse_ren.bat
:: call it when you're sure it's right, but i'm leaving it safetied
::call parse_ren.bat

(i left all the echo and pauses in until the thing gets working.)
if you have to run the file from another directory than the target, it will need to be tweaked.
keep me posted

Report •

January 18, 2010 at 04:25:56
Well did it this time around, but you probably did it the time before, and the time before that. I say this because every time I moved the .bat over to the production machine (not development one I am testing on), it failed to worked as described above....ALL BECAUSE I could enabledelayedexpansion!!! for the !VAR!...So I had to go into REGEDIT for the HKEY....add that aspect to the Microsoft Software Command component...and waa laaaaa....that should have been the first thing I checked..and it was the last. THANK YOU VERY MUCH!!!

One small thing, which I can manually fix in the second bat is that it tries to rename the .BAT that was written to the subdir....

REN parse_ren.bat 20e_pars.ABCD.123DD.TEST.PAPER.TXT

Literally as I typed this out...someone then stated that there are duplicate DATES within the file names and now the last two characters(which make it distinct) 'XX' as labeled in original post, needs to be added to the 5th position now (with or without paper)



But this did not work when I added to the final such

set xx=20!xx:~4,2!!xx:~0,4!!xx~6,2!

Report •

January 18, 2010 at 12:28:47
glad it's (almost) working! sounds like its down to "mopping up" stage.
here, you left out colon before the tilde (~) in last item:
set xx=20!xx:~4,2!!xx:~0,4!!xx~6,2!
should be:
set xx=20!xx:~4,2!!xx:~0,4!!xx:~6,2!
but this will give you names format like (XX is 01 is 99):
if you want them the other way (as you described), change
SET pos5=
to: SET pos5=!xx:~6,2!.
(that's a dot at very end, hard to see, easy to miss!)
then where it sets PAPER up, change to:
if "!nn!" EQU "SAS*" set pos5=PAPER!pos5!

hope this works...

Report •

January 18, 2010 at 12:35:32
REN 01060901.RMT 20090106.ABCD.123DD.TESTPAPER01..TXT

I am getting (..) before TXT and no (.) before PAPER...but the other logic is working. I will review to see if I can resolve.

Report •

January 18, 2010 at 12:40:14
SET pos5=!xx:~6,2! -- took out the (.) as it was root cause for an extra one.

if "!nn!" EQU "SAS*" set pos5=.PAPER!pos5! -- added (.) before to keep position 4 and 5 separate. It looks good now. I wont get THAT picky, just need to keep the DUPLICATE files separate, by this two digit.


REN 01070901.ABC 20090107.ABCD.123DD.TEST.PAPER01.TXT

REN 01080800.ABC 20080108.ABCD.123DD.TEST00.TXT

Report •

January 18, 2010 at 14:39:10
So it works...completely and efficiently!!!! You are QUITE THE DEVELOPER!!! And I thank you ..... for ever and a DAY over again!!

So would you like a 4th part to this it was just presented to me...LOL

IF the %%a file name portion in "filename:%%a.%%b" has any ALPHA characters can it be possible to LOOK in the file for the following: ~GS* (only occurs once) and then count 24 positions over to the DATE formatted as CCYYMMDD

~GS*LL*XXX STD TESTS*123456*20050803*

This is because the process AB34567.* cant parse out any date when named like this. And then apply it to the !xx! variable.

Is it also possible to parse the DIR and plug them into the POS*?

<ROOT>\ABCD\123DD\ABCD\TEST and make plug them into the POS2, 3, and 4? To make this FULLY automated windows task?

Thought I would ask....

Report •

January 18, 2010 at 16:21:07
here's the gist of the gnarley part:
for /f tokens=1* delims=:" %%a in ('findstr /b "~GS"') do (
for /f tokens=* delims=0123456789" %%c in ("%%~na") do (
if "%%c" neq "" (
set zz=%%b
set zz=!zz:~28,8!

(your sample data showed the date in position 28, so i used
that, but if it's 24 just change it always remembering that the extraction is based zero) I'm not sure exactly how you mean by "applied to xx", substituted for the one derived in the current batch?
The DIR substitution should not be hard, but need to know where it's coming from (the path): current directory? output from a dir of a certain root?
something like:
for /f "tokens=2,3,4 delims=\" %%m in ('CD') do set uu=%%m.%%n.%%o

I gotta go right now, but I will attempt to help if you can update me and add some specifics.

Report •

January 19, 2010 at 09:35:54
I like a good challenge, so I had a good look at the first post, and tried out my way of coding, since I always end up with totally different coding than the other experts here (you know who you are !!!).Anyway, I had to cheat only once, to get the first line of a file being read (thanks Nbrane). Basically, my approach is always to break down the functions, and I start with the core, in this case a program (BAT/CMD) that will allow one file (one file only) to be renamed. This is what you see below. You can make calls to this by means of a simple FOR loop. It's slower this way, but code ends up being better debuggable. It assumes all is happening in the actual target directory. Use to your likings :

@echo off

SET pos3=123DD
set pos5=

REM ------------------------

set file=%1
if not exist %file% goto error01

set cent=20%file:~4,2%
set month=%file:~0,2%
set day=%file:~2,2%
set add=%file:~6,2%

echo file=%file%
echo cent=%cent%
echo month=%month%
echo day=%day%
echo add=%add%

set /p firstline=<%file%

if "%firstline:~0,4%"=="SAS*" set pos5=.PAPER

echo rename %file% %cent%%month%%day%.%add%.%pos2%.%pos3%.%pos4%%pos5%.txt

goto end

echo File %file% does not exist
goto end


Report •

January 20, 2010 at 04:44:08
NBRANE -- The directory logic wonderfully!!! No changes
needed to that logic!!

TVC-- I must say I like the way you think and I will definitely
put it against my files and see what comes of it. At first
glance it looks good....thanks for your efforts!!

Now for the logic trying to get the ~GS segment noticed and
then count over positions to find a date and place that in the
file name when the original filename does not have a format-
able date name....that is a challenge still.

NBRANE I took the logic for that...and I am not getting it to
function I tried placing it in multiple areas of the other logic,
tried it as a separate CALL .BAT things fail from that point. I
made some slight changes (variable assignments and such),
but still nothing. Am I missing something with the logic?

Report •

January 20, 2010 at 12:11:32
@tvc: I stole the 1-line-file-read trick from M2, can't take credit for that,'re right about calling subs, though, they stressed modularity in computer school and I seem to neglect that sometimes...

ok, here was what i intended more or less, in context, where:

FOR /F "tokens=1-2 delims=." %%a IN ('dir /b /a-d') DO (
SET xx=%%a
:(here, put the alpha test:)
for /f "tokens=* delims=0123456789" %%M in (%%a) do set test=%%M
:(it should work out so test is null if no alphas in name)
ECHO.filename:%%a: alpha?[%test%]
set zz=
:(foll. is all on one line)
if "%test%" neq "" for /f "tokens=* delims=" %%M in ('findstr /b "~GS" %%a.%%b') do set zz=%%M
:(this is the next line)
echo zz is:!zz!
:(substitute the "GS" date zz for xx if it has been determined)
if "!zz!" neq "" set xx=!zz:~28,8!
set pos5...

I assumed that was what you meant by "applying" the date taken from "24 places over" from the ~GS string, but not sure.

I tested the alpha-test thingey on my system and it seemed to work... (i had my doubts, but on limited sample it worked).

in my first post, the "findstr" was incomplete which may have led to the difficulties, my apologies for any extra work that has caused.
Also, if those asterisks in the ~GS are always there in the same format, you could substitute putting "tokens=5 deims=*" into the findstr for/f loop and not do the !zz:~28,8! but just !zz!
try plugging the above into the script, tweak as needed, and see what you get for the ECHO's and if it bombs, you may need to post your entire script as you now have it, i might have misplaced or morphed some pieces in the fray and need to "resynch".

Report •

January 21, 2010 at 19:09:22

I made mods to it, but hot damn, it is a WELL oiled machine with MANY MANY MANY thanks to you.

I would recommend your skills to ANYONE who has asked. I can only imagine the stuff you can come up with.

What is funny....EVERYBODY said this can't be done. I tell them....ANYTHING can be done..and I owe it all to you!!!

Report •

January 21, 2010 at 19:33:06
well, thanks, (i guess, "SICK WORK"? LOL) just kidding. I'm glad it runs now, hope it saves lots of work. good luck...

Report •

January 21, 2010 at 19:46:05
FROM URBAN DICTIONARY . (COM) ** and to be funny**

Pronunciation: 'sik
Function: adjective
Etymology: Middle English sek, sik, from Old English sEoc; akin to Old High German sioh sick
1. Not feeling good.

2 a (1) : affected with disease or ill health : AILING (2) : of, relating to, or intended for use in sickness <sick pay> <a sick ward>


Now used as the newest word to replace cool. Something that you've never seen before.
Old version:

Mary,"I feel sick to my stomach".

Jess,"Ask the teacher for a Nurses' Pass".

New Version
Keegan:This Calculator is sick!

NBRANE:That code is sick!

Report •

January 25, 2010 at 11:16:36

How does this WORK? As I researched (and myself have never used), but cannot understand this logic....(as used above)

SET /p nn=<"%%a.%%b"

Report •

January 25, 2010 at 13:08:24
ok! LL i guess i'm behind on my "urban slang", i'll have to remember to use that one ("sick") and see if other people know what it means.

set /p, like most commands, will accept input from "standard input" which can be redirected from a file, which in this case is %%a.%%b (the quotes are only needed if the filename has a space in it, otherwise they're not part of the syntax).
since set /p only accepts one line (char.s up to 'newline', or car.ret), the rest of the file after line one is just ignored. so this effectively just grabs the first line of the file.
i didn't come up with this, I stole it from M2 i think.

Report •

Ask Question