Main Page   Related Pages  

Application in batch files

So far, all usage examples have only displayed the contents of the system folder of interest on the console.

But how to make use of that information from within a batch file?

The key for doing this is environment variables.

In order to use the result of SFP in a batch file, you have to manage putting the displayed information somehow into an environment variable.

However, for security reasons, it is not possible for a Win32 program such as SFP to directly modify the environment variables of the batch file it has been called from.

That is, SFP cannot just put its result into some environment variable, because Windows simply does not provide this functionality.

The most simple other alternative is for the batch file to "catch" the output of SFP and assign it to an environment variable.

Prior to Windows 2000, there has only been a single easy way to do this: A temporary batch file fragment had to be created, just containing a SET command but not its value, such as


and that file must not have been terminated with a newline sequence.

Let's say this fragment is stored in file temp.tpl.

The variable SFP could then be set using the following script:

 REM create temporary script from template file
 COPY temp.tpl temp.bat
 REM complete the SET-command
 SpecialFolderPath CSIDL_PROGRAM_FILES >> temp.bat
 REM execute the SET-command
 CALL temp.bat
 REM remove the temporary file
 DEL temp.bat
 ECHO This is the "Program Files" directory: %SFP%

Although this still works, since Windows 2000 there is a better and easier way to do it:

 FOR /F "usebackq delims=" %%A
 ECHO This is the "Program Files" directory: %SFP%

(Note that the FOR and IN lines had to be broken down for display reasons here, actually they belong to a single line.)

How does this work?

The above script code uses a new feature of the Windows 2000 / XP command interpreter, called backquote evaluation.

Actually, this feature is an enhancement in the FOR command, and works as follows:

The /F "usebackq delims=" option instructs FOR to enable the new feature, and refers to the command portion enclosed within the parantheses following the IN.

Normally, that portion is a list of tokens or file names to be assigned to the FOR variable in order.

But when backquote evaluation is used, FOR expectes an external command enclosed within backquote characters instead.

FOR then executes this command, and assigns each line of text output by that command to the FOR variable in turn, and executes the loop statements after DO for each value.

As SFP only outputs a single line of text in that case, the SET command inside the FOR loop will also only be executed once.

And as this SET command assigns the current value of the FOR variable to the environment variable %SFP%, its value will be available even after the FOR command has completed.

Admittedly, this use of the FOR command is very special and not paricularly easy to remember.

For that reason, the usage help of the SFP command will show an usage example appropriate for this case.

SFP indends to be self-explanatory to the developer once he/she has read this manual, and not to force anybody to carry around separate documentation files.

So, normally the SFP executable with its built-in usage help will be everything you need.

Next: Usage

Generated on Mon Jul 21 11:11:35 2003 for SpecialFolderPath by doxygen 1.3.1