• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

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
0
Barron1299
Asked:
Barron1299
  • 4
  • 3
1 Solution
 
Bill PrewCommented:
Right, if you use wildcards on the MoveFie method you have to specify a new folder as the second parm, not a new name.  Kinda stinks, but that's the way it works.  What you can do is look at each file in the folder and do the pattern match yourself.  Here's a sample of some code that should give you the idea, questions welcome....

' Define arrar 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"))  

strBaseDir = "C:\Temp"

' Instantiate filesystem object, open base folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Check each file in folder
For Each objFile In objFolder.Files
   ' Check it to each pattern we are matching
   For Each arrMatch In arrRenames
      ' See if it matches, if so rename it
      strOld = arrMatch(0)
      strNew = arrMatch(1)
      If LCase(Left(objFile.Name, Len(strOld))) = LCase(strOld) Then
         objFile.Name = strNew
      End If
   Next
Next

Open in new window

~bp
0
 
Barron1299Author Commented:
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?
0
 
Bill PrewCommented:
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
1
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Barron1299Author Commented:
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

0
 
Bill PrewCommented:
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
0
 
Barron1299Author Commented:
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.
0
 
Bill PrewCommented:
Thanks, glad that was useful!

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now