Backing up a file in Windows with a batch script

One part of my job means working with a CMS (content management system) to add some functionality to a site with custom code.

Without having source code access this can mean easily breaking things on the backend admin section because the WYSIWYG (what you see is what you get) editor can strip away parts of the custom code as soon as you switch off the HTML view or even just load the page because code view is switched off by default.

I wanted to encourage frequent backups of files which are edited because sometimes minor edits might be required when I am not around to fix things that could potentially go wrong.

Version control such as GIT for example was a bit too much of a learning curve for the people concerned, so I wrote up a batch script that would allow a user to copy a file into an archive directory before making edits to the current working version.

I also wanted the bath file to work regardless of where you move it to, so as long as there is a directory called Archive in the same directory as the batch file it would work.

What I came up with was a solution that would only require a user to click on an icon, which would open a command shell that can be used to paste the name of a file within the directory you wanted to make a copy of. Once the file name is entered they would hit enter and the current set date and time would be added as a prefix to the copied file.

Here is what I came up with:

set /p Input=Enter some text:
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%

set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%

copy "%Input%" "%Input% - %stamp%"

By saving this in Notepad or other code editor of your choice (I like Notepad++) I could save the file with a .bat extension which would create the batch file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.