Solved

VBscript to copy directoy

Posted on 2008-09-29
17
355 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 33

Expert Comment

by:MikeKane
Comment Utility
Oops..   sorry about that.  Not enough coffee yet I guess.

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

Expert Comment

by:MikeKane
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:MikeKane
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi, line 9 just needs another quote in it:
Myfile = "C:\files" & strYear & strMonth & strDay

Regards,

Rob.
0
 

Author Comment

by:georgedschneider
Comment Utility
That worked.  Thanks for your help on this.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
No problem.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now