windows scripting

I was asked the other day if I could take over a task that was running on windows machine. The sentence was hardly out of my boss’s mouth when he said that “you unix guys don’t like windows though”. Well, he is correct but perhaps not for the same reasons that he thinks (who knows how bosses really think).

I am not a big windows fan because although you can write batch scripts they tend to be fairly simplistic. When I say simplistic, I mean what is possible right out of the box without downloading third party apps, installing something from MSDN or other power tools.

The “task” that I was being assigned was about processing some data files by type every few minutes. I thought about using a for loop to process every PNG file, followed by a loop to process every XML file but the more I thought about it the sillier that seemed to me.

Fortunately there is a way to examine the extension of a file even in windows. Simply add tilde and x when looking at the variable containing the filename.

In one way, it is very unix like. The comparisons are case sensitive which is a bit of a bummer on windows, where filenames are not really case sensitive. I did have to add a bit of extra logic to deal with both the upper and lower case extensions but in a more normal environment they should all be consistent.

@echo off
cd c:\temp\dosplay
FOR /f %%i in ('dir /l /b *.*') DO (

@echo .
@echo .
@echo ext %%~xi
@echo loop %%i

if "%%~xi" == ".PNG" (
start mspaint %%i
)
if "%%~xi" == ".png" (
start mspaint %%i
)

if "%%~xi" == ".XML" (
rem start notepad++ %%i
call vi %%i
)
if "%%~xi" == ".xml" (
rem start notepad++ %%i
call vi %%i
)
@echo .
@echo .
)

In this test I am using the start command to start executables but the call command when executing other batch scripts.

The second half of the assignment was to pause for a given amount of time between processing files. On Unix or Linux I would use the sleep command, which does not exist on windows. I was actually surprised to find out that there are two different alternatives which work just as well.

The first is the timeout command. It is actually the same as the sleep command but it actually counts down on the screen.

c:\temp\dosplay>timeout 10 > output.txt
c:\temp\dosplay>type output.txt
Gewartet wird 10 Sekunden. Weiter mit beliebiger Taste. 

If this is ok in your output then just redirect this output in the same way as any other commands. When this is inacceptable them simply redirect the output to nul.

timeout 10 >nul

The second method is actually essentially the same. The waitfor command will wait for a certain about of time before simply resuming.

c:\temp\dosplay>waitfor /t 10 pause
FEHLER: Beim Warten auf "pause" wurde das Zeitlimit überschritten.

This example will wait for the pause to finish. It actually won’t finish nor will pressing any keys cause it to continue. It will simply will wait for the number of seconds given and then generate an error. It is possible to redirect this error to the nul output device as well.

rem waitfor /t 10 pause >nul
This entry was posted in Command line, programming and tagged , . Bookmark the permalink.