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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regex replace characters

Status
Not open for further replies.

Dalie

Programmer
Feb 16, 2009
17
NL
Hi again,
Hopefully one of my last questions ;) I really start enjoying to solve problems these day's and I learned a lot so far :D I found this simple script that replaces bad characters.
Code:
WScript.Echo RenFile("bad%file&name.txt")  
  
Function RenFile(FileName)    
With New RegExp  

  .Pattern = "[/\\:*?""<>|#{}%&~]"  
  .Global = True  
  RenFile = .Replace(FileName, "_")  

End With  
End Function
The only problem I got is I'd like to apply it to filenames something like:
Code:
...
For Each objFile In colFileList
   RenFile(objFile.FileName)
Next
...
How come it doesn't execute the replace function when I want to apply the function to filenames? Where do I go wrong?
 
Are you using WScript.Echo to show you the renamed value? Do you want it to rename the file, because simply calling the function won't rename it for you...it simply reformats the text?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
dm4ever thanks for you quick reaction, indeed the wscript.echo just shows the renamed value (and doesn't rename) but that was just show what I would like to apply to rename files. I'm trying to figure out how to apply the function on renaming files. My first idea was something like this:
Code:
For Each objFile In colFileList
    strNewName = objFile.Drive & objFile.Path & [b]RenFile(objFile.FileName)[/b] & "." & objFile.Extension
    theResult = objFile.Rename(strNewName)
Next
Where the bold part in the code above calls the function from my openings post. I know this won't work this way but hopefully the idea is better explained. Maybe someone can point me in the right direction where to look or what I need to change...
 
Well it works =) any improvements/suggestions are welcome! (As said in previous posts I am quite new to VBScript, any feedback is welcome). The next objective will be (after replacement) to prevent repeated characters.... hint hint :p ;)
Code:
Set RegularExpressionObject = New RegExp
...
For Each objFile In colFileList
    strNewName = objFile.Drive & objFile.Path & repName(objFile.FileName) & "." & objFile.Extension
    errResult = objFile.Rename(strNewName)
Next

Function repName(strIn)
			With RegularExpressionObject
				.Pattern = "[/\\:*?""<>|#{}%&~.]"
				.IgnoreCase = True
				.Global = True
			End With
	ReplacedString = RegularExpressionObject.Replace(strIn, "_")
	repName = ReplacedString
End Function
 
Well, now it's time to get some sleep.. I didn't figure out if I need the last regex, I just want to prevent any "special" character at the beginning or end of the filename (i.e. test.file..txt). Any suggestions are still welcome (to make the code shorter or something like that..)
Code:
		With RegularExpressionObject
				.Pattern = "\.{2,}"
  ReplacedString = Trim(RegularExpressionObject.Replace(ReplacedString, "."))	
		End With

'		With RegularExpressionObject				
'				.Pattern = "\s(\!|\.|\?|\;|\,|\:)"
'  ReplacedString = RegularExpressionObject.Replace(ReplacedString, "$1")
'		End With
 
If you're looping through the files using FSO then you can rename the file using the name property or by using the MoveFile method.

...your code would be close to this

Code:
Set RegEx = New RegExp
RegEx.Pattern = "[/\\:*?""<>|#{}%&~]" 
RegEx.Global = True

...code

For Each objFile In objFolder.Files
	 If RegEx.Test(objFile.Name) Then
	 	objFile.Name = RegEx.Replace(objFile.Name, "")
	 End If
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