Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename the Nth character in a file name

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Jan 26, 2006
392
US
Hi...

I have a list of files in the format:

123-1-0001.tif
123-1-0002.tif
123-1-0003.tif
124-4-0001.tif
124-4-0002.tif

I need to rename the FIRST dash (4th character)to an underscore like this:
123_1-0001.tif
123_1-0002.tif
123_1-0003.tif
124_4-0001.tif
124_4-0002.tif

I have been using the following script to repalace any dash with an underscore, but not sure how to modify so only the first dash (4th character) is renamed, not the additional dash (6th character)

Code:
sPath = "."
set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set FCol = oFolder.Files
For Each File In FCol
strFileName = File
strNewFile = Replace(strFileName,"-","_")
oFSO.Movefile strFileName, strNewFile
Next

Anyone have a suggestion for me?

Thanks!

Brian
 




Hi,


How about Name Oldfile as Newfile

Skip,

[glasses] To be safe on the [red]FOURTH[/red],
Don't take a [red]FIFTH[/red] on the [red]THIRD[/red]
Or you might not come [red]FORTH[/red] on the [red]FIFTH[/red]
[red][highlight blue]FORTH[/highlight][/red][white][highlight red]WITH[/highlight][/white] [tongue]
 
Thanks for the quick reply. I have about 25,000 files to rename. I also have about another 500 per day coming in. So the rename option will not work.
 
Furthermore, Name Oldfile as Newfile is VBA, not VBS.

Use this:
strNewFile = Replace(strFileName,"-","_"[!],1,1[/!])

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV beat me to it, I was looking it up for a while.
 
use the count pramater
change
Code:
strNewFile = Replace(strFileName,"-","_")

with
Code:
strNewFile = Replace(strFileName,"-","_"[COLOR=red][b],,1[/b] [/color])
 



Sorry...
Code:
strNewFile = mid(strFileName,1,3) & "_" & mid(strFileName,5,Len(strFileName)-4)

Skip,

[glasses] To be safe on the [red]FOURTH[/red],
Don't take a [red]FIFTH[/red] on the [red]THIRD[/red]
Or you might not come [red]FORTH[/red] on the [red]FIFTH[/red]
[red][highlight blue]FORTH[/highlight][/red][white][highlight red]WITH[/highlight][/white] [tongue]
 
Thank you for all the replies. I tried pwise's option, but getting the error:
Line: 9
Char: 1
Error: Expected Statement
Code: 800A0400
Source: Microsoft VBScript Compilation Error

Here is my code so far:
Code:
sPath = "."
set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set FCol = oFolder.Files
For Each File In FCol
strFileName = File
strNewFile = mid(strFileName,1,3) & "_" & mid
(strfilename,5,Len(strfilename)-4)
oFSO.Movefile strFileName, strNewFile
next

Am I doing something wrong?
 
Why not use
Code:
strNewFile = Replace(strFileName,"-","_",1,1)

Its a simpler line
 
I tried

strNewFile = Replace(strFileName,"-","_",1,1)

before, but for some reason it replaced BOTH dashes. I just tried it again though and it worked perfectly.

Thanks!

 
I recently renamed a lot of images using something similar to this.

Code:
Option Explicit

Dim objShell : Set objShell = CreateObject("Shell.Application")
Dim objFolder : Set objFolder = objShell.NameSpace("C:\temp\images")

Dim objItem
For Each objItem In objFolder.Items
	objItem.Name = Replace(objItem.Name, "-", "_", 1, 1)
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top