Computing.Net > Forums > Programming > Renaming Files

Renaming Files

Reply to Message Icon

Original Message
Name: Crossroad
Date: June 13, 2007 at 17:55:54 Pacific
Subject: Renaming Files
OS: W2k
CPU/Ram: Pentium 4
Model/Manufacturer: Intel
Comment:

Hi !

I am looking for a batch file or vbs script to rename multiples files under multiples and different subfolders where 4 characters within the filename would end up with only the first 3 of these same characters at the end of the filename.

example;
c:\folder\temp\green1234apple.txt
would endup
c:\folder\temp\greenapple123.txt

Is this feasible ?

Any help appreciated !

Thanks



Report Offensive Message For Removal


Response Number 1
Name: RTAdams89
Date: June 13, 2007 at 19:17:12 Pacific
Subject: Renaming Files
Reply: (edit)

I know BulkRenameUtility will do this, but i'm nto sure if if can be run from a batch file. Might look into it to get some ideas.

-Ryan Adams
http://ryanadams.blogsite.org


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: June 13, 2007 at 19:47:17 Pacific
Subject: Renaming Files
Reply: (edit)

Is the alphabetic part[s] consistent in length and placement?


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 3
Name: Crossroad
Date: June 14, 2007 at 04:44:24 Pacific
Subject: Renaming Files
Reply: (edit)

Hi guys !

Thanks for your replies.

To answer your questions M2Go, the alphabetic lenght is always 28 characters in lenght for the filenames as the 3 characters to move to the end of the filename starts at character 18 of the filename itself.

I don`t know if this help ?

Any help appreciated.

Thanks



Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: June 14, 2007 at 08:01:56 Pacific
Subject: Renaming Files
Reply: (edit)

here's a vbscript. it only renames files in one folder.
[code]
Dim FourChars, NewChars
Set FSO = CreateObject("Scripting.FileSystemObject")
FourChars="1234"
NewChars = Mid(FourChars,1,3)
Set objFolder = FSO.GetFolder("c:\temp")
For Each Files In objFolder.Files
If InStr(1,Files.Name,FourChars) > 1 Then
NewName = RenameFile(Files.Name,FourChars)
WScript.Echo "NewName is ",NewName
Files.Name = NewName
End If
Next
Function RenameFile(TheFile,FourChars)
FileBaseName = FSO.GetBaseName(TheFile)
FileExt = FSO.GetExtensionName(TheFile)
If Len(FileExt) = 0 Then
FileExt = ""
Else
FileExt = "."&FileExt
End If
NewFileName = Replace(FileBaseName,FourChars,"",vbTextCompare)
NewFileName = NewFileName & NewChars & FileExt
RenameFile = NewFileName
End Function
[/code]


Report Offensive Follow Up For Removal

Response Number 5
Name: Crossroad
Date: June 14, 2007 at 19:03:15 Pacific
Subject: Renaming Files
Reply: (edit)

Hi !

Thanks for your help Ghostdog ! Your vbscript work fine.

Now, I'm just trying to figure out how I could make it work if I have files in subfolders of the c:\temp directory.

Any help appreciated.

Thanks


Report Offensive Follow Up For Removal


Response Number 6
Name: Razor2.3
Date: June 14, 2007 at 23:04:39 Pacific
Subject: Renaming Files
Reply: (edit)

Tree walking version of ghostdog's VBScript:
DoStuff "C:\temp"

Sub DoStuff(sDir)
Dim FourChars, NewChars
Set FSO = CreateObject("Scripting.FileSystemObject")
FourChars="1234"
NewChars = Mid(FourChars,1,3)
Set objFolder = FSO.GetFolder(sDir)
For Each Files In objFolder.Files
If InStr(1,Files.Name,FourChars) > 1 Then
NewName = RenameFile(Files.Name,FourChars)
WScript.Echo "NewName is ",NewName
Files.Name = NewName
End If
Next
For Each oDir in objFolder.SubFolders
DoStuff oDir.Path
Next
End Sub

Function RenameFile(TheFile,FourChars)
FileBaseName = FSO.GetBaseName(TheFile)
FileExt = FSO.GetExtensionName(TheFile)
If Len(FileExt) = 0 Then
FileExt = ""
Else
FileExt = "."&FileExt
End If
NewFileName = Replace(FileBaseName,FourChars,"",vbTextCompare)
NewFileName = NewFileName & NewChars & FileExt
RenameFile = NewFileName
End Function


Report Offensive Follow Up For Removal

Response Number 7
Name: ghostdog
Date: June 15, 2007 at 02:11:08 Pacific
Subject: Renaming Files
Reply: (edit)

thanks raz


Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: June 15, 2007 at 03:13:02 Pacific
Subject: Renaming Files
Reply: (edit)

What I meant was the alphabetic [non numeric] part of the file NAME.

In your example: green1234apple, those parts would be green apple.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 9
Name: Crossroad
Date: June 15, 2007 at 20:30:54 Pacific
Subject: Renaming Files
Reply: (edit)

Hi Guys !

Thanks for your replies, if I get this working I'll owe each of you couple of beers, but right now I'm still standing outside the pub !

Razor; I get a vbsrcipt error message at line 22 which is

FileBaseName = FSO.GetBaseName(TheFile)

Error: Object required: FSO

although, I got the same line in from the previous script of ghostdog and it would work great without any script errors , the only thing is that all the files have to be located under that one folder c:\temp, of course it would be gravy if I could get it working if there are any files under any subfolders of c:\temp

M2 ; The filename could be alpha-numeric .
e.g. it looks like this;
43GT557-123-4353-1234-45dtyu.txt
I don't know if this help.

Again, any help appreciated.

Thanks guys !


Report Offensive Follow Up For Removal

Response Number 10
Name: Razor2.3
Date: June 15, 2007 at 20:38:05 Pacific
Subject: Renaming Files
Reply: (edit)

Meh, that script's missing a line before the DoStuff "C:\temp" line.

The first line should read:
Dim FSO

Oh well.


Report Offensive Follow Up For Removal

Response Number 11
Name: Crossroad
Date: June 15, 2007 at 21:51:42 Pacific
Subject: Renaming Files
Reply: (edit)

Hi Razor !

Thanks for your quick response. Your script is now working under each subfolders of the c:\temp folder like I was looking for, Hurrey !!!!

But for some strange reasons, it doesn't move the 4 chars at the end of the filename, it just removes it off the filename.

Again I compared ghostdog's script (as his would move the 4 characters at the end of the filename)to yours and it is exactly the same besides the lines of coding you added to check the subfolders;

For Each oDir in objFolder.SubFolders
DoStuff oDir.Path
Next
End Sub

but it doesn't move them up to the end of the filename,where could this go wrong ?

Thanks



Report Offensive Follow Up For Removal

Response Number 12
Name: Razor2.3
Date: June 15, 2007 at 22:22:33 Pacific
Subject: Renaming Files
Reply: (edit)

Honestly, I don't know. I never even bothered to look at ghostdog's script, outside of the recursive calling (aka tree walking)
Let's see here...

Ah, here's the problem:
NewChars = Mid(FourChars,1,3)
He's setting variables outside of the function he uses them. That's very sloppy coding. No doggie bone for ghost.

Yeah, if you move said line to just after the Function RenameFile(TheFile,FourChars) line, you should be okay.


Report Offensive Follow Up For Removal

Response Number 13
Name: Mechanix2Go
Date: June 15, 2007 at 23:17:23 Pacific
Subject: Renaming Files
Reply: (edit)

"43GT557-123-4353-1234-45dtyu.txt"

should change to what?



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 14
Name: ghostdog
Date: June 16, 2007 at 01:54:13 Pacific
Subject: Renaming Files
Reply: (edit)

well since i thought Raz amended code would work, i never even bothered to look at. No new blade for you Raz..now let's see...oh ok..i have rearranged a bit.

[code]
DoStuff "C:\temp"
Sub DoStuff(sDir)
Dim FourChars, NewChars
Set FSO = CreateObject("Scripting.FileSystemObject")
FourChars="1234"
NewChars = Mid(FourChars,1,3)
Set objFolder = FSO.GetFolder("c:\temp")
For Each Files In objFolder.Files
If InStr(1,Files.Name,FourChars) > 1 Then
NewName = RenameFile(Files.Name,FourChars)
WScript.Echo "NewName is ",NewName
'Files.Name = NewName
End If
Next
For Each oDir in objFolder.SubFolders
DoStuff oDir.Path
Next
End Sub

Function RenameFile(TheFile,FourChars)
FileBaseName = FSO.GetBaseName(TheFile)
FileExt = FSO.GetExtensionName(TheFile)
If Len(FileExt) = 0 Then
FileExt = ""
Else
FileExt = "."&FileExt
End If
NewFileName = Replace(FileBaseName,FourChars,"",vbTextCompare)
NewFileName = NewFileName & NewChars & FileExt

RenameFile = NewFileName
End Function

[/code]

I have not tried, its left to OP to experiment with it. Oh by the way, that NewChars outside function thingy, well its become sloppy after amendment to recursive version, oh well...


Report Offensive Follow Up For Removal

Response Number 15
Name: Mechanix2Go
Date: June 16, 2007 at 02:23:45 Pacific
Subject: Renaming Files
Reply: (edit)

Dontcha hate it when them thingys get sloppy?


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 16
Name: Razor2.3
Date: June 16, 2007 at 02:24:19 Pacific
Subject: Renaming Files
Reply: (edit)

Shoot, it was sloppy beforehand, it just isn't allowed in the recursive version. Really, I have enough to do without making sure you follow proper coding conventions.

Oh, and your revamped code looks exactly like my original modification of your code, plus you added a bug or two.

I swear, what are we going to do with you, ghostdog? Send you to a zombie kennel?


Report Offensive Follow Up For Removal

Response Number 17
Name: ghostdog
Date: June 16, 2007 at 02:48:09 Pacific
Subject: Renaming Files
Reply: (edit)

read my post properly, i said i have not tried and its up to OP to test it. It is just like what you did when you chucked in that recursive function without bothering to check. what can i say...should i sharpen your rusty blade for you. why am i even bothering replying this.


Report Offensive Follow Up For Removal

Response Number 18
Name: Mechanix2Go
Date: June 16, 2007 at 03:22:18 Pacific
Subject: Renaming Files
Reply: (edit)

Hi gd & Raz,

Maybe you can tell me what:

43GT557-123-4353-1234-45dtyu

is trying to become.

TIA


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 19
Name: ghostdog
Date: June 16, 2007 at 03:40:29 Pacific
Subject: Renaming Files
Reply: (edit)

[code]
Dim FourChars, NewChars,FSO
FourChars="1234"
NewChars = Mid(FourChars,1,3)
Set FSO = CreateObject("Scripting.FileSystemObject")
DoWalk "C:\temp"
Sub DoWalk(sDir)
Set objFolder = FSO.GetFolder(sDir)
For Each Files In objFolder.Files
If InStr(1,Files.Name,FourChars) > 1 Then
NewName = RenameFile(Files.Name,FourChars)
WScript.Echo "NewName is ",NewName
'Files.Name = NewName
End If
Next
For Each oDir in objFolder.SubFolders
DoWalk oDir.Path
Next
End Sub

Function RenameFile(TheFile,FourChars)
FileBaseName = FSO.GetBaseName(TheFile)
FileExt = FSO.GetExtensionName(TheFile)
If Len(FileExt) = 0 Then
FileExt = ""
Else
FileExt = "."&FileExt
End If
NewFileName = Replace(FileBaseName,FourChars,"",vbTextCompare)
NewFileName = NewFileName & NewChars & FileExt
RenameFile = NewFileName
End Function


[/code]
hi M2,
from my understanding of OP's initial specs:
output:
[code]
NewName is 43GT557-123-4353--45dtyu123.txt
[/code]
however....i think there's more to it than meets the eye.


Report Offensive Follow Up For Removal

Response Number 20
Name: Mechanix2Go
Date: June 16, 2007 at 03:59:02 Pacific
Subject: Renaming Files
Reply: (edit)

Hi ghostdog,

Yeah, I think a whole lot more.

As ever, a solution starts with a clear problem statement.

Thanks


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 21
Name: FishMonger
Date: June 16, 2007 at 10:54:07 Pacific
Subject: Renaming Files
Reply: (edit)

Here's another possible solution that you might want to consider.

============================================

#!perl

use File::Find;
use File::Basename;

find(\&want, 'C:/temp');

sub want {
    return unless $File::Find::name =~ /\.txt$/;
    ($name, $path, $suffix) = fileparse($File::Find::name, '.txt');
    @parts = split(/-/, $name);
    chop $parts[3];
    $new = $path . join('-', @parts[0,1,2,4,3]) . $suffix;
    rename($File::Find::name, $new);
}


Report Offensive Follow Up For Removal

Response Number 22
Name: Crossroad
Date: June 16, 2007 at 10:54:32 Pacific
Subject: Renaming Files
Reply: (edit)

Hi guys !

Thank you all for your replies, to my stupefaction when I got out of bed few munutes ago (yes, it has been a drunky night) and checked the forums, I could not believed all the replies from you all. I think I have never seen so many replies over one request. I think it is a record. (LOL)

Alright, before a third world war starts over this forum (LOL), Lets's put an end to this one.

Razor;
thank you for all the amendements in the coding, it is now working as a beauty as I was looking for.

Ghostdog;
thanks for the first script to start with and then got modified down the road.

M2;
Thank you for your patience and never managed to get the right filename format from me.Ghostdog was right at the end when he gave you this format : NewName is 43GT557-123-4353--45dtyu123.txt
I don't know if you still want to give a shot at this one, maybe you do for future readers who wants to do it in a batch file. It is up to you !

Anyway, now that the script is working fine and you all have been a great help, let's all meet at McGuire's Pub for few beers and shoot some darts. After all, it hate wars. (LOL)

Thanks guys !!!

Crossroad



Report Offensive Follow Up For Removal

Response Number 23
Name: Mechanix2Go
Date: June 16, 2007 at 11:31:56 Pacific
Subject: Renaming Files
Reply: (edit)

I'll happily pursue it but I'm still not clear on the name layout.

I don't do darts, but I DO beer and dragonladies.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 24
Name: Crossroad
Date: June 16, 2007 at 12:17:55 Pacific
Subject: Renaming Files
Reply: (edit)

Hi M2 !

My script in VB is now working, but since you seem to be a die hard fan of batch file and I hate to let lad like you down then I'll pursue it with you until we got it all figured out.

Here is a sample of the present filename;
43GT557-abc-4353-123-45dtyu5.txt

and the final result filename would look like this;
43GT557-abc-4353--45dtyu5123.txt

The 3 characters at location 18,19, and 20 of the filename would have to move to the end of the filename before the .EXT filename(in this example we have the numbers 123 but it could be any numbers,letters or a combination of both like 1a2).

I hope this will help, now if you want to give your best shot for our present and future readers, be my guest my friend, but as I said, I already have it done and working in a VB script with the help of Ghostdog and Razor. It is up to you M2 !

Thanks

Crossroad !


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Renaming Files

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge