Jump to content

I have a set of DAT files that I am wanting to change the extension to part of the filename


G+_Wesley Robinson
 Share

Recommended Posts

The .DAT file is generated from an outside source. The file is a CAD database entry that needs to be changed before the CAD program will be able to use it. Currently I am having to rename them everyday. The initial file is coming in as allam####YYMMDD.DAT where #### are an incremented sequence everyday and the rest is the date. The file name I need is bdailyMM.DD. I have written a couple of batch files that rename the ####YY to bdaily and drop the allam off the front of the file but i cant figure out how to drop the .DAT and move the decimal to where the DD is the extension.

Link to comment
Share on other sites

Easy enough to do in Python:

# Python file rename program

# Rename files from somename123.DAT to somename1.23

 

import os

 

print "Renaming Files...\n",

RenCount = 0

for filename in os.listdir("."):

   if filename.endswith(".DAT"):

      # Remove ".DAT"

      NewName = filename[:-4]

      if len(NewName) >= 3:

         NewName = NewName[:-2] + "." + NewName[-2:]

         os.rename(filename, NewName)

#         print filename, NewName   

         RenCount += 1

 

print "Finished. Files Renamed:",RenCount

Link to comment
Share on other sites

 Share

×
×
  • Create New...