Windows batch scripting (part 1)

December 4, 2009 at 7:09 am | Posted in Uncategorized | 1 Comment
Tags: , ,

Sometimes, the GUI just isn’t enough. This is true in Windows as much as it is in Linux. Note that I will be using cmd.exe and not the new (and improved) PowerShell. PowerShell is a lot like Linux, so I’ll be talking about that when I learn how to use bash😉 .

Batch scripting can be very simple, very complex, or anything in between. For example, to simply show something on the screen it is sufficient to use the command echo. However, simply doing that won’t give you time to see your output. So, you have to put pause at the end. Like such:

echo Good Morning
pause

Note that I didn’t use quotation marks with echo, since echo literally outputs everything after its command (with the exception of what comes later).

However, if you execute this, you’ll see that every command is shown, then executed. This isn’t what we want. To turn off the output of commands, we’ll use @echo off. Now, take a look at this next script;

@echo off
echo press any key to hack the system..
pause
cd C:\
dir /s
pause >nul

Cool, huh?🙂

Like I said earlier, the first line turns off the output of commands. The second line shows something to the screen. Then we wait until the user presses any key. After that, we learn something new. The cd command changes directories, thus goes to another folder on you pc. Then dir shows everything there, including the subdirectories (with the /s argument). Then the last line waits for a key, but shows nothing. Technically, we redirect pause to nul.

And there’s many other cool uses for redirecting. See, I don’t go to school to script jokes only😉 . I’m taking this course to become a system administrator. A cool use for redirecting something, for example to a text file, is to log which user logs on when. We’ll use the time and date command for this. However, we don’t know what the command to show date and time is. We have to find this out somehow.

Normally, I’d use the man command. But this isn’t linux, this is Windows. And so, to make everything more difficult, we use the /? argument instead of man. For example:

time /?
date /?

As such, we find out that the /T argument shows the time and date with these commands. On a side-note, if anyone knows what the T stands for, please tell me in a comment. My teacher tells me Windows uses dutch (Toon = Show) for its command interpreter, but I doubt that😉 .

Alright, now we’ll put this to good use;

time /T >log.txt
date /T >log.txt
echo ———————– >log.txt

So now, the log.txt file always contains the date and time of the last login. However, how do we know which user was logged on? Well, we’ll use environmental variables for this. Like so:

echo %username% >log.txt
time /T >log.txt
date /T >log.txt
echo ———————– >log.txt

There, now the log.txt file always contains the username, time and date of the last user who logged on. Pretty cool, isn’t it? However, what if we want to store ALL the users who logged on? Well, we have to make a little change to the redirect command. As it is now, the commands write something to  a text file from beginning to end. We need something to write something to the end of the file.

echo %username% >>log.txt
time /T >>log.txt
date /T >>log.txt
echo ———————– >>log.txt

Easy, wasn’t it? Well, we accomplished what we wanted to accomplish this time. Hopefully, I’ll be able to post more of this. If not, it means I failed my exams!😀

Until next time!
Armor Nick out.

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. By the way, this may also come in handy for more information about batch scripting: http://commandwindows.com/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: