Listing 1: CleanShortcutName.vbs Option Explicit ' ******* CALLOUT A ******* Dim ShortcutMarkers ShortcutMarkers = Array("Shortcut to ", " - Shortcut") ' ******* END CALLOUT A ******* ' ******* BEGIN CALLOUT B ******* Dim fso: Set fso = CreateObject("Scripting.FileSystemObject") Dim sh: Set sh = CreateObject("WScript.Shell") Dim rx: Set rx = New RegExp rx.IgnoreCase = True ' ******* END CALLOUT B ******* ' ******* BEGIN CALLOUT C ******* Dim arg For Each arg in WScript.Arguments.UnNamed PrettyShortcutName arg Next ' ******* END CALLOUT C ******* ' ******* BEGIN CALLOUT D ******* if UseInputStreamNotArguments Then Do While Not WScript.StdIn.AtEndofStream Dim name: name = WScript.Stdin.ReadLine If(Not PrettyShortcutName(name)) Then WScript.StdErr.WriteLine name End If Loop End If Function UseInputStreamNotArguments UseInputStreamNotArguments = false if WScript.Arguments.UnNamed.Count = 0 Then if LCase(fso.GetBaseName(WScript.FullName)) _ = "cscript" Then UseInputStreamNotArguments = true Else WScript.Echo "To clean shortcut names, drop them onto the script file." End If End If End Function ' ******* END CALLOUT D ******* Function PrettyShortcutName(ByVal shortcutname) PrettyShortcutName = False ' ******* BEGIN CALLOUT E if fso.FileExists(shortcutname) then ' We found a file if LCase(fso.GetExtensionName(shortcutname)) = "lnk" Then ' ******* END CALLOUT E ******* ' It IS a shortcut. ' ******* BEGIN CALLOUT F ******* Dim scFile: Set scFile = fso.GetFile(shortcutname) Dim NewName: NewName = scFile.name Dim marker For Each marker in ShortcutMarkers NewName = Replace(NewName, marker, vbNullString) Next ' ******* END CALLOUT F ******* ' ******* BEGIN CALLOUT G ******* On Error Resume Next ' ******* END CALLOUT G ******* ' ******* BEGIN CALLOUT H ******* Dim targetpath: targetpath = sh.CreateShortcut(shortcutname).targetpath If Err.Number = 0 Then ' We found the shortcut's target Dim targetExtension: targetExtension = fso.GetExtensionName(targetpath) If Err.Number = 0 Then ' The shortcut is a real file (not a folder or virtual object) ' Using a regex to simplify case handling; could be done other ways as well. ' Using a regex also allows us to remove any terminal spaces that might be ' in the base name. rx.Pattern = " *\." & targetExtension & "\.lnk$" if rx.Test(NewName) Then NewName = rx.Replace(NewName, ".lnk") End If Else ' An error encountered here would just mean the target isn't a file with an extension. ' We clear the error since it's ok. Err.Clear End If End If ' ******* END CALLOUT H ******* ' ******* BEGIN CALLOUT I ******* ' This line will generate errors if the file already exists. scFile.Name = NewName ' ******* END CALLOUT I ******* If Err.Number = 0 Then PrettyShortcutName = True On Error Goto 0 ' We changed the name successfully; return true End If End If End Function