AdamTheTech Logo
AdamTheTech
Enthusiast of Technology, Web Development, and Sci-Fi

Microsoft’s Free Virtual Machine Images

Deus Ex: Human Revolution Review

Embed dll Files Within an exe (C# WinForms)

Create a Simple Backup System

Please be aware that this entry is over two years old. Therefore, it may contain broken links, outdated information, or views and content which are no longer completely valid.

If you’re like most computer users these days, backups are nearly non-existent in your little digital work area.

Here’s something that will take maybe 15 minutes of your time to figure out, which will allow you to backup a good portion (or all; if you want to go that far) of your stuff. The time investment is nil for such a large benefit, so there no excuses! Especially since this will be free, unlike Acronis True Image (which is more than worth the $40-$50, by the way).

So, are you ready? Here we go.

Perhaps you’ve heard of batch (or .bat) files. Perhaps not. Either way, now you have, and they are simply executable files where you can write a whole collection of commands based on the Windows CMD command line. Instead of entering commands one at a time into the command line window, you can put them in a batch file, and a simple double-click on the file will run your little program.

All you need to get started in notepad and WinRar. A shortcut to opening notepad is as follows: Hit the Windows Key and the letter “R” at the same time (Windows+R), and type this in the Run box and hit enter: notepad

This will bring up notepad so you can begin writing your batch file. In order to save a batch file, go to “File” > “Save As”, and type a file name followed by this: .bat

This will create your batch file. Since double clicking on it executes it, right click on it and click “Edit” to open it up in notepad should you happen to close the notepad window.

Now, first type the following at the top of every batch file you create:

@echo off

Next, title your batch file so you know what it is when it runs. Just type “title” followed by whatever you want. Since what I’m going to show you will involve backing up “My Documents”, name it something along those lines.

title My Documents Backup

Next, we’re going to issue a command to copy the entire contents of “My Documents” to a separate location. This is so none of your data gets messed up during the backup procedure and makes sure there aren’t any open files. Just note that the location of your “My Documents” folder may (and will probably) be different, so plug in the directory path you need. If you don’t know where to look, simply do a file search for “My Documents” and copy down the file path in your batch file. So, here’s the command:

XCOPY "C:\Documents and Settings\Adam\My Documents " "C:\backups" /y/s/e/c/i

This will make a copy of the “My Documents” folder in the “backups” folder on your C: drive. If you have a separate or external drive you want to use instead, feel free. The copying process will most likely be a bit faster.

So, the next thing we want to do here is put all the files in a nice, neat little package. And maybe compress everything to save some hard drive space. This is where WinRar comes in. If you’ve never used WinRar, it’s similar to how you use zip files. If you’re not familiar with either, it’s used to compress files to save space for storage, sending over the net, or downloading. Anyway, here’s the command on how to RAR your files:

"C:\Program Files\WinRAR\WinRAR.exe" A -r -M3 -MD4096 -Y -O+ -EP  -ac -isnd -vn -ep1 "C:\backups\my_docs_backup.rar" "C:\backups\My  Documents"

Looks a bit complex, doesn’t it? Let me just break it down.

This is the location of the WinRAR program:

"C:\Program Files\WinRAR\WinRAR.exe"

This tells WinRAR what to do with your files. These are called command switches. They’re quite useful once you learn how to use them, but for our purposes here today, let me just say it tells WinRAR to put your copy of “My Documents” in one file, compress it, and retain the directory structure (this is so when you want to look for something or restore a folder or all folders, it looks exactly like the “My Documents” folder you’re backing up).

A -r -M3 -MD4096 -Y -O+ -EP -ac -isnd -vn -ep1

Next we have the path of where you want to store the compressed (or rather, “RARed”) archive file, plus the .rar file’s name.

"C:\backups\my_docs_backup.rar"

Last, but not least, the path of the folder you are backing up. In this case, it’s the copy of the “My Documents” folder.

"C:\backups\My Documents"

Now, you probably don’t want to keep that extra copy of “My Documents” kicking around on your drive taking up space, so we’re going to have the batch file delete it as soon as it has made the backup:

RMDIR "C:\backups\My Documents" /q/s

…and that’s it! The complete contents of your batch file should look something like this:

@echo off
title My Documents Backup
XCOPY "C:\Documents and Settings\Adam\My Documents " "C:\backups" /y/s/e/c/i
"C:\Program Files\WinRAR\WinRAR.exe" A -r -M3 -MD4096 -Y -O+ -EP  -ac -isnd -vn -ep1 "C:\backups\my_docs_backup.rar" "C:\backups\My  Documents"
RMDIR "C:\backups\My Documents " /q/s

All you have to do now is save the file and run it to see if you managed to follow through on everything correctly. Chances are if this is your first time working with batch files, it won’t run 100% on the first try. You may have to fix a couple things. Since all you’re mainly working with here are the file paths, chances are you typed something in wrong. If you get lost or frustrated at this point, feel free to search with Google, post on the PCM forums, or post in the blog here.

I hope you find this quite useful! If you want a little bit more information on some of the details I merely glossed over, take a look at this: (broken link–original content as follows), or continue to Part II here.

Scheduled Tasks and Batch Files by David Risley (10/23/2003)

Windows has a feature known as Scheduled Tasks which can be quite useful to people. Most people don’t use it because they don’t know how or can’t think of a way to use it. But, I assure you, if your PC is an integral part of your life (as mine is due to my line of work), taking advantage of Scheduled Tasks can do a lot of grunt work for you.

Basically, Scheduled Tasks is a way for Windows users to schedule certain programs to run automatically at certain times of the day. You can do things as simple as start up an application automatically at a certain time. Some people use this to automatically start up their email application before they get to work so that their email is sitting there when they arrive. Scheduled Tasks can also be used to shut down an application after a certain amount of time. The real power of scheduled tasks, though, is recognized when you couple it with custom scripts and batch files.

Now, the non-programmers out there are probably thinking this is too far out of their league to use. But, I assure you it is not. Batch files (files with .BAT extention) are simply files which contain commands that would usually be executed one-by-one via the command line in DOS. You can move files around, delete directories and perform other file operations by command line, and through the use of scheduled tasks, you can do it automatically.

I’ll give you an example that I use. I leave my PC on all the time. Like most people, I tend to be asleep at night. So, things that need to be done but I don’t want to wait for (such as backups) I write batch files for to run them automatically. So, during the week, my PC is fairly busy between the hours of 2AM and 8AM. Every other day, I run a series of batch files which automatically RAR my important files into easily managed RAR archives. (For those of you who don’t know, RAR is a file compression technology similar to ZIP files.) I perform some file operations, and then take advantage of the command line interface of WinRAR to Rar my files into an archive (I will provide the command lines below). Then I use the command line interface to my FTP program to upload the file to a remote location.

So that gives you an example of what you can do with Batch files and Scheduled Tasks. Now let me give you some how-to. Here are a few commands which are useful for file operations:

XCOPY: Used to move entire folders or even entire drives from one location to another. There are many “switches” which can be used on this command. You can view the full reference to the command here (alternate link here). A sample of a command from my batch files is:

XCOPY "F:\pcmech" "E:\Backups\Websites\PCMech" /y /s /e /c /i

This copies the files for PCMech on my PC from one location to another.

RMDIR: Used to remove a directory. I use it in my batch files to clean up after a backup. A reference can be found here.

RMDIR "E:\Backups\Websites" /q /s

COPY: Used to copy a file from one location to another.

COPY /y "fromfile" "tofile"

One other command might be useful. Its available on Windows XP. taskkill is a command which is used to end a Windows process by command line. In batch files used for backups, you might need to use this in order to close a program so as to free the file lock which that program has on its data files. I use it to close down Outlook before I can backup it’s data file. I run another scheduled task later in the morning to re-start Outlook so that my email is there when I sit down at my desk.

Now, for WinRar. WinRAR is a shareware program used to create or uncompress RAR files. It has a Windows interface which most people are accustomed to. But, you can also use it via a command line interface. I use it to create my RAR archives. Most people spend a lot of time trying to get these commands right because it is not very intuitive. I will provide you with my command which hopefully will serve as a helpful reference for people if they care to use this method of backups or file management:

C:\WinRar\RAR.exe A -r -M5 -MD4096 -Y -O+ -PUSER -EP -ac -isnd -vn -ep1 "E:\Backups\backup.rar" "E:\Backups\Backup Files\*.*"

The meaning of many of these command switches is in the WinRar help file. This command will run the RAR.exe file (the command line module to WinRar), create a RAR archive called backup.rar and place into it all of the files and folders located in the “Backup Files” directory. “USER”, in this case, would be the password on the archive.

The purpose of this article is simply to give you something to go by in creating your own batch files and taking advantage of scheduled tasks. I fully realize that some of this might seem too complicated, but I assure you, a little trial and error and playing around with Batch files to do some of these things will teach you how to use them. Believe me, we all flounder around at the beginning to make sure these batch files do what they are intended to do. I spend many hours getting my various batch files to do what I intended them to do. That WinRar command line above took a good friend of mine hours to get right (hopefully my publishing it here will save you some real time).

(Originally published on a now-defunct blog)