Tag Archive | archiving

Move all files in the current directory to an archive folder via batch script

Sometimes you work on some files within a directory and want to do a backup from time to time. Of course, if you have a version control system like git or svn by hand, you might use this. But if you have no versioning tool, a simple technique often used is to copy all files to an archive folder in the current directory and append a version string to the filename. If you do this a few times after another, you might want to write a small batch script, that does this stupid job for you.

Today I found myself in this situation, thus I started writing a small batch script that copies all files except the batch file itself to the an archiv folder, appending a timestamp to the filename:


set archivefoldername=Archiv

set hour=%time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
echo hour=%hour%
set min=%time:~3,2%
if "%min:~0,1%" == " " set min=0%min:~1,1%
echo min=%min%
set secs=%time:~6,2%
if "%secs:~0,1%" == " " set secs=0%secs:~1,1%
echo secs=%secs%

set year=%date:~-4%
echo year=%year%
set month=%date:~3,2%
if "%month:~0,1%" == " " set month=0%month:~1,1%
echo month=%month%
set day=%date:~0,2%
if "%day:~0,1%" == " " set day=0%day:~1,1%
echo day=%day%
set datetimef=%year%-%month%-%day%_%hour%%min%%secs%

if not exist %archivefoldername% mkdir %archivefoldername%

for %%i in (*) do (
 if not %~n0 == %%~ni (
  copy %%i %archivefoldername%\%%~ni_%datetimef%%%~xi
 )
)