Rename 11k files with two param's

October 29, 2009 at 08:06:34
Specs: Windows XP
I have over 11k files with the following types of names..

*.835 and *.RMT (usually a mix of numbers or letters precede the extension like 12349FHJ or JK87293)

Inside EVERY file on the first row and at column number 70 I have the following date format....

070815 and need it = 20070815 (the format I need to convert it to).

I need to grab this date, convert it as described above, and then RENAME the associated file with this naming convention and the converted date....

20070815.abcd.1234D.TXT

..where 'abcd.1234D.TXT' is ALWAYS the same for each renamed file, just the newly converted date needs to be added.

I need to do this for each file and was trying to accomplish it using a .bat, but am unsure the best way. If you have another suggestion, I am WIDE OPEN!!

PLEASE HELP IF YOU KNOW HOW!!


See More: Rename 11k files with two params

Report •


#1
October 29, 2009 at 21:24:52
So here is what I'm thinking, first would you be opposed to using VBScript? (If you can run a batch, you can run a VBSCript) Then I'm wondering if you have over 11 thousand files, is your naming convention 20070815.abcd.1234D.TXT going to have a conflict with names?

This is what I'm thinking you could do and tell me if this sounds un-do'able. But when you run the script, for each file in the directory Take the first line and at character 70 add a 20, then rename the file using characters 70-78 and your .abcd.1234D.TXT Is that basically what you are looking at doing?


Report •

#2
October 30, 2009 at 07:01:21
If my above proposal sounds like it will work for you, may I suggest using the following to do it?
All you have to do is change the one variable (objYourFolder) to reflect the name of the folder that you want to test this script in.


Option Explicit

Dim objFSO, colFiles, objFile, Lines
Dim strFilePath, strChngDt, obJYourFolder

objYourFolder = "C:\test\your\folder"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder(objYourFolder).Files
For Each objFile in colFiles
strFilePath = objFSO.GetParentFolderName(objFile) & "\"
lines = Split(objFSO.OpenTextFile(objFile).ReadAll, vbNewLine)
lines(0) = Left(Lines(0),69)&"20"&Right(Lines(0),(Len(Lines(0)))-69)
strChngDt = Mid(Lines(0),70,8)
objFSO.OpenTextFile(objFile, 2).WriteLine Join(lines, vbNewLine)
objFSO.MoveFile objFile, strFilePath & strChngDt & ".abcd.1234D.txt"
Next


Report •

#3
October 30, 2009 at 07:13:58
gtaion....
I appreciate the response with this....

Each date SHOULD only occur once. I have yet to prove this theory though, so an exception MAY need to be written.

With so many files it is hard to find duplication, HOWEVER, it may occur. Your code/logic seems right on point except I cannot change what is in the file. So adding a 20 to col 70 would then negate the file all together.


Report •

Related Solutions

#4
October 30, 2009 at 08:02:56
So I must have mis-understood, You don't want to change the format of the date in the file itself, You just want to use the date contained in the first line in the new file name?


Option Explicit

Dim objFSO, colFiles, objFile, Lines
Dim strFilePath, strChngDt, obJYourFolder

objYourFolder = "C:\Your\folder\path"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder(objYourFolder).Files
For Each objFile in colFiles
strFilePath = objFSO.GetParentFolderName(objFile) & "\"
lines = Split(objFSO.OpenTextFile(objFile).ReadAll, vbNewLine)
strChngDt = "20" & Mid(Lines(0),70,6)
objFSO.MoveFile objFile, strFilePath & strChngDt & ".abcd.1234D.txt"
Next


Report •

#5
October 30, 2009 at 08:29:10
Then if we are on the same sheet of music now, we can add some more logic to this, by saying if the character at position 70 is less then 2, the preceding number is 19 (this allows for dates 1920-2019) then we can also say if the new file name doesnt exist rename the file, otherwise leave it alone.


Option Explicit

Dim objFSO, colFiles, objFile, Lines, strNewName
Dim strFilePath, strChngDt, obJYourFolder

objYourFolder = "C:\Your\folder\path"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder(objYourFolder).Files
For Each objFile in colFiles
strFilePath = objFSO.GetParentFolderName(objFile) & "\"
lines = Split(objFSO.OpenTextFile(objFile).ReadAll, vbNewLine)
If (Mid(Lines(0),70,1)) < 2 then
strChngDt = "20" & Mid(Lines(0),70,6)
else
strChngDt = "19" & Mid(Lines(0),70,6)
End If
strNewName = strFilePath & strChngDt & ".abcd.1234D.txt"
If not (objFSO.FileExists(strNewName)) then
objFSO.MoveFile objFile, strNewName
End If
Next


