Solved

VBscript to copy directoy

Posted on 2008-09-29
17
376 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:georgedschneider
  • 8
  • 5
  • 4
17 Comments
 
LVL 33

Expert Comment

by:MikeKane
ID: 22596610
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.
0
 

Author Comment

by:georgedschneider
ID: 22596925
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

0
 

Author Comment

by:georgedschneider
ID: 22596996
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from 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.

 
LVL 33

Expert Comment

by:MikeKane
ID: 22597034
Oops..   sorry about that.  Not enough coffee yet I guess.

change
"C:\Test\"
to
C:\Test"
0
 
LVL 33

Expert Comment

by:MikeKane
ID: 22597068
The format of:
objFSO.Copyfolder
includes an overwrite option as the 3rd parameter.  

I.e.
objFSO.CopyFolder "C:\" & Myfile,"C:\Test\", TRUE
0
 

Author Comment

by:georgedschneider
ID: 22597266
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

0
 
LVL 33

Expert Comment

by:MikeKane
ID: 22597335
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....  

0
 

Author Comment

by:georgedschneider
ID: 22600668
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

0
 
LVL 33

Expert Comment

by:MikeKane
ID: 22600776
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.....


0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22601098
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

0
 

Author Comment

by:georgedschneider
ID: 22601940
that was the other thought i had before I posted my last response.  I'll give it a shot and see what happens.
0
 

Author Comment

by:georgedschneider
ID: 22604994
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

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22609829
Hi, sure.  This should write to a log file each time is run, although it only tells you the folder that was copied, not the individual files.  That's a limitation of the CopyFolder method.

Regards,

Rob.
Set objFSO = Createobject ("Scripting.FileSystemObject")
Const intForAppending = 8
strLogFile = "C:\FolderCopyLog.log"
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"
	Set objLog = objFSO.OpenTextFile(strLogFile, intForAppending, True)
	objLog.WriteLine Now & ": " & MyFile & " copied to C:\Test"
	objLog.Close
	Set objLog = Nothing
Else
	WScript.Echo "No Files Exist"
	Set objLog = objFSO.OpenTextFile(strLogFile, intForAppending, True)
	objLog.WriteLine Now & ": " & MyFile & " does not exist"
	objLog.Close
	Set objLog = Nothing
End If

Open in new window

0
 

Author Comment

by:georgedschneider
ID: 22689285
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

0
 
LVL 65

Expert Comment

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

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
ID: 22711303
That worked.  Thanks for your help on this.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22716312
No problem.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from 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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 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