Ninjaguy900
asked on
Create folder in vbs
Hi Experts
I need to create a vbs script that allows me to rename and copies files to a folder on the same server
I have managed to find the code for renaming the file based on the creation date
But now I also need for this file to be copied to a folder that potentially doesnt exist
i.e.
report is created called sample.csv
script is run nightly to pick up the new file and rename it to the creation date sam_120709.csv
how do I then move the file into a folder that doesnt exist
am looking to put files into monthly folders
folder name is simply yyyymm
Below is what I have so far
Thanks in advance
I need to create a vbs script that allows me to rename and copies files to a folder on the same server
I have managed to find the code for renaming the file based on the creation date
But now I also need for this file to be copied to a folder that potentially doesnt exist
i.e.
report is created called sample.csv
script is run nightly to pick up the new file and rename it to the creation date sam_120709.csv
how do I then move the file into a folder that doesnt exist
am looking to put files into monthly folders
folder name is simply yyyymm
Below is what I have so far
Thanks in advance
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='C \Report'} Where " _
& "ResultClass = CIM_DataFile")
For Each objFile In FileList
strDate = Left(objFile.CreationDate, 8)
strNewName = objFile.Drive & objFile.Path & _
"X143" & "_" & strDate & "." & "csv"
strNameCheck = Replace(strNewName, "\", "\\")
i = 1
Do While True
Set colFiles = objWMIService.ExecQuery _
("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
If colFiles.Count = 0 Then
errResult = objFile.Rename(strNewName)
Exit Do
Else
i = i + 1
strNewName = objFile.Drive & objFile.Path & _
"X143" & "_" & strDate & "_" & i & "." & "csv"
strNameCheck = Replace(strNewName, "\", "\\")
End If
Loop
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for you prompt response
I'm having a very steep learning curve !
Skipper68 - I am getting an error on your code on line 10 - really done know why
Shift-3 the code works to a point, it creates the folders based on the creation date, but fails to move them into the folder
Am I missing something?
What should the correct process be
copy , rename, then move, then delete
or is it possible to rename & move in the same segment of script
many thanks
I'm having a very steep learning curve !
Skipper68 - I am getting an error on your code on line 10 - really done know why
Shift-3 the code works to a point, it creates the folders based on the creation date, but fails to move them into the folder
Am I missing something?
What should the correct process be
copy , rename, then move, then delete
or is it possible to rename & move in the same segment of script
many thanks
ASKER
My bad Shift-3:
hadn't removed the apostrophe from the beginning of line 30!
doh
many thanks
hadn't removed the apostrophe from the beginning of line 30!
doh
many thanks
Open in new window