Following tips are useful while scripting in Windows

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

Credits

Windows command prompt tips
Tagged on:                                                                     

One thought on “Windows command prompt tips

  • February 22, 2016 at 4:31 am
    Permalink

    I was looking evwrheyere and this popped up like nothing!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Recommend on Google