Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename files

Status
Not open for further replies.

LearnScript

Programmer
Dec 28, 2011
3
US
Hi All,
I am new to this group and this is my first post. I need to write a vb script and I did not write any scripts before. Here is my problem. I have a folder A which contains .wav files. I need to move to Folder B and rename all the files. For example, I have abc_companyname.wav files in folder A then I have to construct a new name to rename.
example:
folder A : AbcFarm10152011_01.wav
if filename consists of Farm then construct a new name like below and rename.
folder B : clientname_vendorname_Farm_FT_FC_AID_date_CallID_.wav

Below is my code. I am doing something wrong which I couldn't figure out.
Dim fso,f
dim clientname , Vendor, code, FT, FC, AID, CallDate, CallID, FileType
Set fso=CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\Docs\abc\queue\Source")
clientname= "abc"
Vendor= "xyz"
AID= "2222222"
CallDate = Date
FileType = "wav"
CallID = 000000
For Each file In f.Files
If InStr(file.Name,"Farm")>0 Then
code = "Farm_CT"
FT = "XYZ"
FC = "ABCXYZ"
CallID = CallID +1

newName = clientname & "_" & vendor & "_" & code & "_" & FT & "_" & FC & "_" & AID & "_" & CallDate & "_" & CallID & "_" & FileType
fso.MoveFile file.Name , "C:\Docs\abc\queue\Target\newName"
End If
Next

Set f = Nothing
Set fso = Nothing

I am getting file not found error. I didn't see any rename function in vb script. To use replace function I need to change the entire file name not a substring of it. I am doing something wrong at fso.MoveFile area. Any help would greatly appreciated.
 
I'd replace this:
CallDate = Date
with something like this:
CallDate = Year(Date) & Right("0" & Month(Date),2) & Right("0" & Date(Date),2)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV. My question is how to construct the newName with all the variables and save file with that new name.
when I do fso.MoveFile file.Name , "C:\Docs\abc\queue\Target\newName"
then it is saving a file with newName but not with a newly constructed name
 
fso.MoveFile file.Name , "C:\Docs\abc\queue\Target\" & newName

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
also, looks like "newName" is missing a dot before the extension... either
Code:
FileType = "[COLOR=red].[/color]wav"
or
Code:
newName = clientname & "_" & vendor & "_" & code & "_" & FT & "_" & FC & "_" & AID & "_" & CallDate & "_" & CallID & "_" & [COLOR=red]"." & [/color]FileType
 
Thank you all. I am still not able to rename with newName. I figured out that the problem is with concatenation. when i give something like a single word (sample.mp3) then it is saving with that word. If i concatenate with something else ("NewFile.MP3" & CallDate) then I am getting an exception like below
NotSupportedException was unhandled
The given path's format is not supported.
Any help would greatly appreciated.

Thanks.
 
does the destination path exist? ensure the newName does not contain any \.

Also, "NewFile.MP3" & CallDate would equal "NewFile.MP3201111229"

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top