EFFICIENT FILE COPYING AND ARCHIVING FROM MULTIPLE SOURCES WITH DATE-STAMPED BACKUPS TO A SPECIFIC EXTERNAL USB DRIVE
Drawing on my past experience with [DOS batch scripting]
, I’ve decided to streamline my workflow
by cutting out tedious, repetitive file operations. Even though I’m using the lightning-fast [Far Manager]
,
it’s still not quite quick enough and frankly, file operations demands way too many keystrokes.
This becomes especially irksome now that I’m juggling three projects at once.
Taking them from home to work or vice versa.
All that routine file shuffling eats up a sizeable chunk of my day.
Today, I’m putting an end to this inefficiency once and for all. The batch script I’ve prepared will:
- Copy the backend component of a project from its Apache directory into the corresponding React folder
- Immediately compress it into a 7z archive automatically stamped with today’s date in the filename
- Transfer the archive directly to a specific external USB drive [identified by its unique serial number]
- It also performs the same archival and copying routine for a second project
A few technical notes to mention:
- Data is written straight to the flash drive no temporary proxy files are created, minimizing overhead
- We’re leveraging 7-Zip’s multi-threading capabilities with a low compression ratio to prioritize speed
without sacrificing reliability
@echo off
setlocal enabledelayedexpansion
:: Fast date query
for /f "tokens=2 delims==." %%a in ('wmic os get localdatetime /value') do set "dt=%%a"
set "TODAY=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%"
:: Find drive first
set "TARGET_DRIVE="
for /f "skip=1 tokens=1,2" %%A in ('wmic logicaldisk get caption^,volumeserialnumber 2^>nul') do (
if "%%B"=="268967B7" set "TARGET_DRIVE=%%A"
)
if not defined TARGET_DRIVE (
echo Insert required flash disk!
pause
exit /b 1
)
:: Copy htdocs\app to backup location
echo Copying htdocs\app to backup location...
xcopy "C:\xampp\htdocs\app" "C:\Sites\Meals-on-wheels-04\_backups\htdocs\app" /E /I /H /Y >nul
:: Create destination folder on flash drive
set "DEST_DIR=%TARGET_DRIVE%\_%TODAY%"
if not exist "%DEST_DIR%" mkdir "%DEST_DIR%"
echo Starting super fast cloning...
:: Archive the two main folders to flash drive
"C:\Program Files\7-Zip\7z.exe" a -t7z -mx=0 -mmt=on "%DEST_DIR%\Meals-on-wheels-04-%TODAY%.7z" "C:\Sites\Meals-on-wheels-04\" >nul
"C:\Program Files\7-Zip\7z.exe" a -t7z -mx=0 -mmt=on "%DEST_DIR%\tracker-dap-%TODAY%.7z" "C:\Sites\tracker-dap\" >nul
echo Backup and archiving complete, have a nice day!
pause