Computing.Net > Forums > Programming > Renaming files from txt within file

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.

Renaming files from txt within file

Reply to Message Icon

Name: wik
Date: January 13, 2009 at 19:20:41 Pacific
OS: Windows XP Pro 2002
CPU/Ram: 3.2GHz /1GB
Product: Hewlett-packard / Dc7100
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: January 14, 2009 at 03:31:08 Pacific
Reply:

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?


0

Response Number 2
Name: wik
Date: January 14, 2009 at 14:22:28 Pacific
Reply:

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


0

Response Number 3
Name: Judago
Date: January 14, 2009 at 14:48:10 Pacific
Reply:

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.


0

Response Number 4
Name: wik
Date: January 14, 2009 at 20:33:01 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: Renaming files from txt within file

Renaming files from txt within file www.computing.net/answers/programming/renaming-files-from-txt-within-file/19181.html

Read lines from .txt file in DOS? www.computing.net/answers/programming/read-lines-from-txt-file-in-dos/15219.html

renaming files from excel databse www.computing.net/answers/programming/renaming-files-from-excel-databse/17508.html