PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Stop The Internet Filter!

   No Clean Feed

   The Internet Filter Is An Ex-parrot!






PGTS Humble Blog

Thread: Microsoft (Decline Of)

GP JPG
If at first you do not succeed ... You must be a programmer!

Windows CMD scripts


Chronogical Blog Entries:



Date: Sat, 31 Oct 2015 23:00:00 +1100

Windows cmd scripts

As of Server 2003, windows CMD scripts support nested if statements. However if you sprinkle lots of comments in your code (always recommended) you might get caught out if you put comments in the nested block.

For example the following block of code behaves as you would expect:

@echo off
if exist foo.bar (
    echo Found foo.bar
    echo everything is ok
)

Found foo.bar
everything is ok

But the code block below gives a rather strange error:

@echo off
if exist foo.bar (
    echo Found foo.bar
    echo everything is ok
    :: A comment
)

) was unexpected at this time.

The following code block gives a different but equally cryptic error:

@echo off
if exist foo.bar (
    echo Found foo.bar
    echo everything is ok
    :: A comment

)

The syntax of the command is incorrect.

It seems that if you put a comment before a closing ')' the CMD script will not be parsed successfully by the shell.

The following are a few examples of the for loop that is built-in to the command shell. This has been around for quite a while. But if you are used to the relatively sensible logic of traditional Unix commands, you will probably find it difficult to get your head around the arcane syntax of this Windows built-in. It can take a long time to craft a for command, but once you have, the command should be reliable and robust for a specific application, but not very versatile. The following code is built to be run in a CMD script (hence the double '%' characters):

@echo off

:: Non aligned directory style listing of files in current folder.
echo.
echo Example 1
for /f "usebackq delims=" %%x in (`dir /b`) do @echo  %%~ftzax

:: show a long directory style listing of files (in current and sub folders) more than 1 day old...
:: (Assumes you have Strawberry or ActiveState perl)
echo.
echo Example 2
for /f "usebackq delims=" %%x in (`dir /b /s`) do (
        @perl -e "printf \"%%s %%9.0f %%s\n\", \"%%~tx\", %%~zx, '%%x' if ((-f '%%x') && (-M '%%x') > 1)"
)

:: Search current folder and sub-folders
:: if the file is more than 90 days old do something
:: Otherwise do something else
:: Note: This is very slow compared to Unix
echo.
echo Example 3
for /f "usebackq delims=" %%x in (`dir /s /b`) do (
        perl -e "exit 1 if ((-f '%%x') && (-M '%%x') > 90)"
        if ERRORLEVEL 1 (
		@echo %%x might have something done to it
	) else if ERRORLEVEL 0 (
		@echo %%x might have something else done to it
	)
)

echo.
echo Example 4
:: Find all folders that contain the word "sent" (plus a trailing ".") in the folder Q:\data 
:: Can't anchor the search string with '$' ... So assume that '.' is end of line
:: Would be broken if any folder begins with '.' after Sent\
set tmpout=C:\Temp\tmpout.tmp
copy nul %tmpout% >nul 2>&1
echo Searching for folders ...
for /r Q:/data %%x in (.) do (
        @echo %%x |findstr -i -r sent\\\. >> %tmpout%
)
for /f "delims=" %%x in (%tmpout%) do (
        echo %%x
)

The following is an example of the forfiles command which is (very) roughly equivalent to the Unix 'find' command. ... It has only been around since Vista, but was long over-due. At last you can search for files based on age without turning your brain inside-out trying to comprehend the weird backwards logic of the Windows for built-in.

@echo off
:: find all files older than 60 days
forfiles /s /m *.* /c "cmd /c echo @fdate @path" /d -60

Other Blog Posts In This Thread:

Copyright     2015, Gerry Patterson. All Rights Reserved.