Computing.Net > Forums > Programming > Rename 11k files with two param's

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.

Rename 11k files with two param's

Reply to Message Icon

Name: tcerveny
Date: October 29, 2009 at 08:06:34 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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!!



Sponsored Link
Ads by Google

Response Number 1
Name: gtaion
Date: October 29, 2009 at 21:24:52 Pacific
Reply:

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?


0

Response Number 2
Name: gtaion
Date: October 30, 2009 at 07:01:21 Pacific
Reply:

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


0

Response Number 3
Name: tcerveny
Date: October 30, 2009 at 07:13:58 Pacific
Reply:

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.


0

Response Number 4
Name: gtaion
Date: October 30, 2009 at 08:02:56 Pacific
Reply:

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


0

Response Number 5
Name: gtaion
Date: October 30, 2009 at 08:29:10 Pacific
Reply:

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


2

Related Posts

See More



Response Number 6
Name: tcerveny
Date: October 30, 2009 at 09:29:28 Pacific
Reply:

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!


0

Response Number 7
Name: tcerveny
Date: October 30, 2009 at 10:56:15 Pacific
Reply:

WORKED LIKE A CHARM!!


0

Response Number 8
Name: tcerveny
Date: October 30, 2009 at 12:50:33 Pacific
Reply:

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


0

Response Number 9
Name: gtaion
Date: October 30, 2009 at 14:08:21 Pacific
Reply:

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


1

Response Number 10
Name: tcerveny
Date: November 2, 2009 at 07:37:30 Pacific
Reply:

THAT WAS AWESOME!!! Thanks for the helpful tips/tricks and WONDERFUL CODE!!! That did a stellar job!!! Strong work!!


0

Response Number 11
Name: tcerveny
Date: November 2, 2009 at 08:13:12 Pacific
Reply:

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? :-(


0

Response Number 12
Name: gtaion
Date: November 2, 2009 at 15:45:20 Pacific
Reply:

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


0

Response Number 13
Name: gtaion
Date: November 2, 2009 at 19:39:50 Pacific
Reply:

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


1

Response Number 14
Name: tcerveny
Date: November 5, 2009 at 09:37:36 Pacific
Reply:

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!!!


0

Response Number 15
Name: gtaion
Date: November 5, 2009 at 14:59:35 Pacific
Reply:

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.


0

Response Number 16
Name: Mechanix2Go
Date: November 6, 2009 at 11:26:57 Pacific
Reply:

So 12349FHJ.RMT becomes

20070815.abcd.1234D.TXT

is that it?


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

M2


0

Response Number 17
Name: tcerveny
Date: November 6, 2009 at 12:20:55 Pacific
Reply:

Mechanix2Go yes that is it.


0

Response Number 18
Name: Mechanix2Go
Date: November 6, 2009 at 13:01:33 Pacific
Reply:

What separates the columns?


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

M2


0

Response Number 19
Name: tcerveny
Date: November 6, 2009 at 13:53:24 Pacific
Reply:

The columns are static positions in the file. Space padded


0

Response Number 20
Name: Mechanix2Go
Date: November 6, 2009 at 14:48:40 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Rename 11k files with two param's

Rename *.TXT file with 24 Hour Time Stamp www.computing.net/answers/programming/rename-txt-file-with-24-hour-time-stamp/19772.html

Rename a file with time www.computing.net/answers/programming/rename-a-file-with-time/11595.html

batch rename files with a part of.. www.computing.net/answers/programming/batch-rename-files-with-a-part-of/16747.html