TuXHaT


Linux / sysadmin / Debian / Ubuntu / Red Hat / Centos

starting windows shell scripting

By tux • Sep 27th, 2009 • Category: Windows

I recently read blog entry on “Five Windows command prompt tips every IT pro should know”,

http://blogs.techrepublic.com.com/itdojo/?p=947

There started my desire to try win32 shell scripting. Not a full time study, just try bits and pieces which may be useful in day to day things :)

TIP1:

Everybody likes comments to there scripts. In win32 shell any line starting with :: is a comment

:: This is a comment

TIP2:

Conditional loops are favorites for all sysadmin in their daily tasks. For eg: one would use a for loop in bash to list a directory and do some stuff on the files and sub-folders in it. Like below,

for n in * ;do echo $n; done

This will display the contents of current working directory.

How will you do it in a windows shell ? Here it is,

Try this in a windows command prompt,

:: echo off will disable echoing the commands back to stdout
  @echo off
:: Here %V is a single letter replaceable variable
  FOR /F "tokens=*" %V IN ('dir /b .') DO echo %V

If you are putting the command in a batch script %V should be replaced with %%V.

Try out another interesting eg:

 FOR /L %i IN (0 2 100) DO echo %i

Which will simply, echo all the numbers in between zero and 100 inclusive, incrementing by 2 each time.

Try ‘for /?’ for help page :)

Tagged as: , , ,

tux is
Email this author | All posts by tux

One Response »

  1. Hi!

    Nice one

Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image