Solved

Vbscript

Posted on 2016-09-27
8
65 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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:Barron1299
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
Thanks, glad that was useful!

~bp
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now