[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

Auto-Copy Program - fancy copy script

Hi

I need a program to do the following:

Source Folder: C:\Source
Destination: D:\Source-dd-mm-yy

So I need a program that will copy the enitre source folder to another local drive, but append onto it the date.

If there is a feature to cycle data, even better. I.E if out of space delete the oldest folder.

0
hongedit
Asked:
hongedit
  • 8
  • 3
  • 3
  • +2
2 Solutions
 
Rahul_GadeCommented:

You don't need a program, you can just write few lines of command in a batch file to do the same.
Just make use of some DOS commands like copy, date, echo, "if exists", "for in" loop etc.

-Rahul
0
 
hongeditAuthor Commented:
Could you provide an example?

My DOS skills are limited to CD and DIR.
0
 
chandrasekar1Commented:
i am not expert in writing the batch script, but as far i searched, we can copy the files from source to target folder with current date, but checking the drive size and deleting the files is not feasible.

for /f "tokens=2-4 delims=/ " %%g in ('date /t') do (
  set mm=%%g
  set dd=%%h
  set yy=%%i
)

if exist "d:\destination\%mm%-%dd%-%yy%" (
  rd /S /Q "d:\destination\%mm%-%dd%-%yy%"
)

xcopy "C:\source" "d:\destination\%mm%-%dd%-%yy%" /s /i

Open in new window



save this code to a .bat file and doubleclick it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hongeditAuthor Commented:
Thanks - that seems to do the trick mostly!

On my test the destination only says 10-2011-

So it's missing the day

0
 
chandrasekar1Commented:
its due to different time zone setting, works fine for India, let me know your zone, ill try to resolve it
0
 
hongeditAuthor Commented:
UK :)
0
 
chandrasekar1Commented:
hongedit, i not able to reproduce the problem in my system by changing the TimeZone, after checking in net further, i found below code

if exist "d:\destination\%date:~-4%-%date:~3,2%-%date:~0,2%" (
  rd /S /Q "d:\destination\%date:~-4%-%date:~3,2%-%date:~0,2%"
)

xcopy "C:\source" "d:\destination\%date:~-4%-%date:~3,2%-%date:~0,2%" /s /i

Open in new window


ref link
http://stackoverflow.com/questions/203090/how-to-get-current-datetime-on-windows-command-line-in-a-suitable-format-for-us
http://stackoverflow.com/questions/1192476/windows-batch-script-format-date-and-time
0
 
BillDLCommented:
Hi hongedit

Your system has built-in Date and Time system variables that can be echoed back at any time.  If you open a new Command window and type the following command it should show you the same format of date as I have, ie. DD/MM/YYYY, currently 15/10/2011

echo %DATE%

You cannot use the / delimiter in a file name, so you want to use a Modifier that splits up the DD MM and YYYY parts.  a variable like %DATE% can be modified like this:
%DATE:~x,y%
where x = the starting point from zero at the left, and y = the number of characters to include.

Type the following command and you should get "DD-MM-YYYY" (15/10/2011) from the date variable:

echo %DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%

You can swap that around to get YYYY-MM-DD, which is a lot better for logical file sorting if that is the file name prefix, with this:

echo %DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%

Change your system date on a computer to one with a single digit day, eg. go back to 9th October, and ensure that it includes the leading Zero (09-10-2011) and doesn't jumble up the results.  It should be fine on your system.  I'm UK based too and it works OK for me.

You can re-use
%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%
anywhere you want to use it in a batch file, eg copy file and apply date suffix to new name:.

copy "Filename.txt" "Filename_%DATE:~0,2%-%DATE:~3,2%-DATE:~6,4%.txt"

So, transferring this to chandrasekar1's batch file it would look like this with a couple of extra modifications:
 
@echo off

REM Modify the paths to suit in the 3 lines below.
REM the 3 variables will expand to whatever is set here.

set FolderName=FolderName
set SRC=C:\Source
set DEST=D:\Destination

if exist "%DEST%\%FolderName%_%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%" (  
  rd /S /Q "%DEST%\%FolderName%_%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%"  
)  
  
xcopy "%SRC%\%FolderName%" "%DEST%\%FolderName%_%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%" /s /i

pause

Open in new window

What happens if the batch file has already been run that day and the folder with today's date already exists in the destination?

