Link to home
Start Free TrialLog in
Avatar of georgedschneider
georgedschneiderFlag for United States of America

asked on

VBscript to copy directoy

I'm attempting to write a vbscript that will copy a directory to a specified location.  The current direcoty is located at C:\l and I want to copy the direcotry to C:\Location2.  Here's the tricky part that I can't seem to figure out.  The direcotry changes every day based on date.  For example today's directory would be 20080929.  The script I've tried is below which doe snot seam to work.


Dim FSO
Set objFSO = Createobject ("Scripting.FileSystemObject")
strMonth = Right("0" & Month(Date), 2)
strDay = Right("0" & Day(Date), 2)
strYear = Year(Date)
Myfile = strYear & strMonth & strDay
objFSO.CopyFolder "C:\MyFile", "C:\Test\"
WScript.Echo "File Copied"

Open in new window

Avatar of MikeKane
MikeKane
Flag of United States of America image

Very easy -
objFSO.CopyFolder "C:\MyFile", "C:\Test\"
should be
objFSO.CopyFolder "C:\" & MyFile, "C:\Test\"

Otherwise you are copying the string "C:\myfile" instead of the variable you really want.
Avatar of georgedschneider

ASKER

I've adjusted to code to the below and I'm receiving an at line 7, char 1 path not found with a code 800A004C.
Dim FSO
Set objFSO = Createobject ("Scripting.FileSystemObject")
strMonth = Right("0" & Month(Date), 2)
strDay = Right("0" & Day(Date), 2)
strYear = Year(Date)
Myfile = strYear & strMonth & strDay
objFSO.CopyFolder "C:\" & Myfile,"C:\Test\"
WScript.Echo "File Copied"

Open in new window

Its Monday.  My brain must still be asleep.  The test folder had saturday;s date so would have produced the error I jsut posted.  How can I ensure that if the folder exists in the destination folder it will overwrites what's there?

Here the scenario I ran.
1) Craeted a text file in a folder called 20080929 called test.txt
2) Ran the script and the folder was copied to C:\Test
3) Deleted the test.txt file form the folder 20080929
4) Ran the script agin

Result the test.txt file was still in the Test\20080929 folder.
Oops..   sorry about that.  Not enough coffee yet I guess.

change
"C:\Test\"
to
C:\Test"
The format of:
objFSO.Copyfolder
includes an overwrite option as the 3rd parameter.  

I.e.
objFSO.CopyFolder "C:\" & Myfile,"C:\Test\", TRUE
This seems to only overwrite exisitng files.  Meaning if a file is deleted form the folder 20080929 and then I copy it to the location Test\ it will overwrite the folder but not midify its contents.  This may be the way the copy process works.  

Is there a simialr command to perform a move or a cut and paste?


Dim FSO
Set objFSO = Createobject ("Scripting.FileSystemObject")
strMonth = Right("0" & Month(Date), 2)
strDay = Right("0" & Day(Date), 2)
strYear = Year(Date)
Myfile = strYear & strMonth & strDay
objFSO.CopyFolder "C:\"& Myfile, "C:\Test\", True 
WScript.Echo "File Copied"

Open in new window

You mean if a file is removed from the source, and you run the script again, you want the target to look the same.....?
Why not just issue a delete folder TEST before issuing the copy?  

-or-

You could also make an external call to ROBOCOPY and use the /MIR option to mirror the directory....  

I modfied the script to the bellow and have it workign pretty well.  My one question is there a way to put something in for the 13 days during the year when I wouldn't want it it run other than on Sunday's?  For exapmle Christmas, Thanksgiivng, New Year's day etc?  One way would be to create several statements where Mydate <> to 12/25/2008 or 01/01//2209 etc.  This would be a little cumbersome.  Even if icould have it read the dates form a file somewhere it would be better than having to input the holidays every year?
Set objFSO = Createobject ("Scripting.FileSystemObject")
MyDate = Date  
Wkday = Weekdayname(weekday(Mydate))
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Year(MyDate)
If Wkday <> "Sunday" Then 
Myfile = strYear & strMonth & strDay
objFSO.CopyFolder "C:\"& Myfile, "C:\Test"
WScript.Echo "Files Copied"
End IF

Open in new window

I'll tell ya what, I've worked in the financial IT sector for 20 years.  Managing the holiday schedule is still a manual process.  

The fixed holidays like xmas and the 4th are easy enough: i.e. if month = 12 and day = 25 ,  if month = 07 and day = 04, etc).    The other holidays that fall on various days have always been entered manually to a conf file that is read by the rest of the systems.    

You could add the 7 constants at the top of the script for the 7 federal holidays.....

constXMAS = 1225
constLaborday= 0903

Then just run the evaluations before the copyfolder function.....    



Either way, you'll end up editing some file every year.....


Avatar of RobSampson
Wouldn't it be easier to just not copy anything if the Myfile folder doesn't exist?  That way, if you're backup didn't run, it wouldn't try to copy anything....

Regards,

Rob.
Set objFSO = Createobject ("Scripting.FileSystemObject")
MyDate = Date  
Wkday = Weekdayname(weekday(Mydate))
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Year(MyDate)
If Wkday <> "Sunday" Then 
Myfile = "C:\" & strYear & strMonth & strDay
If objFSO.FolderExists(Myfile) = True Then
	objFSO.CopyFolder Myfile, "C:\Test"
	WScript.Echo "Files Copied"
Else
	MsgBox Myfile & " does not exist. Files not copied."
End IF

Open in new window

that was the other thought i had before I posted my last response.  I'll give it a shot and see what happens.
The file exist command you showed me is much better then doing a weekday check to make sure its not Sunday since no file is produced on Sunday.  I alterted the script to below.  One final question onthis.  Is it possible to the the files copied written to a text fwith the date and time as a way of creeating a log file?
Set objFSO = Createobject ("Scripting.FileSystemObject")
MyDate = Date - 1   
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Year(MyDate)
Myfile = "C:\" & strYear & strMonth & strDay
If objFSO.FolderExists(Myfile) = True Then  
objFSO.CopyFolder Myfile, "C:\Test"
WScript.Echo "Files Copied"
Else
WScript.Echo "No Files Exist"
End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm using the following code and I'm receiving an undetermined string constant at Line 9 Character 49 with a code of 800A0409 with a source of Microosft VBScript compilation error.
Set objFSO = Createobject ("Scripting.FileSystemObject")
Const intForAppending = 8
strLogFile = "C:\FolderCopyLog.log"
MyDate = Date - 1   
Wkday = Weekdayname(weekday(Mydate))
strMonth = Right("0" & Month(MyDate), 2)
strDay = Right("0" & Day(MyDate), 2)
strYear = Year(MyDate)
Myfile = "C:\files & strYear & strMonth & strDay
If objFSO.FolderExists(Myfile) = True Then  
objFSO.CopyFolder Myfile, "C:\Test"
WScript.Echo "Files Copied"
Set objLog = objFSO.OpenTextFile(strLogFile, intForAppending, True)
objLog.WriteLine Now & ": " & MyFile & " copied to C:\Test"
objLog.Close
Set objLog = Nothing
End If

Open in new window

Hi, line 9 just needs another quote in it:
Myfile = "C:\files" & strYear & strMonth & strDay

Regards,

Rob.
That worked.  Thanks for your help on this.
No problem.  Thanks for the grade.

Regards,

Rob.