In this post, I will show you the power of scripting and its applications with a simple example. As there is much to cover and this is just the introductory and basic post, I will explain in more details, thus this will be longer than it was meant to be. The script I show here is one I wrote to help me update files from a list of folders into a backup location. I was constantly creating new folders and files within a location, so this script helps me save the changes I made into the backup directory. This was created during my previous job and was particularly helpful to me as it saved me time and energy. So I would like to share it and help others create their own, simple scripts. If you already have some scripting experience, then skip to section File Updater Script. Otherwise, read on!
Why Scripting?
Like me, you may also often find yourself doing repetitive and tedious work on the computer. For example, you have a large collection of files that you backup every now and then into another location, say just another local folder in your computer. Now, wouldn't that be too tedious to manually select and Copy/Paste the folders every time you want to update the files? Also, you would have to answer whether to"Overwrite", "Cancel" or "Skip" for the files that already exist in the destination location. This kind of work is tedious and repetitive. This is when scripting makes your life a whole lot easier.
Basically, scripting is writing code to tell the computer to execute tasks that the user does not want to perform manually. Scripting is simpler than writing a full-fledged software, so people with little to no technical experience can benefit from it. But how? Who does it? Take a look at Figure 1, below.
Figure 1. Command Prompt Screenshot |
Well, this is the famous Command Prompt from Windows Operating System. This terminal is like an "engine" that runs the scripts and does what it's told to do. You can easily find it by clicking "Start" and typing "Command Prompt". You may or may not be familiar with the screen shown in Figure 1; nevertheless, you could still write a simple script.
First Script: "Hallo Zusammen"
Open Notepad and type the following:
echo Hallo Zusammen
pause
Now save this in a location where you can find it later and name the file "Example.bat". If you have noticed, the ".bat" extension is short for Batch, which is the name of Windows Command Prompt interpreter. You should end up with a file like shown below.
Figure 2. Example.bat icon |
If you do not get a file as shown in Figure 2, then you might have named the file incorrectly. In this case you would have to find out how to "show the filename extension" of your corresponding Windows Operating System. You could try this link---> How to Show or Hide file name extensions. If it doesn't help, I suggest you Google it.
Now, execute "Example.bat" that you have created. If everything was done correctly, you should see a window like the one shown in Figure 1. Obviously, the path name would be corresponding to your machine's file location. Basically, you have just output a text to the terminal window! Echo just outputs whatever follows it to the Window Terminal and Pause just stops the window from closing when the script is finished running. With this introductory explanation, one can proceed to the main dish...
File Updater Script
As previously mentioned, the following script was used to update folders in a certain location to another , just like making a backup. This is particularly useful for updating files and folders instead of just copying everything every time thus, saving a lot of time. Let's take a look at the following code:
::This is a comment. Anything that start with :: is ignored by the interpreter.
::Check to see if Folder1 and Folder2 exist, otherwise exit.
if NOT EXIST "C:\Folder1" exit
if NOT EXIST "C:\Folder2" exit
::Create a mapping from D: to location C:\Folder1
::Create a mapping from S: to location C:\Folder2
::Basically D: will be equivalent to location C:\Folder1
subst R: "C:\Folder1"
subst S: "C:\Folder2"
::Change current working directory to S:
cd /d S:
::Loops through all folders within S: and check that they exist
::in D: as well. If not, create the folder.
::Notice that %%A is a comodin and takes the name of each folder
::within S: as the for loop traverses it.
for /d %%A in (*) do if NOT EXIST "R:\%%A" md "R:\%%A"
::Copy contents of C:\Folder2 into C:\Folder1 with Robocopy
::/Z means that it can use resources over a network storage
::/E means to copy subfolders even they are empty.
::/XO means that old files will be updated; same files skipped.
for /d %%A in (*) do robocopy /Z /E /XO "\%%A" "R:\%%A"::At this point it is finished updating folders.
::Change current working directory back to C:, the default.
cd /d C:::Release both mappings to free memory.
subst R: /d
subst S: /d
::As seen before this just shows a message to the terminal.
echo Update performed succesfully... Now program will exit...
pause
::Just to exit and close the window.
exit
And of course, you can just Copy/Paste this code and save it as a ".bat" file. Obviously, you will have to change the paths correspondingly. Keep in mind that you have to change Folder1 and Folder2 paths into your desired path location two times: one for the exist check and the other for the mappings. Yes, this could be simplified by using variables, but I had avoided variables to keep this simple.
Important: This script copies subfolders in C:\Folder2 to C:\Folder1
You use this at your own risk as I will not be responsible for any damage you may cause.
Lastly, I wish you can take advantage of this script and tailor it according to your needs, but very carefully. Again, this is a powerful tool; you use it at your own risk, and I will not be held responsible for any damage that the scripts may cause. Of course, this is just a skeleton and you can, indeed, add more features to it if you will. I would really appreciate if you could as well share your scripts customizations. As customary, I welcome questions and comments.
No comments:
Post a Comment