Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Vbscript

Posted on 2016-09-27
8
Medium Priority
?
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 57

Accepted Solution

by:
Bill Prew earned 2000 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 57

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 57

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 57

Expert Comment

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

~bp
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

670 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