Report •

#6
October 30, 2009 at 09:29:28
Wow...gtaion...you have exceeded my best expectations. I am going to try this out and let you know the result. You definitely are on the same "sheet of music" with me now..I appreciate this greatly!!! Than you again!

Report •

#7
October 30, 2009 at 10:56:15
WORKED LIKE A CHARM!!

Report •

#8
October 30, 2009 at 12:50:33
What would I need to add if I also need to ALSO pull all characters prior to the first [.] already in the ORIGINAL file name, that we ultimately change?
ie

ORIGINAL FILENAME = 123456XX.835 or X123412Z.RMT

'X123412Z' is needed in the NEWLY created file name of
20070815.abcd.1234D.X123412Z.TXT


Report •

#9
October 30, 2009 at 14:08:21
Filename without the extension is refered to as the "Basename" So if using the basename will work for you we can just add it into the script like so.

But if you actually need to split the filename and just grab the first part we can do that too but it will take a little more to get it done. Like a filename "first.second.third.RMT" and you just want "First" let me know and we can do that too.


Option Explicit

Dim objFSO, colFiles, objFile, Lines, strNewName
Dim strFilePath, strChngDt, obJYourFolder, strBaseName

objYourFolder = "C:\Your\folder\path"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder(objYourFolder).Files
For Each objFile in colFiles
strFilePath = objFSO.GetParentFolderName(objFile) & "\"
strBaseName = objFSO.GetBaseName(objFIle)
lines = Split(objFSO.OpenTextFile(objFile).ReadAll, vbNewLine)
If (Mid(Lines(0),70,1)) < 2 then
strChngDt = "20" & Mid(Lines(0),70,6)
else
strChngDt = "19" & Mid(Lines(0),70,6)
End If
strNewName = strFilePath & strChngDt & ".abcd.1234D."_
& strBaseName &".txt"
If not (objFSO.FileExists(strNewName)) then
objFSO.MoveFile objFile, strNewName
End If
Next


Report •

#10
November 2, 2009 at 07:37:30
THAT WAS AWESOME!!! Thanks for the helpful tips/tricks and WONDERFUL CODE!!! That did a stellar job!!! Strong work!!

Report •

#11
November 2, 2009 at 08:13:12
I ran it in another folder and came back with a problem now. LOL. All of my files end up with

20070815.ABCD.12301D.20070815.ABCD.12301D.BASENAME.TXT
or
20070815.ABCD.12301D.20070815.ABCD.12301D.2070815.ABCD.12301D.BASENAME.TXT

