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: arvindr72
Date: May 27, 2009 at 03:22:51 Pacific
OS: Windows XP
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: tsa012835182.txt
I want to rename it to include the text from within the file. The file contains several lines of text
15-05-09 003094 0610
15-05-09 003094 0715
15-05-09 003094 1715
15-05-09 003094 1807
I want to get 15-05-09 the first line in column 1, and then rename it as the name of the file.
The final filename should look like: TSA01-14-05-09.txt
Then proceed with the next file.
The 15-05-09 text is different in each file but in same location (column 1).

The final filename for this one should look like: tsa0115-05-09.txt



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 27, 2009 at 04:38:15 Pacific
Reply:

if you have Python on windows

import glob,os
os.chdir(os.path.join("C:\\","test"))
for files in glob.glob("tsa*txt"):
    try:
        f=open(files)
    except Exception,e:
        print e
    else:
        filename = f.readline().strip().split()[0]
        f.close()
        newfilename = files[:5]+filename+".txt"
        try:
            os.rename(files,newfilename)
        except Exception,e:
            print e

save the above as myscript.py and on command line
c:\test> python myscript.py



0

Response Number 2
Name: arvindr72
Date: May 27, 2009 at 04:54:58 Pacific
Reply:

Sorry I don't have python installed.

I have tried to run a script which was there for a similar requirement on the forum, but could not getthe same.

The difference being, my txt file is tab delimited and the file has to be named useing the 1 column and not the 4th and the 5th.


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