How to remove part of filename

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
jvierra
Posts: 13725
Joined: Tue May 22, 2007 9:57 am
Contact:

How to remove part of filename

Post by jvierra » Fri Jun 22, 2007 12:24 am

Check the version of WSH.

W2K came with WSH 5.1. It may not have been upgraded. I don't think SP4 updated WSH.

User avatar
jvierra
Posts: 13725
Joined: Tue May 22, 2007 9:57 am
Contact:

How to remove part of filename

Post by jvierra » Fri Jun 22, 2007 12:37 am

Go to MS download site and downlaoad and install latest WSH pacjkage. I think it's also linked on teh MS scripting site.

oceanhai

How to remove part of filename

Post by oceanhai » Fri Jun 22, 2007 1:24 am

Drat! I reinstalled WSH 5.6 and the script still fails to rename some files (the ones with an underscore in the db name) on this one Win2k machine. Still searching...Here is the script if you are so inclined to try it:Dim strSourceString, strNewString, strRenameCommand, strYear, strMonth, strDay, strDate, strDelCommand, strCopyCommand, strSource, strDest, strDelCommandDone, strCopyCommandDoneSet objFSO = CreateObject("Scripting.FileSystemObject")Set ws = WScript.CreateObject("WScript.Shell")strDelCommand = "cmd /c del E:Restore*.* /Q"strSource = """E:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupServer1Daily*.*"""strDest = "E:Restore /D:"strCopyCommand = "cmd /c xcopy " & strSource & " " & strDestobjStartFolder = "E:Restore"Set objFolder = objFSO.GetFolder(objStartFolder)'---------- Get the current date ----------strYear = DatePart("YYYY",Date)strMonth = DatePart("M",Date)strDay = DatePart("D",Date)strDate = strMonth & "-" & strDay & "-" & strYear'---------- Delete files in restore directory ----------Wscript.Echo "strDelCommand: " & strDelCommandstrDelCommandDone = ws.Run(strDelCommand,1,True)'---------- Copy today's backups from repl directory to E:restore ----------strCopyCommand = strCopyCommand & strDateWscript.Echo "strCopyCommand: " & strCopyCommandstrCopyCommandDone = ws.Run(strCopyCommand,1,True)Set colFiles = objFolder.FilesFor Each objFile in colFiles strSourceString = objFile.Name WScript.Echo "strSourceString: " & strSourceString'---------- Do pattern matching ---------- strPattern="^([a-zA-Z0-9]*)_ww_dddddddd" strNewString=Trim(GetMatch(strSourceString,strPattern)) strNewString=strNewString & ".bak" WScript.Echo "strNewString: " & strNewString strStartFolder = objStartFolder 'Rename Files strRenameCommand = "cmd /c rename " & strStartFolder & "" & strSourceString & " " & strNewString WScript.Echo "strRenameCommand: " & strRenameCommand ws.Run(strRenameCommand)NextFunction GetMatch(strString,strPattern) Dim RegEx,arrMatches Set RegEx = New RegExp RegEx.IgnoreCase = True RegEx.Global=True RegEx.Pattern=strPattern Set colMatches=RegEx.Execute(strString) For Each match In colMatches strResults=strResults & " " & match.value Next GetMatch=strResultsEnd Function

oceanhai

How to remove part of filename

Post by oceanhai » Fri Jun 22, 2007 3:21 am

Here is a snippet of the list. Notice how the filenames for the foo_hd and man_hd_04 databases did not get modified (all others that don't have a "_" in the db name work fine). Thanks!Directory of E:Program FilesMicrosoft SQL ServerMSSQL.1MSSQLBackupServer1Daily (this is the source)06/22/2007 01:00a 731,464 master_db_200706220100.BAK06/22/2007 01:00a 271,678 model_db_200706220100.BAK06/22/2007 01:00a 104,503,138 msdb_db_200706220100.BAK06/22/2007 01:30a 8,050,028 alsfk2_db_200706220130.BAK06/22/2007 01:30a 5,891,392 BPF2006_db_200706220130.BAK06/22/2007 01:31a 2,049,336 pims_db_200706220130.BAK06/22/2007 01:31a 81,029,004 foo_hd_db_200706220130.BAK06/22/2007 01:35a 2,142,036 ftla_db_200706220130.BAK06/22/2007 01:31a 27,163,040 man_hd_04_db_200706220130.BAKDirectory of E:Restore (this is what is in the destination directory after the script runs)06/22/2007 01:31a 49,259,406 .bak06/22/2007 01:30a 8,050,028 alf2k2_db_20070622.bak06/22/2007 01:30a 5,891,392 BPF2006_db_20070622.bak06/22/2007 01:31a 2,049,336 pims_db_20070622.bak06/22/2007 01:31a 81,029,004 foo_hd_db_200706220130.BAK06/22/2007 01:00a 731,464 master_db_20070622.bak06/22/2007 01:00a 271,678 model_db_20070622.bak06/22/2007 01:00a 104,503,138 msdb_db_20070622.bak06/22/2007 01:35a 2,142,036 ftla_db_20070622.bak06/22/2007 01:31a 27,163,040 man_hd_04_db_200706220130.BAK

Locked