Skip to content

Archive

Tag: Windows

I haven’t made much use of Windows batch files for many, many years. I just haven’t had a need for it, but I learnt a few interesting titbits of Batch file use that I never really knew before today.

Putting IFs into FOR loops
Firstly, batch files support FOR loops as well as IF statements, and therefore putting this together, I would expect this piece of code to work:

for /D %%d in (*) do (
  if "%%d" == "a" goto A 
  if "%%d" == "b" goto B

  echo "Default - %%d"
  goto CONTINUE

  :A
  echo "A - %%d"
  goto CONTINUE

  :B
  echo "B - %%d"
  goto CONTINUE

  :CONTINUE
)

At first glance this code should work. It is merely a few IF statements inside the for loop, which loop through all the directories inside the current directory, and then prints a message following the logic of the if statements.

This code fails (at least while running on Windows 7) because, while it will work for the first item, after the goto statements, the for loop decides to stop working.

To get around this, the solution I used was to move the logic inside the for loop into a separate batch file, and then using the CALL command to execute the second batch file from within the FOR loop. This works a charm

First batch file

for /D %%d in (*) do (
 	CALL SecondFile.bat %%d
)

Second batch file

if "%1" == "a" goto A 
if "%1" == "b" goto B

echo "Default - %1"
goto CONTINUE

:A
echo "A - %1"
goto CONTINUE

:B
echo "B - %1"
goto CONTINUE

:CONTINUE
)



Substring in Batch files
What I also wanted to do, was only compare a portion of the directory name, so then discovered that Batch files have a very simple method of substringing.

The basic format is %varname:~[x],[y]%, where [x] is the starting index in the string, and [y] determines the number of characters to select, which is optional

If the first parameter is positive, then it denotes the index from the start of the string, while if it is negative, it will denote the index from the end of the string.

So let us use an example

set var=teststring

echo %var:~0,5%
echo %var:~-3%
echo %var:~-5,2%
echo %var:~4%

This will output the following output

tests
ing
tr
string
Share

Microsoft gets a lot of flack for Windows. A lot of it is because they are seen as this big monolith bullying everybody else, but more importantly, despite Windows market share, it is not an overly fantastic operating system.

Windows XP is still my favourite version of Windows, despite the fact that I have been using Vista for a while now.

Vista is probably one of the worst operationg systems, in my view, to have come out of Microsoft, and has a host of irritations for me, from the UAC to the fact that it struggles to copy very large files easily. XP is a much more stable and reliable operating system than Vista, despite it being 7 years older.

Well, in 2 days time, windows 7 is being released, and I am certainly hoping that it will fix a lot of the issues that Vista had. Microsoft cannot afford to have another failed operating system on their hands.

From what I have seen, Windows 7 will be leaner, faster, less resource hungry, but also trimmed down a bit in terms of features, excluding things like a movie player and mail program. Considering the fact that I used third party apps for those type of applications anyway, they will not be missed.

The world eagerly awaits the eradication of Vista……

Share