Link to home
Start Free TrialLog in
Avatar of hongedit
hongeditFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.

Avatar of Rahul Gade
Rahul Gade
Flag of India image


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
Avatar of hongedit

ASKER

Could you provide an example?

My DOS skills are limited to CD and DIR.
ASKER CERTIFIED SOLUTION
Avatar of chandrasekar1
chandrasekar1
Flag of India 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
Thanks - that seems to do the trick mostly!

On my test the destination only says 10-2011-

So it's missing the day

its due to different time zone setting, works fine for India, let me know your zone, ill try to resolve it
UK :)
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
SOLUTION
Avatar of BillDL
BillDL
Flag of United Kingdom of Great Britain and Northern Ireland 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
Exceptional Answers.

Perfect!
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"
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

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?
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.
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...
Those are the real paths by the way...
BillDL

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