Solved

Vbscript

Posted on 2016-09-27
8
88 Views
Last Modified: 2016-10-28
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
Comment
Question by:Barron1299
  • 4
  • 3
8 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41819011
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
 
LVL 1

Author Comment

by:Barron1299
ID: 41819053
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41820387
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:Barron1299
ID: 41820675
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41841808
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
 
LVL 1

Author Comment

by:Barron1299
ID: 41862768
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41863911
Thanks, glad that was useful!

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question