TenMinuteTutor

Coding, maths and art

Collecting the source files

The amount of source code you have to back up on a regular basis is probably a small proportion of the total amount of files on your disk. You will no doubt have object/exe/class/jar files, open source programs, cache files, downloads which are throwaway files and don’t need to be backed up (they can easily be recreated or downloaded). You might also have purchased items (software, music etc), legacy projects, holiday snaps which you want to back up but only when they change. They do not need to be part of your regular backup.

You can make your life a lot easier if you separate these items out in your filing system, in particular have a folder on your disk which contains all the items which need regular backup and nothing else.

Here is a simple batch file which copies all the files from your c:/source folder to a backup folder:

set d=%date:~0,2%%date:~3,2%%date:~6,4%
set f=backup_%d%
xcopy  C:\source\ c:\backup\%f%\source /S /Y /I

This basically copies all the files and folders in your source area (c:/source) into a folder c:\backup{date}\source. Obviously, you should change the drive and folder names to suit your setup.

xcopy is similar to the standard copy batch file command, but with more options. Here we use 3 flags:

  • /S copies all the files and folders in your source. If you didn’t use this, only the top level files would be copied, the sub-folders would not.
  • /Y overwrites files without asking for confirmation. You could leave this out, but if you sometimes need to update a backup before you commit it to storage, this is a useful flag.
  • /I automatically creates sub-folders in the destination area.

You will notice that the batch file displays all the files it is copying, scrolling up the screen. For me, this is useful feedback (no, I can’t read that fast, but I would notice if nothing at all came up), but the /Q flag will turn this off if you wish.

Finally, if you cannot separate your source files from your throwaway files, you can tell xcopy to not copy certain files. You do this using exclude files (type xcopy /? at the command line and look at the EXCLUDE option).