Link to home
Start Free TrialLog in
Avatar of Barron1299
Barron1299Flag for United States of America

asked on

Vbscript

I hope someone can help me out, I have some files that names are never consistent, the one thing that is constant is the beginning of the file name
Rename %src%\605*.mp3 SP1601.mp3
Rename %src%\617*.mp3 SP1602.mp3
Rename %src%\620*.mp3 SP1603.mp3
Rename %src%\632*.mp3 SP1604.mp3
Rename %src%\635*.mp3 SP1605.mp3
Rename %src%\640*.mp3 SP1606.mp3
Rename %src%\645*.mp3 SP1607.mp3
Rename %src%\702*.mp3 SP1608.mp3


I have it running in a batch script where is can search for the  number and rename it, which works great, but now I need it in vbscript. Is there a way to search for a the number in the name of the file and if it has it then rename it, just like the example I have above but using vbscrpt.

I tried: objFSO.MoveFile strFiledes & "605*.mp3", "SP1601.mp3" and it doesn't find the file.

Thanks in advance for your help
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Barron1299

ASKER

If you don't mind, I do have a couple questions:

So I got your code to work, except (I have never used the length function) I added a (1) in strNew to get the naming to work. Without the adding the 1 to stNew(1) it renamed file to 605, 617, 620, etc... and clipped the mp3 extenstion. I added the 1 and it renamed all files perfectly.

Part of the script before the rename, is copying all MP3 files from a read only folder (about 40 total files, but only need 29 specific files of the 40) would copyfile be better and be able to rename at the same time, then I can only pull the exact files I want?
Avatar of Bill Prew
Bill Prew

Sorry, cut and paste error on my part, yes, strNew needed to be (1), I editted the prior code for future correctness.

If you are copying the files one by one from the read only area, then yes I would say you can do the renaming right there.  If you need / want help post the code you have doing that and we can adapt this logic to it.

~bp
Here is part of what I have so far, unfortunately, I am copying files in bulk not one by one. I am trying to eliminate the call for the batch script to rename the files

On Error Resume Next

strDate = GetTomorrowDate
strBaseDropbox = "C:\Dropbox\Dropbox\" & strDate & "\"
strMoveFiles = strBaseDropbox & "*.mp3"
strFiledes = "C:\Dropbox\DailyImport\"
strDeleteFiles = strFiledes & "*.mp3"
strBatchScript = strFiledes & "RenameFiles.bat"

Set objFSO = CreateObject("Scripting.FileSystemObject")
	'clean out import folder for new files
	objFSO.DeleteFile strDeletefiles
	
	'Move all mp3 files to import folder for renaming
	objFSO.CopyFile strMoveFiles, strFiledes
	
'Run batch script to rename all files
Set objShell = WScript.CreateObject("WScript.Shell")
	'objShell.Run "C:\Dropbox\Renamefiles.bat"


'All function below

'Get Tomorrow's date M-D-YY
Function GetTomorrowDate
	strTomorrow = DateAdd("d",1,Date)
	strYear4 = DatePart("yyyy", strTomorrow)
	strYear = Right(strYear4,2)
	strMonth = DatePart("m",strTomorrow)
	strDay = DatePart("d", strTomorrow)
	
	GetTomorrowDate = strMonth & "-" & strDay & "-" & strYear
End Function

Open in new window

Okay, see if this makes sense, this is what I was thinking.  Read it first very carefully and make sure you understand it, and that you think it does what you want.  Then do some testing there to double check how it works.  I'll do some testing here too, but wanted to pass this along so you could validate it aligns with what you are trying to do.

strDropboxFolder = "C:\Dropbox\Dropbox\" & GetTomorrowDate()
strImportFolder = "C:\Dropbox\DailyImport"

Set objFSO = CreateObject("Scripting.FileSystemObject")

' Clean out import folder for new files
objFSO.DeleteFile strImportFolder & "\*.mp3"
	
' Copy all mp3 files to import folder for renaming
Set objFolder = objFSO.GetFolder(strDropboxFolder)

' Check each file in incoming folder
For Each objFile In objFolder.Files
   ' Only copy MP3 files
   If LCase(Right(objFile.Name, 4)) = ".mp3" Then
      ' Copy the file to the import folder changing name as needed
      objFile.Copy strImportFolder & "\" & NewName(objFile.Name)
   End If
Next
	

' ========== All functions below ==========

' Get Tomorrow's date M-D-YY
Function GetTomorrowDate
   datTomorrow = DateAdd("d",1,Date)
   strYear4 = DatePart("yyyy", datTomorrow)
   strYear = Right(strYear4,2)
   strMonth = DatePart("m",datTomorrow)
   strDay = DatePart("d", datTomorrow)
	
   GetTomorrowDate = strMonth & "-" & strDay & "-" & strYear
End Function

Function NewName(strOldName)
   ' Define array of old name pattern to new names
   arrRenames = Array(Array("605", "SP1601.mp3"), _
                      Array("617", "SP1602.mp3"), _
                      Array("620", "SP1603.mp3"), _
                      Array("632", "SP1604.mp3"), _
                      Array("635", "SP1605.mp3"), _
                      Array("640", "SP1606.mp3"), _
                      Array("645", "SP1607.mp3"), _
                      Array("702", "SP1608.mp3"))  

   ' Assume new name equals old name if no rename match is found
   NewName = strOldName

   ' Check it to each pattern we are matching for renames
   For Each arrMatch In arrRenames
      ' See if it matches, if so rename it
      strOld = arrMatch(0)
      strNew = arrMatch(1)
      If LCase(Left(strOldName, Len(strOld))) = LCase(strOld) Then
         NewName = strNew
      End If
   Next
End Function

Open in new window

~bp
So the first way worked for me the best, I was trying to get the second script you posted work but for some reason it wouldn't catch all the files. In other words, some days it would skip a file for no reason at all, it was weird. but i used your original layout and it works great. Thank you so much Bill, as you have done so many times assisted me and I am extremely grateful.
Thanks, glad that was useful!

~bp