Search This Blog

Wednesday 26 September 2012

The Ultimate Guide To Batch Scripting From Basic To Advanced (Windows Programming Language). PART 2

The Ultimate Guide To Batch Scripting From Basic To Advanced (Windows Programming Language). PART 2

Start Command + How to Open up a Website

Ever wanted to make a program open using your batch file? Well, all you need to do is use the start command! But this command can get confusing. You must add file paths to make the program start up. Most default programs (Notepad and Internet Explorer) don't need additional file paths. You just need to use the name of the file. 

So for example, I will open up notepad.

Code:
start notepad.exe


If I wanted to open up Internet Explorer, you would use this:

Code:
start iexplore.exe


If you wanted to open up a specific website you would use this (but with the link changed):

Code:
start iexplore http://www.google.com



Now, what if you wanted to open up a picture? You need to find the location of the file. So how do you do this? Well, all you need to do is right-click the file and select "Properties." Then look for the file location.





Ok, so I now know where it is located. THAT IS NOT ALL YOU NEED! You also need the file name with the extension. The file I was looking at was name "2" and it was a .png file so I would add "2.png" to the end of the directory we just found. So, if I wanted to open up that specific picture, I would use:

Code:
Start C:\Users\Chamantha\Desktop\2.png


Loops

These are pretty useful when you think about it. Ever wanted your batch file to do something forever? Instead of copying and pasting lines over and over again, you can just use a loop.

All you need is two commands to make a loop. Those two commands are the label (:x) command and the goto command. So let me show you an example.

Code:
@echo off
:A
echo HELLO!
goto :A


Ok so the ":A" is a label. You use the colon (:) and then name the label. I named my label "A." There, I just set the start of the loop. Then where do I close the loop? Well, you just use the goto command. I inserted goto :A because I want the program to go to A. And "A" was set with the label. So when the program gets to "goto :A" it will go back to the label "A." So whatever code is inside the loop will repeat over and over again. So in the example I showed you earlier, the batch file will display "HELLO!" over and over again.

Now this command has a dark side too. Be careful. For example, if you make the batch file start notepad over and over again, your computer will eventually crash. The example I showed you is harmless.



User Variables/Interaction

Now this is one of the most advanced set of commands that I will teach you. Get ready! Pirate
I believe that examples help you learn more than a lesson. I will combine both so you get the best of both worlds. So first off, what is a user variable? Well, think of it this way. It is a set of commands that allow user input. It is actually pretty cool. Since this is one of the hardest commands to learn by hand, I decided to make you guys a video. Trust me; a visual of user variables is much easier to understand.

 
 




Commands Covered in this Tutorial:


set /p
if
goto
marker/label
exit


Dangerous Commands

There are some commands that are pretty dangerous. In this section we will cover the del (delete) command and the ren (rename) command.

 
Del (Delete)

This command is really easy to use. If I wanted to delete that photo that I opened earlier (C:\Users\Chamantha\Desktop\2.png), I would use this:

 
Code:
del C:\Users\Chamantha\Desktop\2.png

 
And if I ran a batch file with that command, the picture would be deleted.

 
Ren (Rename)

What if I wanted to rename that file instead? Let's pretend I did not delete it. If I wanted to change the name of a text file from 1.txt to 2.txt, I would again need to find the location of the file. Right-click your file and select "Properties."







Then at the end of the directory you just found, add the name of the file + the extension. So my file location would be:
 
Code:
C:\Users\Chamantha\Desktop\1.txt

  

There, now I just got the location of the file. Now how do I rename it to 2.txt? Well, in my situation, I would use this:
 
Code:
ren C:\Users\Chamantha\Desktop\1.txt 2.txt

No comments:

Post a Comment