How to delete Multiple Spaces in filenames using RegEx?
Also, how to remove spaces from beginning and end of filenames?
Thanks!
How to delete Multiple Spaces in filenames using RegEx?
Also, how to remove spaces from beginning and end of filenames?
Thanks!
Remove spaces from the beginning
old: ^ (.*)#
new: \1
Remove spaces from the end
old: (.*) .([^.]+)#
new: \1.\2
And you can use this extract of the Steve's script [Titlecase) to remove multispaces and strip leading/trailing spaces.
[code]@script vbscript
option explicit
dim fMultiSpace
dim fStripSpaces
fMultiSpace = True ' Remove multiple spaces
fStripSpaces = True ' Strip leading/trailing spaces
Function Rename_GetNewName ( strFileName, strFullPath, _
fIsFolder, strOldName, ByRef strNewName )
dim strExt
dim iOldLength
dim iNewLength
'Strip the filename extension for now
if fIsFolder = False then
if InStr(strNewName, ".") then
strExt = Right(strNewName, Len(strNewName)-(InStrRev(strNewName, ".")-1))
strNewName = Left(strNewName, InStrRev(strNewName, ".")-1)
else
strExt = ""
end if
end if
' Remove all multiple spaces
if fMultiSpace = True then
iNewLength = 0
iOldLength = len(strNewName)
Do While iOldLength > iNewLength
iOldLength = len(strNewName)
strNewName = Replace(strNewName," "," ")
iNewLength = len(strNewName)
Loop
end if
' Strip leading/trailing spaces
if fStripSpaces = True then
strExt = Trim(strExt)
strNewName = Trim(strNewName)
end if
' Rejoin the name and extension and we're finished
strNewName = strNewName & strExt
End Function[/code]
And try my Rename Tollbar (see signature)
You don't need a complex script to remove all spaces from filenames.
The easiest way to do it is to use find and replace mode, finding a space and replacing it with nothing.
Or, if you want to use a regular expression instead for some reason, you can use this:
Old: b (.*)#[/b]
New: \1\2
Thanks for your replies!
Yes, I would prefer a simple rename command, instead of complex scripts
[quote="leo"]The easiest way to do it is to use find and replace mode, finding a space and replacing it with nothing.
Or, if you want to use a regular expression instead for some reason, you can use this:
Old: b (.*)#[/b]
New: \1\2[/quote]
This method removes all the spaces... Is there a way to reduce MULTIPLE spaces to a SINGLE space?
Sure:
Old: (.*) (.*)#
New: \1 \2
There are two spaces between the pair of (.*) but only one space between the \1 and \2.
VBscript Regular Expression
Remove multispaces
re.Global = true
re.Pattern = " {2,}" 'mean search and replace 2 or more spaces
strNameOnly = re.Replace(strNameOnly, " ")
Strip leading spaces
re.Global = true
re.Pattern = "^ "
strNameOnly = re.Replace(strNameOnly, "")
Strip trailing spaces
re.Global = true
re.Pattern = " $"
strNameOnly = re.Replace(strNameOnly, "")
Thanks!