Windows Batch Scripting for Beginners and how to use it.

What is a Batch file?

Most people will never really understanding what the extension .bat is in Windows or even DOS.  However it is something that is native to programmers around the world because it is the first thing most people who make programs or works with scripting start to use way before they do anything else.  It should be considered the first linguistic languages that is very powerful.  It is designed to be used in DOS but can also be used in Windows as a DOS command easily and effectively through Windows OS.   Some of these are what you should explore even more to learn how to program and create scripts even more.  However this blog post is just a introduction into doing it and how you can use this in your everyday IT Job.

According to Wikipedia:

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching.

Understanding scripting is the first part in creating powerful scripts for everyday use.  However, I am one of the few to use the help command in DOS:

HELP [command]

This is a good tool for those who might want to explore the power of using a Batch to get your job done or to creating a batch file because you will need to know what you can use in the batch file.

Here are a few commands you might want to use in a batch file:

  • SET /P variable=[promptString]
  • Start of the batch file ECHO OFF and ECHO ON after the batch file is complete.
  • If and THEN (Wikipedia)
  • CALL filename.bat

Creating Batch files

I’m not going to go into much details about creating the batch in the way you might need but I will be talking about this in general about ways you can create a batch and which programs are good at helping you create a batch file.

First you need a program to create it.  You can always use Wordpad or Notepad but you could find it isnt’ a good as Notepad ++.   I have used it in the past for programming in many different programming languages such as Pascal, C++, COBOL, and SHELL Scripting.  I’ve talked about SHELL scripting in past for LINUX which you can use Notepad++ for it also to create Linux shell scripting.  I will say you can do a lot with the open-sourced program that is free to use.  So I always want to use this with anything I am doing because it shows you where you might have a problem in your scripting language and helps you identify how to fix it.  You’re best advice is to just try to think about what you want to accomplish with a batch file and start thinking logically about how you would do that.

You will need to make sure the .batextension is on the file name.  Make the filename something that you can easily run in DOS because you don’t want to make the batch file anything complicated because that might not work properly if you use a call command in another batch file.  So consider the naming convention of your batch file(s).

Trial and Error

After creating the batch file you will need to do some trial and error test to make sure it will work with what you need to use it for and also make sure there isn’t something unexpected.   This is what programmers do all the time by compiling  the program and running it on different systems to see what it will do in different Windows environments.

Finally, You will see that you can do so much more with a batch that you previously did and it can be a great asset in doing repetitive stuff at work or at home.   If you do this right it can even help speed up your life.   Here are a few Blog posts that I have talked about previous:

As you can see these will help you understanding scripting but also help you to figure out what you might want to do with a Batch and why you might need to create a batch file.  Even I know this is only a few blog post that will at least hopefully give you even more ideas on what will help you to create batch files.

Don’t forget to like and subscribe to my blog page for even more updates.   Do you have a favorite DOS command?  why not share it with others so they can learn even more.

3 Dos commands that you need to know!

DOS Rocks!

Direcotry of System or Disk Operating System (DOS) is one way the technology is good at fixing certian issues. I am going to talk about some that you might or may not know about.  Each command is good for those who are connected to through a network or network group.   If these system are on the same workgroup and are on the shared through the network this will help you in solving those really problems.   You’ll love these as much as I use them because they are really good at keeping you looking like a genius.

FSUTIL (Microsoft learn)

C:\fsutil volume diskfree \\(Shared drive)  

You’ll need to add the \\ as the shared drive or shared system that you want to know how much space is left on it if you need to know at a quick glance will this file or program fit on this drive.   It’s very useful for those times that you may need to keep track of the space.

QUERY (Microsoft Learn)

query session /server:computername

query users /server:computername

one of thse can be used to determine if there are users who might be causing problems by being logged in but aren’t actually doing anything on the system.   These users could be from a previous shift or might of been logged in for a while without any activity.  So this helps to know if a program or access is causing another user from not being able to do their job one way or another.  This is great tool for corporations where more than one users could be using the system at one time or another.

LOGOFF or SHUTDOWN

logoff [<sessionname> | <sessionID>] [/server:<servername>] [/v] (Microsoft Learn)

shutdown /l /r /m:(Computername)  (Microsoft Learn)

Each one of these commands does about the same thing but one reboots the system also because you never know when the remote system might need to be rebooted also after forcing the user to logoff.   So you can decide which one you might need to user remotely for a system.   You can even add in comments or tell the reason why the system rebooted with the shutdown command by adding  /d [p : u](Planned or Unplanned with a number 1-256) .   I will remind you to be cautious as to what you put down as the reason and you can even specifiy how long to wait before the system reboots by adding /t 0-256 MINUTES.   Each one of these command can be helpful for you when you are trying to solve some minor major issues with a system not working properly.

DOS is Powerful

Wheather you want to believe it or not, DOS can be a very useful tool for those who might need to fix issues with the system like windows and other issues that you might need help to fix certain issues that might arise on the system.   You can find some great useful tools at the DOS prompt but you have to be willing to learn it even more than most because it is so much like Linux.   I definately say old and new techs should know how to do this and use the dos prompt when you need to fix issues.  Do you have a favorite command prompt that you use?   Why not leave a comment and tell people about the command that you think is the most useful command?   I’d like to hear it and learn even more about the power of using the DOS prompt.

Understanding Shell Script in Linux Mint and how to use it properly

 

Shell script

Shell scripts are powerful!