chandrasekar1's batch file tested for the folder's existence and deleted it if found, but I'm not sure that would be what you want.  Is it?

There are other eloquent ways of getting the current date using VBScripts, and I know of a small standalone command line program specifically designed to create a folder with the current date, but I don't think you will need it.

All you have to do now is ensure that the XCOPY command does what you want:

xcopy /?   for available command line switches.

You can add the switches in any order you wish and none are case sensitive.  Some override others though.  Read the /I switch description and be aware that adding a trailing backslash to a folder name can be problematic when doing a folder to folder copy with the /S or /E switches.

I suggest that you test and list the copy progress without actually copying anything by initially adding the /L switch which "displays file names that WOULD BE copied".  Alternatively in a batch file just add an   ECHO   in front of the XCOPY command to see activity.

You mentioned "cycling" data.  It is possible to do some maths in batch files to get yesterday's date or the date a week ago (like Tomorrow.bat), but it involves some gymnastics and VBScript is usually better equipped for this as suggested HERE.  I'm not expert in VBScript, but

For those purposes it might be better in the longer term if you had a read through the ROBOCOPY usage, because this is the kind of thing that it is designed for:
http://ss64.com/nt/robocopy.html
It supports a lot more "Exit Codes" that can be used as a diagnostic and you can have your batch file email you (or some other action) if it fails for particular reasons:
http://ss64.com/nt/robocopy-exit.html

I hope this helps.

Bill
0
 
hongeditAuthor Commented:
Exceptional Answers.

Perfect!
0
 
hongeditAuthor Commented:
Oo, bit quick on the trigger there.

I just noticed something with the script as it is, I probably should have mentioned this.

THe SOURCE drive I want to copy is the entire drive, i.e S:\*.*
The Destination Drive is to a folder on a drive, i,e D:\Backups

I set the FolderName variable to *
SRC = S:\
DEST = D:\Backups

This copies the contents of S:\ but straight as it is, and not into a folder with a date.

If I set FolderName to anything (i.e backup) it says"File not found - backup"

If I leave it as *, after a while I get the error "Could not expand second file name as to match first"
0
 
hongeditAuthor Commented:
Ok, I changed it, now new error!

INSUFFICIENT MEMORY

7403 Files Copied.

SRC Drive - 18GB
DES Drive at time of failure - 6.1GB

:(


@echo off

REM Modify the paths to suit in the 3 lines below.
REM the 3 variables will expand to whatever is set here.

set SRC=S:\
set DEST=D:\Backups

if exist "%DEST%\%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%" (  
  rd /S /Q "%DEST%\%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%"  
)  
  
xcopy "S:\" "D:\Backups\%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%" /s /i

pause

Open in new window

0
 
BillDLCommented:
Hi hongedit

Lack of "memory" doesn't really refer to free hard drive capacity, however some error messages can be very cryptic and it can be hard to know what the cause is.

You can check the last Exit Code by typing:
echo %errorlevel%
however the XCOPY exit codes are not comprehensive enough to give an exact reason for an error:

0  - Success. Files were copied without error.
1  - No files were found to copy.
2  - Ctrl+C was used to terminate process.
4  - Various errors (insufficient memory/disk space, invalid drive, invalid syntax).
5  - Disk write error occurred.

In your case XCOPY most likely failed with a "disk space" error (Exit Code 4) and it just reported it as "insufficient memory".

What actually is the total combined size of all the files and folders on your S: Drive?
Does it exceed the free space on your D: Drive?
Is there an operating system running off the S: Drive, and if so what is it?
0
 
BillDLCommented:
Try the same batch file but instead of using   S:\    remove that trailing backslash and just use   S:

I suggest that you add in the /L switch meantime to try and determine where the failure is occurring.
0
 
hongeditAuthor Commented:
I actually managed to get it working like this:

 
mkdir C:\Users\User\Desktop\LiveDrivebackup\%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%
robocopy L:\ "C:\Users\User\Desktop\LiveDrivebackup\%DATE:~0,2%-%DATE:~3,2%-%DATE:~6,4%" /MIR /MT:10 >C:\log.txt

Open in new window


However it seems to fail on scheduled tasks, and I dont know why...
0
 
hongeditAuthor Commented:
Those are the real paths by the way...
0
 
paultomasiCommented:
BillDL

Thanks for mentioning TOMORROW.BAT in http:#a36972787. I've only just been notified of it.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now