Renaming files from txt within file

Hewlett-packard / Dc7100
January 13, 2009 at 19:20:41
Specs: Windows XP Pro 2002, 3.2GHz /1GB
Please help!
I want to rename hundreds of files from my local directory by getting a string of text from the file.

For example: Filename: 00401420.lcc

I want to rename it to include the text from within the file. The file contains several lines of text

00401420*KT*SL*SUB1*RD1*0.200
00401420*SL*RT*SUB1*RD1*0.300
00401430*RT*FR*SUB1*RD1*0.500

The first column is the current name of the file. I want to get SUB and RD from the first line in column 4 and 5, and then rename it as the name of the file.
The final filename should look like: RD1-SUB2.lcc

Then proceed with the next file.

The RD1 and SUB2 text is different in each file but in same location (column 4 and 5).
example in the following file they are BUR1 and DE5

00501620*FR*SL*BUR1*DE5*0.500
00501620*SL*BG*BUR1*DE5*0.100
00501630*BG*FR*BUR1*DE5*0.600

The final filename for this one should look like: DE5-BUR1.lcc

Any help on this would be much appreciated! Thanks WIK


See More: Renaming files from txt within file

Report •


#1
January 14, 2009 at 03:31:08
How do you identify columns? Can it be assumed that column 1 is always 8 characters, column 4 always starts in character position 16 and column 5 always in position 21?

Report •

#2
January 14, 2009 at 14:22:28
Thanks for your response.

No the numbers of character change in column 2,3 & 6 in each row in each file.
Column 1 is always 8 characters. The delimiter on the columns is always the * symbol.
Columns 4 and 5 are the same text and characters for each row per file, but change for different files, hence only need to use the information from the first line or each file.

Files can be two rows or 20 rows.

These previous examples probably not so good here are some more.

00706410*SBT*STYI*DEIETNW*WENTYS*0.450
00706420*STY*ERTI*DEIETNW*WENTYS*10.890
00706430*ERTI*BTYUY*DEIETNW*WENTYS*7.589
00706440*BTYUY*APHTREE*DEIETNW*WENTYS*26.562
00000000*BTYUY*APHTREE*DEIETNW*WENTYS*

Existing file name is 00706410.lcc
Needs to be WENTYS-DEIETNW.lcc

00337910*HOSKET*45MB4IN*OSALEYER*MORTOD*0.220
00337920*45MB4IN*PASRH*OSALEYER*MORTOD*1.220
00337930*PASRH*'EN'*OSALEYER*MORTOD*23.22
00337940*'EN'*CULBIRNI*OSALEYER*MORTOD*0.350
00337950*CULBIRNI*'IE'*OSALEYER*MORTOD*1.260
00000000*CULBIRNI*'IE'*OSALEYER*MORTOD*

Existing file name is 00337910.lcc
Needs to be MORTOD-OSALEYER.lcc

NOTE: The last line of the file is a copy of the prevoius with 00000000 in column 1 and nothing in column 6. The * symbols are still there.

Does that make it more difficult?

Hope someone can help me as have been trying for weeks to figure it out?
Thanks again

Wik


Report •

#3
January 14, 2009 at 14:48:10
This should do it:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
pushd drive:\path\yourdir
for /f "delims=" %%a in ('dir *.lcc /b/a-d') do (
set /p line=<"%%~fa"
for /f "tokens=4,5 delims=*" %%b in ("!line!") do echo ren "%%~fa" "%%c-%%b.lcc"
)
pause
popd
endlocal

drive:\path\yourdir need to be the directory holding the files. I have prefixed the actual ren(ame) command with echo so you can check that it is what you before making any changes. When you are sure just remove echo and pause.


Report •

Related Solutions

#4
January 14, 2009 at 20:33:01
Thanks very much Judago. Genius!

Works perfectly!

A litle bit of tweaking to suit some changes to the files and will be perfect for a few other applications too.

Thanks

Very much appreciated

Wik


Report •


Ask Question