Any Admin who has used shell scripts to complete a bunch of tasks on Linux will tell that you should already be using it.  It’s something that even I can’t really understand but know a lot about since I have to install Linux a lot.   Sometimes it can go weeks before I have to use my shell script again or the next day.  It varies from time to time when I will need to use it but it is an admins best friend.   Is shell scripts like python, absolutely but there is something that we should at least explore and understand.   I am going to talk about this and give examples for those who might want to follow along with me as we talk about “What is shell scripts and how can I use them?”.  I am sure this will be a long series maybe a few post here and there but it will be really good for those who might want to learn even more about the world of Linux.

Shell scripts vs python

Let’s at least get this one out of the way, because someone is bound to ask this question.  What’s the difference in shell scripts verses python?  In the short answer not much, but in the long answer the python language is more complex and you can do a lot more with the python did you can do with shell but only to a point.  Each one has it’s own unique qualities and can be used on Linux and Windows.   Although shell scripts can’t be run on exactly, they can be renamed to be batch files (.bat) which is DOS(Directory of System) old system.  Way before Windows we had DOS and way before any GUI interfaces we had to create batch files to load up the system.  If you’re old enough you probably remember autoexec.bat or even the config.sys files that you occasionally had to edit to get to the command prompt.

Shell scripts in Linux

What’s the use of using shell scripts in Linux you might be asking yourself?  It’s very useful to use when there is a problem or need to install a bunch of things that would take longer to install individually.  Shell script basics are really just that like a batch file in DOS and it can do a lot of things from asking a question to installing many different files.   It can rename or remove files, it is also good for cleaning up a directory that might be full of junk files or maybe you want to have a shell script to backup your /home directory.  This is the power of shell scripting in Linux.  Shell script basics can make your life so much easier and better in the long when you finally decide to delve into the Linux system.

Shell scripts If and then

Just like batch files, you can use this in Shell scripting.  If something happens then do this or you can even go farther that by using Else statements also.  You can ask something from the admin like where the file location of a file you want to use.  It can make your life so much simpler and can help keep the Linux system running smoothly.

if [ expression ]
then
statement
fi

You can do so much more with the shell script when you personalize it to what ever you might need.

echo What is the shell script you want to run?
read $FILE
if ! [ +r $FILE ]; then
Echo "Admin permissions are required to run this shell script"
then exit 0

As you can see this can be very useful when you want to get around certain installation situations and needing to install files without prompting.

Here’s a good one for those who might need to do just that:

echo y|sudo apt-get install [apt program]

This will allow you to bypass the yes question when it is asked and you can just create a basic shell script to install the files that you wanting to do like I have from time to time with gaming systems.   If you don’t want to wait around and install them manually this is the best approach for people who might want to multiple programs at once.

Executing the shell script

if you can’t execute the shell script than you haven’t given permissions to execute it in Linux.  You either have edit the properties in Linux, by right clicking on the shell script and clicking on the properties tab.   Then check “Allow executing file as a program” or you can do a command line

chmod 0700 script.sh (only allow owner to run script)
chmod 0755 script.sh (Allow owner users, and groups to run script)

As you can see the chmod command can be very useful in Linux, so take care to use it wisely.   I will talk about even more about shell scripting in the coming days and hopefully provide you with a very powerful script for installing all the gaming and even the drivers that you might need to for your new Linux gaming system.

Figuring out the Email-Worm Win32.Zafi.b

This is another just I just saw on the web and wanted to talk about what this little Worm does and what it’s known Aliases:

Email-Worm.Win32.Zafi.b (Kaspersky Lab) is also known as: I-Worm.Zafi.b (Kaspersky Lab), W32/Zafi.b@MM (McAfee), W32.Erkez.B@mm (Symantec), Win32.Hazafi.30720 (Doctor Web), W32/Zafi-B (Sophos), Win32/Zafi.B@mm (RAV), PE_ZAFI.B (Trend Micro), Worm/Zafi.B (H+BEDV), W32/Zafi.B@mm (FRISK), Win32:Zafi-B (ALWIL), I-Worm/Zafi.B (Grisoft), Win32.Zafi.B@mm (SOFTWIN), Worm.Zafi.B (ClamAV), W32/Zafi.B.worm (Panda), Win32/Zafi.B (Eset)

[ad#ad2-left]This worm spreads via the Internet as an attachment to infected messages, and also via local and file-sharing networks.
It is written in Assembler, and packed using FSG. It is 12800 bytes in packed form, and 33292 in unpacked form.

This Worm seems to be running through email and file sharing sites, One thing it tries to do is stop the process and deletes:
fvprotect.exe
winlogon.exe
jammer2nd.exe
services.exe

It attempts to detect antivirus program files on the computer and overwrite them with a copy of itself.

[ad#ad2-right]It also attempts to conduct DoS attacks on the following sites:

www.2f.hu
www.parlament.hu
www.virusbuster.hu
www.virushirado.hu

This seems to be a very big virus and can be removed with the use of Kapersky Virus removal tool for free for this type of virus. In order to prevent this virus in the future the user has to remember about not getting opening unknown documents or emails and not running any unkown program from an unknown file sharing.   Also remember you need to have an anti-virus  and also a firewall to protect yourself in the future.

Find Out If Your Computer Is Secretly Connecting to the Web

If you are trying to track down why your computer is running so slooowwwly, try using this simple DOS command from Digital Inspiration to uncover a possible problem:

* Type cmd in your Windows Run box.
* Type “netstat -b 5 > activity.txt” and press enter.
* After say 2 minutes, press Ctrl+C.
* Type “activity.txt” on the command line to open the log file in notepad (or your default text editor)

http://lifehacker.com/software/windows/find-out-if-your-computer-is-secretly-connecting-to-the-web-302636.php

If you ever wondered if someone is on your system you can find out by doing this!