G+_Wesley Robinson Posted July 12, 2014 Share Posted July 12, 2014 I have a set of .DAT files that I am wanting to change the extension to part of the filename. Example: bdaily0710.DAT I am wanting to change it to bdaily07.10 and drop the .DAT. Is this something I can do with python or a batch file that I can automate. Link to comment Share on other sites More sharing options...
G+_Dan Sarauer (N3m1sys) Posted July 12, 2014 Share Posted July 12, 2014 Just a file rename? That could be accomplished with your choice of language, and then scheduled to run at a set interval. Link to comment Share on other sites More sharing options...
G+_Rudy Rodriguez Posted July 12, 2014 Share Posted July 12, 2014 What program created the .DAT data file? If the filename is changed, will the intended program that reads this file still be able to use this file? Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 12, 2014 Author Share Posted July 12, 2014 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 More sharing options...
G+_Darryl Medley Posted July 14, 2014 Share Posted July 14, 2014 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 More sharing options...
G+_Dan Sarauer (N3m1sys) Posted July 14, 2014 Share Posted July 14, 2014 Also if you wanted to use batch files, you could always do the following: for /F %%f in ('dir /b *.dat') do ( rem Drops the .dat extension set fName=%%~nf rem rename output file to bdailyMM.DD rename "%%f" "bdaily%fName:~-4,2%.%fName:~-2% ) Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 Daniel Sarauer I tried this batch file but it is telling me access denied on each one of the files. Any ideas? Link to comment Share on other sites More sharing options...
G+_Dan Sarauer (N3m1sys) Posted July 15, 2014 Share Posted July 15, 2014 Access denied? I'd venture a guess that you may need elevated privileges. Did you try running as administrator? Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 Thanks that did fix the access denied but it seems to be trying to run in the system32 folder, not the directory that it is located in. Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 I got that by starting the file with pushd %~dp0 but now it says that a duplicate file exists of the file cannot be found. Link to comment Share on other sites More sharing options...
G+_Dan Sarauer (N3m1sys) Posted July 15, 2014 Share Posted July 15, 2014 You'll need something to make the files unique then since we took out the unique identifier. Otherwise you'll be left with a bunch of files (if there were multiples for a specified day) named bdaily07.14 for example. Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 Each one that i have has its own date on it. Also there are never multiples on a single day. The files that I have in the dir now are bdaily0707.dat 08 09 10 11 12 and 13. I dont currently have any duplicates Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 It almost looks like it is trying to rename the file the exact same thing as the original. Does the original need to be placed in a temp folder? Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 Darryl Medley I tried running this also but it is giving me an invalid syntax and highlighting the " after \n Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted July 15, 2014 Share Posted July 15, 2014 Wesley Robinson Sorry about that. There is a comma at the end of that line that shouldn't be there. It should just be: print "Renaming Files...\n" For some reason I didn't get an error running under Windows / IDLE. Link to comment Share on other sites More sharing options...
G+_Wesley Robinson Posted July 15, 2014 Author Share Posted July 15, 2014 That's ok. I was able to get it to work by commenting out the two print lines. Thanks everyone for all the help. Link to comment Share on other sites More sharing options...
Recommended Posts