batch file for copying
Hi:
I'm sitting here on Monday morning and my brain does not want to function properly. I am in need of a simple batch file that will run daily using scheduled tasks and was hoping to get some help. I need it to do the following:
Copy a single backup file (C:\backups\dailybk.bkf) on pc called SOURCE to a differnt pc called DESTINATION
The PC's are on a WAN connected by a permanent Router to Router VPN
The host IP is 192.168.1.1
The destination IP is 10.0.0.1
Connectivity and ability to copy is not an issue and works well
My problem is that I need to rename the file being copied to the DESTINATION to one that has a Date stamp that reflects the day / month / year that it was created on the target (DESTINATION pc)and I don't know the variables to do this.
The fle on the SOURCE pc however must not change.
with much thanks
charles
August 27th, 2007 4:49pm
Tuesday morning, and it's not the most beautiful script ever written, but it works.
You'll need to create a share on your destination pc and edit the script to match your environment.
Code Snippet
' Construct datebased filenamedtmThisDay = Day(Date)dtmThisMonth = Month(Date)dtmThisYear = Year(Date)strBackupName = dtmThisYear & "_" & dtmThisMonth & "_" & dtmThisDay
' Map network drive - configurationstrDrive = "Z:"strPath = "\\10.0.0.1\backups"set objNetwork = WScript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive strDrive, strPath
' Copy and rename fileSet objFSO = CreateObject("Scripting.FileSystemObject")objFSO.CopyFile "C:\backups\dailybk.bkf" , "Z:\" & strBackupName & ".bkf"
' Remove networkdriveobjNetwork.RemoveNetworkDrive strDrive
Let me know if you run into trouble.
Free Windows Admin Tool Kit Click here and download it now
August 28th, 2007 10:24am
This one renames the file based on when it's copied though, I just realized. Good enough or do you need it rewritten?
August 28th, 2007 10:25am
Here's a modified one. I'm based in Sweden so I'm not sure about the locale formatting of dates on your side, but try it out. You'll need to modify the path to the original file on two places.
Code Snippet
' Map network drive - configurationstrDrive = "Z:"strPath = "\\destinationserver\sharename"set objNetwork = WScript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive strDrive, strPath
' Copy and rename fileSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile("C:\backup\blabla.bkf")
' Get file creation date and format itstrBackupName = FormatDateTime(objFile.DateCreated, 2)objFSO.CopyFile "C:\backup\blabla.bkf" , "Z:\" & strBackupName & ".bkf"
' Remove networkdriveobjNetwork.RemoveNetworkDrive strDrive
Free Windows Admin Tool Kit Click here and download it now
August 28th, 2007 2:13pm
Thank you for your assistance with this. I am trying to test it on my workstation to make sure I have the right setup and it does not seem to be working. Here are the details. Would you please review and tell me where I'm going wrong?
My origional file is C:\TESTC\TEST1.BKF
My destination folder is D:\TESTD
My internal IP is 10.1.1.135
My computer name is charles-hp
The drive I will use is X
Here is how I set it up. It was created as a TXT document. I then changed it to a BAT file and ran it.
Code Snippet' Map network drive - configurationstrDrive = "X:"strPath = "\\CHARLES-HP\TESTD"set objNetwork = WScript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive strDrive, strPath
' Copy and rename fileSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile("C:\TESTC\TEST1.bkf")
' Get file creation date and format itstrBackupName = FormatDateTime(objFile.DateCreated, 2)objFSO.CopyFile "C:\TESTC\TEST1.bkf" , "X:\" & strBackupName & ".bkf"
' Remove networkdriveobjNetwork.RemoveNetworkDrive strDrive
Much thanks,
charles
August 28th, 2007 4:23pm
Sorry for posting an "almost" solution.
Save this:
strDrive = "X:"strPath = "\\CHARLES-HP\TESTD"set objNetwork = WScript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive strDrive, strPath
' Copy and rename fileSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile("C:\TESTC\TEST1.bkf")
' Get file creation date and format itstrBackupName = FormatDateTime(objFile.DateCreated, 2)objFSO.CopyFile "C:\TESTC\TEST1.bkf" , "X:\" & strBackupName & ".bkf"
' Remove networkdriveobjNetwork.RemoveNetworkDrive strDrive
And then rename it to for example copybackup.vbs. It's a VB-script, so a BAT-ending won't make it run.
Get back to me if it doesn't work
Free Windows Admin Tool Kit Click here and download it now
August 28th, 2007 9:22pm
Hi again,
It still is not working. I have copied your script and pasted it into a simple txt file then I renamed it to a .bat file. Saved it and then ran it.
To test where it is going wrong I set a pause after each line. The following are the results from the CMD promt
thanks,
C
C:\Users\Charles.DOMAIN\Desktop>strDrive = "X:"
'strDrive' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Charles.DOMAIN\Desktop>strPath = " \\CHARLES-HP\TESTD"
'strPath' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Charles.DOMAIN\Desktop>Pause
C:\Users\Charles.DOMAIN\Desktop>set objNetwork = WScript.CreateObject("WScript.Network")
C:\Users\Charles.DOMAIN\Desktop>objNetwork.MapNetworkDrive strDrive, strPath
'objNetwork.MapNetworkDrive' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Charles.DOMAIN\Desktop>Pause
C:\Users\Charles.DOMAIN\Desktop>' Copy and rename file
''' is not recognized as an internal or external command, operable program or batch file.
C:\Users\Charles.DOMAIN\Desktop>Set objFSO = CreateObject("Scripting.FileSystemObject")
C:\Users\Charles.DOMAIN\Desktop>Pause
C:\Users\Charles.DOMAIN\Desktop>Set objFile = objFSO.GetFile("C:\TESTC\TEST1.bkf")
C:\Users\Charles.DOMAIN\Desktop>Pause
C:\Users\Charles.DOMAIN\Desktop>' Get file creation date and format it
''' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Charles.DOMAIN\Desktop>strBackupName = FormatDateTime(objFile.DateCreated, 2)
'strBackupName' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Charles.DOMAIN\Desktop>objFSO.CopyFile "C:\TESTC\TEST1.bkf" , "X:\" &
strBackupName & ".bkf"
'objFSO.CopyFile' is not recognized as an internal or external command,
operable program or batch file.
'strBackupName' is not recognized as an internal or external command,
operable program or batch file.
'".bkf"' is not recognized as an internal or external command,
operable program or batch file.
-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0--0-0-0-0-0-0-0-0-0-0-0--0-0-0-0-0-0-0--0-0-0-0-0-0--0
Here is the exact .bat file I tried to run
strDrive = "X:"strPath = " \\CHARLES-HP\TESTD"
Pause
set objNetwork = WScript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive strDrive, strPathPause
' Copy and rename fileSet objFSO = CreateObject("Scripting.FileSystemObject")PauseSet objFile = objFSO.GetFile("C:\TESTC\TEST1.bkf")Pause
' Get file creation date and format itstrBackupName = FormatDateTime(objFile.DateCreated, 2)objFSO.CopyFile "C:\TESTC\TEST1.bkf" , "X:\" & strBackupName & ".bkf"Pause
' Remove networkdriveobjNetwork. RemoveNetworkDrive strDrivePause
August 28th, 2007 9:54pm
Do NOT rename it to .BAT. Copy the code and paste in into a .txt-document. Then rename it to end with .VBS. The script is coded in VB-script, hence the VBS-ending. That is to utilize the WSCRIPT objects for coding.
You should call the file something like COPYBACKUP.VBS
Free Windows Admin Tool Kit Click here and download it now
August 29th, 2007 12:06am
Joachim:
My mistake, I did not read the post carefully enough. I still have an issue however. I tried running the script after I changed the exstension to .VBS and got this error.
Script: C:\testc\batchtest.vbs
Line:12
Char: 1
Error:Path not found
Code: 800A004C
Source: Microosft VBScript runtime error
The exact file I am using is:
strDrive = "X:"strPath = "\\CHARLES-HP\TESTD"set objNetwork = WScript.CreateObject("WScript.Network")objNetwork.MapNetworkDrive strDrive, strPath
' Copy and rename fileSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.GetFile("C:\TESTC\TEST1.bkf")
' Get file creation date and format itstrBackupName = FormatDateTime(objFile.DateCreated, 2)objFSO.CopyFile "C:\TESTC\TEST1.bkf" , "X:\" & strBackupName & ".bkf"
' Remove networkdriveobjNetwork.RemoveNetworkDrive strDrive
thanks,
Charles
August 29th, 2007 2:42pm
You're getting a "path not found" error which means that the script either can't map the network drive, or find the file you're trying to copy.
Please verify that you can map the network share manually and that the path is correct to the file being copied.
Free Windows Admin Tool Kit Click here and download it now
August 29th, 2007 4:23pm
Hi:
I checked at tested both the map drive and file and everything is in place. THe onlyother variable is that I'm doing my tests on a Vista box.
C
August 29th, 2007 7:51pm
Follow this and make sure that wscript is your default scripting host. The script was developed on a Vista box too, so it works in that environment.
http://technet2.microsoft.com/windowsserver/en/library/d70e796a-c2b9-4849-837b-dd5d93f12f331033.mspx?mfr=true
Free Windows Admin Tool Kit Click here and download it now
August 29th, 2007 8:40pm
Hi Charles,The issue is the filename you are generating. FormatDateTime([date], 2) formats a date as a short date. In this case, the file name looks like "8/30/2007". The FileSystemObject interprets the slashes of course as folder paths, thats why you get the Path not found error.
Replace your:
strBackupName = FormatDateTime(objFile.DateCreated, 2)
With:
strBackupName = Year(objFile.DateCreated) & "-" & _
Right("00" & Month(objFile.DateCreated), 2) & "-" & _
Right("00" & Day(objFile.DateCreated), 2)
This formats the filename as 2007-08-30, no path operators and it will work perfectly.
Thanks,
Mike Hatfield
August 30th, 2007 12:25am
Thanks Mike, my Swedish date format worked flawlessly...
Free Windows Admin Tool Kit Click here and download it now
August 30th, 2007 8:35am
Thank you to all that assisted with this.
Charles
August 30th, 2007 2:15pm