how-to-use/os/backupdir.bat

37 lines
982 B
Batchfile

@echo off
setlocal enabledelayedexpansion
:: Set source and target directories (modify these paths)
set "source_dir="
set "target_base="
:: Get standardized timestamp using WMIC (format: YYYYMMDDHHMMSS)
for /f "tokens=1-6 delims=." %%a in ('wmic os get LocalDateTime ^| find "."') do (
set "datetime=%%a"
)
set "year=!datetime:~0,4!"
set "month=!datetime:~4,2!"
set "day=!datetime:~6,2!"
set "hour=!datetime:~8,2!"
set "minute=!datetime:~10,2!"
set "second=!datetime:~12,2!"
:: Create timestamp with seconds (YYYYMMDDHHMMSS)
set "timestamp=%year%%month%%day%%hour%%minute%%second%"
set "target_dir=%target_base%\%timestamp%"
:: Create target directory
if not exist "%target_dir%" (
mkdir "%target_dir%"
) else (
echo Target directory already exists: %target_dir%
pause
exit /b 1
)
:: Copy files
echo Copying files from %source_dir% to %target_dir%...
xcopy "%source_dir%\*" "%target_dir%\" /E /C /H /Y
echo Copy completed! Directory name: %timestamp%
pause