So now I need to rename the invalid renames. And then it gave me an error. Is there a way to write an exception? And now is there an easy way to rename the incorrects? :-(


Report •

#12
November 2, 2009 at 15:45:20
Okay test to see if this will work for you. Instead of using the basename, I changed it to split the filename by using the .'s as the delimiter and store it in an dynamic array named colParts.
Then to set the Basename we use the UBound function on the colParts array, UBound finds the Upper boundery of an array (The last part) which would be the file extension. But we subtract one from the Upper boundary which gives us what I assume is the basename. This way no matter how many times you run the script on the same files, it still checks the date in the file, then it grabs just the name before the extension and names it using the date in the file, the basename and your ABCD stuff. I hope my explanation of what is going on makes sense.
Sorry my replies have slowed down, I went back to work today. Let me know what else we need to do to improve this script for you.


Option Explicit

Dim objFSO, colFiles, objFile, Lines, strNewName, colParts
Dim strFilePath, strEpoch, obJYourFolder, strBaseName

objYourFolder = "C:\Your\folder\path"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder(objYourFolder).Files
For Each objFile in colFiles
colParts = Split(objFile.Name, ".")
strFilePath = objFile.ParentFolder & "\"
strBaseName = colParts(UBound(colParts)-1)
lines = Split(objFSO.OpenTextFile(objFile).ReadAll, vbNewLine)
If (Mid(Lines(0),70,1)) < 2 then
strEpoch = "20" & Mid(Lines(0),70,6)
else
strEpoch = "19" & Mid(Lines(0),70,6)
End If
strNewName = strFilePath & strEpoch & ".abcd.1234D."_
& strBaseName &".txt"
If not (objFSO.FileExists(strNewName)) then
objFSO.MoveFile objFile, strNewName
End If
Next


Report •

#13
November 2, 2009 at 19:39:50
I got to thinking, you said that you got an error. It would be nice to know what it was. I can only think of two reason you would get an error one, the filename above was getting too long, which was fixed by splitting the name instead of using the basename function. Two you have a file in the directory that doesn't have the date in the first line. The following should help you identify any files that don't contain the date in the proper place, I added another If statement that checks where the date is supposed to be, if the date is there, it does what it's supposed to. If it's not there it tells you the name of the file.


Option Explicit

Dim objFSO, colFiles, objFile, Lines, strNewName
Dim strFilePath, strEpoch, obJYourFolder, strOldName

objYourFolder = "C:\Your\folder\path"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder(objYourFolder).Files

For Each objFile in colFiles
strFilePath = objFile.ParentFolder & "\"
strOldName = Split(objFile.Name, ".")(UBound(Split(objFile.Name, "."))-1)
Lines = Split(objFSO.OpenTextFile(objFile).ReadAll, vbNewLine)
If (Mid(Lines(0),70,1)) <> "" then
If (Mid(Lines(0),70,1)) < 2 then
strEpoch = "20" & Mid(Lines(0),70,6)
Else
strEpoch = "19" & Mid(Lines(0),70,6)
End If
strNewName = strFilePath & strEpoch & ".abcd.1234D." & strOldName &".txt"
If not (objFSO.FileExists(strNewName)) then
objFSO.MoveFile objFile, strNewName
End If
Else
Wscript.Echo objFile & " does not contain a date in the first line."
End If
Next


Report •

#14
November 5, 2009 at 09:37:36
The first updated script you sent..WORKED WITHOUT duplication of filename information!!! Again..thanks!!! Something I tried doing...without this help...would have been manual and have taken me 140hrs AT LEAST!!! Thank you a ton!!!

Report •

#15
November 5, 2009 at 14:59:35
I'm glad I could help, I figure scripting is one of those things that you either use or lose. And Lately at work most of my chores are either working on Databases or re-soldering motherboards. Not much helpdesk or program functionality problems. If you find anything else that if added to this script would improve it, let me know and we can get it done.

Report •

#16
November 6, 2009 at 11:26:57
So 12349FHJ.RMT becomes

20070815.abcd.1234D.TXT

is that it?


=====================================
Helping others achieve escape felicity

M2


Report •

#17
November 6, 2009 at 12:20:55
Mechanix2Go yes that is it.

Report •

#18
November 6, 2009 at 13:01:33
What separates the columns?


=====================================
Helping others achieve escape felicity

M2


Report •

#19
November 6, 2009 at 13:53:24
The columns are static positions in the file. Space padded

Report •

#20
November 6, 2009 at 14:48:40
As posted this will preview what's to be done. To activate it remove the ECHO in front of REN.

==============================
@echo off & setLocal EnableDELAYedExpansion

for /f "tokens=* delims= " %%f in ('dir/s/b *.rmt *.835') do (
set N=
for /f "tokens=* delims= " %%a in (%%f) do (
set /a N+=1
if !N! equ 1 call :sub1 %%a
)
echo ren %%f 20!D!.abcd.1234D.TXT
)

goto :eof

:sub1
for /L %%i in (1 1 69) do (shift)
set D=%1
goto :eof


=====================================
Helping others achieve escape felicity

M2


Report •

#21
January 6, 2010 at 08:04:43
How can I swap filename position data...ie

ABCD.20091225.888880D.123456.XXX.PPP
with
20091225.ABCD.888880D.123456.XXX.PPP

and then add another portion to the filename once changed.....ie

20091225.ABCD. 888880D.123456.XXX.111.PPP

This would NOT need to include parsing anything out from the file.


Report •

#22
January 6, 2010 at 09:02:21
You may be unsurprised to hear I haven't thought much about this in the two months it's been marinating.

I suggest you start a new thread.


=====================================
Helping others achieve escape felicity

M2


Report •

#23
January 6, 2010 at 10:00:34
M2...thank you and agreed.....

http://www.computing.net/answers/pr...

REGARDS!


Report •

#24
January 22, 2010 at 08:18:22
Do all or your files now contain names in the format:
ABCD.20091225.888880D.123456.XXX.PPP


I guess what I'm trying to say, is this is pretty simple if all the files in the directory need to be renamed the other way. We could simply split the filename in to parts using the "."'s as delimiters, like we did above and store them in an array. Then we can tell it to rewrite the file name with the format that you want like. part2.part1.part3.part4.part5.Newfield.part6 I hope this makes sense.


Report •

Ask Question