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.
This topic is 16 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
oceanhai

How to remove part of filename

Post by oceanhai »

I want to do something really simple but I need someone to point me in the right direction. I'd like to remove the 4 characters after the time stamp and before the "." in a filename for all files in a given directory. Here is an example:Rename these filesdb1_db_200706051234.bakdb2_db_200706051234.bakdb3_db_200706051234.bakdb4_db_200706051234.bakTo thisdb1_db_20070605.bakdb2_db_20070605.bakdb3_db_20070605.bakdb4_db_20070605.bakShould I use a regular expression? A wildcard? Thanks for any help you can offer.Ryan
oceanhai

How to remove part of filename

Post by oceanhai »

I want to do something really simple but I need someone to point me in the right direction. I'd like to remove the 4 characters after the time stamp and before the "." in a filename for all files in a given directory. Here is an example:Rename these filesdb1_db_200706051234.bakdb2_db_200706051234.bakdb3_db_200706051234.bakdb4_db_200706051234.bakTo thisdb1_db_20070605.bakdb2_db_20070605.bakdb3_db_20070605.bakdb4_db_20070605.bakShould I use a regular expression? A wildcard? Thanks for any help you can offer.Ryan
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

How to remove part of filename

Post by jhicks »

One way might be to use the Left function on the filename to retrieve the first 14 characters:sOldfile=db1_db_200706051234.baksNewFile=Left(sOldFile,14) & ".bak"
oceanhai

How to remove part of filename

Post by oceanhai »

Thanks for your reply. I'd thought about that but the db name is different for every db (a different number of characters). I also thought about just deleting the 4 characters to the left of the "." in the filename but I wouldn't want to accidentally delete the characters for YYYYMMDD on filenames that had already been modified the day prior. Basically, I just want to check once daily to make sure all files in a directory have the format of dbname_db_YYYYMMDD.bak and delete the HHMM (hours and minutes from the timestamp in the filename when necessary).
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

How to remove part of filename

Post by jhicks »

Then you'll need to use regular expressions. A pattern like this will match or verify the entire filename^([a-zA-Z0-9]*)_ww_dddddddd.bak$Of course with Regex you can slice and dice this many ways. You might use Regex to extract just the matching pattern:strString="db1_db_200706051234.bak"strPattern="^([a-zA-Z0-9]*)_ww_dddddddd"strNewFile=Trim(GetMatch(strString,strPattern))Function 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 »

I've used your regular expression script and added some functionality to rename files. However, I'm getting an error stating "(21, 2) (null): The system cannot find the file specified."Here is the script I'm using. If I run the rename command manually it works fine.Dim strSourceString, strNewString, strRenameCommandSet ws = WScript.CreateObject("WScript.Shell")Set objFSO = CreateObject("Scripting.FileSystemObject")objStartFolder = "C:ScriptsDevRenameBAKFilesBackup"Set objFolder = objFSO.GetFolder(objStartFolder)Set colFiles = objFolder.FilesFor Each objFile in colFiles strSourceString = objFile.Name Wscript.Echo strSourceString strPattern="^([a-zA-Z0-9]*)_ww_dddddddd" strNewString=Trim(GetMatch(strSourceString,strPattern)) strNewString=strNewString & ".bak" strStartFolder = objStartFolder WScript.Echo(strNewString) 'Rename Files strRenameCommand = "ren" & " " & strStartFolder & "" & strSourceString & " " & strNewString WScript.Echo(strRenameCommand) ws.Run(strRenameCommand)Next'strString="db1_db_200706051234.bak"Function 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
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

How to remove part of filename

Post by jhicks »

Oh, you need to specify cmd.exe in the rename command strRenameCommand = "cmd /c ren" & " " & strStartFolder & "" & strSourceString & " " & strNewString
oceanhai

How to remove part of filename

Post by oceanhai »

Great! Everything tests out fine now. Thanks for all of your help.
oceanhai

How to remove part of filename

Post by oceanhai »

Hi jhicks,It turns out that some of the database names have one or more underscores in them, in addition to the two in "_db_" in the filename and this is causing problems with the pattern matching. For example, the match does not seem to work on a file with the name of "fred_test_data_db_200706210130.bak". Is there anything else I can add there to match an underscore "_" that could appear anywhere in the string? Thanks!
oceanhai

How to remove part of filename

Post by oceanhai »

Wait, it turns out that the pattern matching works fine on my dev machine, no matter how many underscores there are in the filename. However it is not working on the prod server. Any filenames that contain an underscore, other than the two in "_db_", will be skipped over and not renamed.The only difference I can think of is that my dev machine is Win2k3 sp1 and the prod machine is Win2k sp4. Has anyone seen anything like this? Is there an upgrade or patch that I might try? Thanks.
This topic is 16 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked