Difference between revisions of "Batch File Scripting Tips"

From WPKG | Open Source Software Deployment and Distribution
Jump to: navigation, search
m (added bullet points)
(Use of User Shell Folders variables)
Line 8: Line 8:
 
* The Windows shell and some command-line programs don't like spaces appearing in file or directory names. For example when using such wildcards as %PROGRAMFILES% in %PROGRAMFILES%\some-directory\some-program.exe where we don't know whether or not %PROGRAMFILES% contains a space, use "%PROGRAMFILES%\some-directory\some-program.exe"
 
* The Windows shell and some command-line programs don't like spaces appearing in file or directory names. For example when using such wildcards as %PROGRAMFILES% in %PROGRAMFILES%\some-directory\some-program.exe where we don't know whether or not %PROGRAMFILES% contains a space, use "%PROGRAMFILES%\some-directory\some-program.exe"
  
 +
* It is possible to load special variables from registry "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders":
 +
** It needs [http://gnuwin32.sourceforge.net/packages/gawk.htm gawk] (in %SOFTWARE%\bin for the following code)
 +
** The front and trailing whitespaces are removed ( gsub(/^[ \t]+/, "",$X) and gsub(/[ \t]+$/, "", $X) )
 +
** The spaces in variable names are removed: "Local Settings" => "LocalSettings"
 +
 +
<source lang="dos">
 +
@echo off
 +
 +
SET QUERY_HIVE=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
 +
SET WPKGBIN=%SOFTWARE%\bin
 +
 +
for /F "usebackq skip=2 delims==" %%G IN (`reg query "%QUERY_HIVE%"`) DO (
 +
REM Possible use of tokens=1,2 to avoid 2 for loops ?
 +
REM  echo %%G | %WPKGBIN%\gawk -F"REG_EXPAND_SZ" "{gsub(/[ \t]+/, \"\", $1);gsub(/[ \t]+$/,\"\",$1);gsub(/^[ \t]+/, \"\", $2);gsub(/[ \t]+$/,\"\",$2);print($1\",\"$2)}"
 +
  for /F "delims=" %%I IN ('echo %%G ^| %WPKGBIN%\gawk -F"REG_EXPAND_SZ" "{gsub(/[ \t]+/, \"\", $1);print($1)}"') DO (
 +
    for /F "delims=" %%J IN ('echo %%G ^| %WPKGBIN%\gawk -F"REG_EXPAND_SZ" "{gsub(/^[ \t]+/, \"\", $2);gsub(/[ \t]+$/,\"\",$2); print($2)}"') DO (
 +
      set %%I=%%J
 +
REM      echo  %%I=%%J
 +
    )
 +
  )
 +
)
 +
</source>
 +
 +
To be use like this:
 +
<source lang="dos">
 +
@echo off
 +
 +
IF "%Dektop%" == "" CALL %SOFTWARE%\bin\load-vars.cmd
 +
 +
echo "My Documents are located at %Personal%"
 +
</source>
  
 
[[category:Documentation]]
 
[[category:Documentation]]

Revision as of 10:56, 8 March 2010

Here are collected various batch file scripting tips for use when writing .bat files for use with WPKG.

  • If a batch file gives a degree of output to the screen it can overwhelm WPKG and the batch script hang. Lessen the output of commands by appending each with ' 1>NUL 2>NUL'. This will prevent commands echoing their messgaes (from use of the echo command), results and errors to the screen and thus to WPKG.

Also, begin a batch script with '@echo off' so that the commands themselves aren't displayed.

  • Where a % character references a variable from within a batch file, where a single % can be used from the command-line a double %% has to be used from within a batch file.
  • The Windows shell and some command-line programs don't like spaces appearing in file or directory names. For example when using such wildcards as %PROGRAMFILES% in %PROGRAMFILES%\some-directory\some-program.exe where we don't know whether or not %PROGRAMFILES% contains a space, use "%PROGRAMFILES%\some-directory\some-program.exe"
  • It is possible to load special variables from registry "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders":
    • It needs gawk (in %SOFTWARE%\bin for the following code)
    • The front and trailing whitespaces are removed ( gsub(/^[ \t]+/, "",$X) and gsub(/[ \t]+$/, "", $X) )
    • The spaces in variable names are removed: "Local Settings" => "LocalSettings"
@echo off

SET QUERY_HIVE=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
SET WPKGBIN=%SOFTWARE%\bin

for /F "usebackq skip=2 delims==" %%G IN (`reg query "%QUERY_HIVE%"`) DO (
REM Possible use of tokens=1,2 to avoid 2 for loops ?
REM  echo %%G | %WPKGBIN%\gawk -F"REG_EXPAND_SZ" "{gsub(/[ \t]+/, \"\", $1);gsub(/[ \t]+$/,\"\",$1);gsub(/^[ \t]+/, \"\", $2);gsub(/[ \t]+$/,\"\",$2);print($1\",\"$2)}"
  for /F "delims=" %%I IN ('echo %%G ^| %WPKGBIN%\gawk -F"REG_EXPAND_SZ" "{gsub(/[ \t]+/, \"\", $1);print($1)}"') DO (
    for /F "delims=" %%J IN ('echo %%G ^| %WPKGBIN%\gawk -F"REG_EXPAND_SZ" "{gsub(/^[ \t]+/, \"\", $2);gsub(/[ \t]+$/,\"\",$2); print($2)}"') DO (
       set %%I=%%J
REM       echo  %%I=%%J
    )
  )
)

To be use like this:

@echo off

IF "%Dektop%" == "" CALL %SOFTWARE%\bin\load-vars.cmd

echo "My Documents are located at %Personal%"