Saturday, December 27, 2014

Script to Automate Backup of Memory Cards on Windows



@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

REM All cards to copy from. List them all, separated by space
REM on my system, SD Card is E:
REM on my system, CF Card is G:
REM and I back up both of them

SET BACKUP_FROM_CARDS=E: G:

REM All drives to backup to. List them all, separated by space

SET BACKUP_TO_DRIVES=I:

REM Since I shoot JPG and NEF, I backup both (yeah, I know, it is crazy!)
REM All file types to backup from the card/s to drive/s. List them all, separated by space

SET FILE_TYPES=jpg nef

REM separator between year, month, and date (also used between hour, minutes, and seconds)
REM if not modified, directory files will be copied into will be appended with yyyymmdd_hhmmss

SET SEPARATOR=

REM set it to 0 if you do not want to shutdown Windows after backup is successfully completed

SET /A SHUTDOWN_ON_SUCCESS=1

REM 60 seconds after backup is completed, system will be shutdown if SHUTDOWN_ON_SUCCESS=1 above

SET /A SHUTDOWN_AFTER_SECONDS=60

REM ====================================================
REM   UNLESS YOU KNOW WHAT YOU ARE DOING,
REM      DO NOT MAKE ANY CHANGES BELOW
REM ====================================================

REM
REM calculate the folder name based on date/time
REM example folder where the files will be saved to: 20141227_131026
REM
SET

DATESTAMP=%DATE:~10,4%%SEPARATOR%%DATE:~4,2%%SEPARATOR%%DATE:~7,2%
SET TIMESTAMP=%TIME:~0,2%%SEPARATOR%%TIME:~3,2%%SEPARATOR%%TIME:~6,2%

SET DATEANDTIME=%DATESTAMP%_%TIMESTAMP%

SET /A CARD_AVAILABLE=0

SET /A DRIVE_AVAILABLE=0

REM
REM check card/s to backup is/are inserted
REM

for %%c in (%BACKUP_FROM_CARDS%) do (
    IF EXIST %%c\NUL (
        SET /A CARD_AVAILABLE=1
    )
)

REM
REM check drive/s to copy to is/are connected
REM

for %%d in (%BACKUP_TO_DRIVES%) do (
    IF EXIST %%d\NUL (
        SET /A DRIVE_AVAILABLE=1
    )
)


REM
REM if card/s not inserted, exit with an error
REM

if %CARD_AVAILABLE% EQU 0 (
    echo No card/s to copy from
    GOTO EXITFAILED
)

REM
REM if drive/s not connected, exit with an error
REM
if %DRIVE_AVAILABLE% EQU 0 (
    echo No drive/s to copy to
    GOTO EXITFAILED
)

REM
REM check if enough space is available on backup drives -TBD

REM
 

REM
REM now do what needs to be done!   
REM
for %%c in (%BACKUP_FROM_CARDS%) do (
    for %%d in (%BACKUP_TO_DRIVES%) do (
        mkdir %%d\%DATEANDTIME%
        for %%t in (%FILE_TYPES%) do (
            ECHO saving all %%t files from %%c to %%d\%DATEANDTIME%
            for /f %%f IN ('dir /b /s /A:-D %%c\*.%%t') do (               
                copy /Y /V %%f %%d\%DATEANDTIME% > NUL
            )
        )
    )
)
GOTO ALLDONE

:EXITFAILED
ECHO fix errors before you try again
SET SHUTDOWN_ON_SUCCESS=0

:ALLDONE
ECHO backup completed

REM
REM now that backup is complteted, format cards -TBD
REM


if %SHUTDOWN_ON_SUCCESS% EQU 1 (
    CHOICE /c YN /D Y /T 30 /M "Do You really want to shut down?"
    IF ERRORLEVEL 2 GOTO NOSHUTDOWN
    ECHO Shutting down in %SHUTDOWN_AFTER_SECONDS% seconds...

    SHUTDOWN /s /t %SHUTDOWN_AFTER_SECONDS%
REM     to put Windows to hibernation, instead, comment out the above line and uncomment the following line
REM     SHUTDOWN /h

)

:NOSHUTDOWN


This script is available for download from Dropbox. Feel free to use after modifying it to your needs. Make sure you check what drive letters your cards and drives resolve to on your Windows system and modify the script accordingly. Drives/cards on my system are as follows:




I have this script, pixbkp.bat, on Desktop of my Windows tablet (of an earlier post). I just double-tap it and the backup is done automagically! The script even shuts down my Windows system after backup is complete!!




Other than memory cards, hard drive, and HP Stream 7" Windows tablet, the following are what I used:
 
If you have any questions about this script or have suggestions to improve this script, please leave a comment below.

Labels: , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home