'Removes bracketed text (like [1]) from 'all file names in a specified directory 'http://www.ericphelps.com Option Explicit Main Sub Main() Dim fil 'As Scripting.File Dim fils 'As Scripting.Files Dim fol 'As Scripting.Folder Dim fols 'As Scripting.Folders Dim fs 'As Scripting.FileSystemObject Dim strDirectories 'As String Dim lngCounter 'As Long Dim strOldText 'As String Dim strNewText 'As String Const READONLY = 1 Const HIDDEN = 2 Const SYSTEM = 4 If MsgBox ("This program will remove all bracketed text (like ""[1]"") from file names in a directory you designate. Continue?", vbOkCancel) = vbCancel Then Wscript.Quit Set fs = CreateObject("Scripting.FileSystemObject") If Lcase(Right(Wscript.FullName, 11)) = "wscript.exe" Then If MsgBox ("This program is being run under WSCRIPT. Results will be stored in a log at " & Wscript.ScriptFullName & ".log" & ". Run this program under CSCRIPT if you want a real-time display of activity. Otherwise, a message box will pop up to inform you when the program finishes. Continue?", vbOkCancel, "No Square Brackets") = vbCancel Then Wscript.Quit Else If MsgBox ("This program is being run under CSCRIPT. Results will be displayed on-screen and will not be logged. Run this program under WSCRIPT if you want a log file. Continue?", vbOkCancel, "No Square Brackets") = vbCancel Then Wscript.Quit End If If WScript.Arguments.Count = 1 Then strDirectories = WScript.Arguments(0) Else strDirectories = FileNameInThisDir("") End If strDirectories = fs.GetAbsolutePathName(InputBox("Enter path to remove bracketed text from file names:", "No Square Brackets", strDirectories)) If strDirectories = "" Then Wscript.Quit Status "*************************************" Status "*************************************" Status "Program: " & Wscript.ScriptFullName Status "Removing brackets in: " & strDirectories Status "Starting Time: " & Now Set fol = fs.GetFolder(strDirectories) 'Get each file in turn Set fils = fol.Files If Err.Number <> 0 Then Exit Sub For Each fil In fils If (Instr(fil.Name, "[") <> 0) Then Status fil.Path If ((fil.Attributes And READONLY) = 0) Then If ((fil.Attributes And SYSTEM) = 0) Then If ((fil.Attributes And HIDDEN) = 0) Then Process fil Else Status " HIDDEN" End If Else Status " SYSTEM" End If Else Status " READONLY" End If End If Next Status "Program finished: " & Now Status "*************************************" Status "*************************************" If Lcase(Right(Wscript.FullName, 11)) = "wscript.exe" Then MsgBox "Program Finished! Details are in the log at " & Wscript.ScriptFullName & ".log", vbOkOnly, "No Spaces" End Sub Function FileNameInThisDir(strFileName) 'As String 'Returns the complete path and file name to a file in 'the script directory. For example, "trans.log" might 'return "C:\Program Files\Scripts\Database\trans.log" 'if the script was in the "C:\Program Files\Scripts\Database" 'directory. Dim fs 'As Scripting.FileSystemObject Set fs = CreateObject("Scripting.FileSystemObject") FileNameInThisDir = fs.GetAbsolutePathName(fs.BuildPath(Wscript.ScriptFullName, "..\" & strFileName)) ''''''''''Clean up Set fs = Nothing End Function Sub Status (strMessage) 'If the program was run with CSCRIPT, this writes a 'line into the DOS box. If run with WSCRIPT, it writes 'to a log in the same directory as the script. Dim ts 'As Scripting.TextStream Dim fs 'As Scripting.FileSystemObject Const ForAppending = 8 'Scripting.IOMode If Lcase(Right(Wscript.FullName, 12)) = "\cscript.exe" Then Wscript.Echo strMessage Else Set fs = CreateObject("Scripting.FileSystemObject") Set ts = fs.OpenTextFile(Wscript.ScriptFullName & ".log", ForAppending, True) ts.WriteLine strMessage ts.Close ''''''''''Clean up Set ts = Nothing Set fs = Nothing End If End Sub Sub Process(objScriptingFile) Dim strFileName 'As String strFileName = objScriptingFile.Name 'Protect script and it's log from being modified If Lcase(objScriptingFile.Path) = Lcase(WScript.ScriptFullName) Then Exit Sub If Lcase(Wscript.ScriptFullName & ".log") = Lcase(WScript.ScriptFullName) Then Exit Sub 'Make sure brackets exist ans are in the proper order If ((Instr(strFileName, "[") = 0) Or (Instr(strFileName, "]") = 0)) Then Exit Sub If Instr(strFileName, "[") > Instr(strFileName, "]") Then Exit Sub 'Remove all the bracketed text Do While Instr(strFileName, "[") <> 0 strFileName = Left(strFileName, Instr(strFileName, "[") - 1) & Mid(strFileName, Instr(strFileName, "]") + 1) Loop 'Rename the file by bouncing it through a temporary name objScriptingFile.Name = "~RenamedFile.tmp" objScriptingFile.Name = strFileName Status " >" & objScriptingFile.Name End Sub