Redirection often involves temporary files.
Most of the examples presented on these pages use temporary files without a fully qualified path, which means they will be created in the current directory.
That may be valid for me on my own systems (being a network administrator I have full access rights in every directory), for many users trying to write in the current directory may result in "Access denied" error messages.
One workaround is using the TEMP directory to store those temporary files.
Usually the TEMP directory is specified by the %TEMP%
environment variable.
Since I once did manage a network where %TEMP%
was set to C:\
, I never assume it is C:\TEMP
, and I cannot assume that %TEMP%
will not end with a backslash ( \
)
I solve that by using %TEMP%.
(note the dot after the second percent sign) so I know it doesn't end with a backslash.
As long as the directory name specified by %TEMP%
doesn't contain a dot after the last backslash, this will work.
The ending dot will either be ignored if %TEMP%
doesn't end with a backslash, or otherwise the resulting \.\
will be interpreted as \
(%TEMP%.\
, which is identical to %TEMP%
).
If the TEMP variable is not set, %TEMP%.\
will be .\
which means the current directory.
Windows 2000's TEMP directory name contains spaces by default, which may cause problems in many "old" batch files.
The workaround is to add quotes, but only after the temporary file name has been concatenated: "%TEMP%.\tempfile"
This will still work in older operating systems.
Note: | Not all batch file examples on my site have been modified to work in Windows 2000 yet, many may still cause problems with a TEMP directory containing a space in its name. |
Using the TEMP directory doesn't always work for one type of temporary batch file.
As shown in several examples using redirected DATE or TIME commands, some temporary batch files (like CURRENT.BAT) may be called by other temporary batch files, and we have no means to add a TEMP path, since the redirected output (like "Current date is ...") was never intended to be interpreted as a command line.
This can be solved by making %TEMP%
the current directory or by adding the %TEMP%
directory to the PATH variable.
I read some discussions about what if a TEMP directory is read-only?
Well, I consider that an error, since the TEMP directory was meant as a place to store temporary files.
Most of my batch files won't work in that hypothetical situation, and I do not intend to fix that unless someone can convince me that a read-only TEMP directory is a valid option.
Update: | Ok, someone finally did convince me. Paul Baumann wrote: |
If you use "runas /user:<username> /env <command>" then %temp% is for the original user-and could very well be read-only for the user you are running the command as. The runas command would be used to try to avoid permissions issues, so this doesn't seem too uncommon of a problem.
Absolutely true. This means some extra coding may be required to safely "run as" batch files that require write permissions.
page last modified: 2011-11-18; loaded in 0.0012 seconds