Answered by:
Bat file does not seem to run as expected

Question
-
Hello,
I'm trying to set environment variables (via SET) through a bat file which then submits job to the scheduler. However it does not set it right the first time. I have to run the bat file twice to make it run correctly. Why is this happening? The bat file is running on a UNC path.
Thanks!
Monday, September 20, 2010 3:57 PM
Answers
-
GK-F3D wrote:
We can't use any admin commands since the script is run at user level.
Only cluscfg and setx with /M switch will require admin privilages.
About your script problem:
- I believe, that variables are expanded once per commandline and in your script whole outlining IF statement is treated as such, so what is displayed by last echo command is actually INPFILE variable state before any of the SET operations took place.
- To fix this I suggest the following modifications, which will cause last INPFILE to be expanded at execution time (delayed expansion):
@echo off
SETLOCAL EnableDelayedExpansionif exist b.inp (
echo b.inp exists
set /a procs=0
for /f %%a in ('findstr /N "." b.inp ^| find /c ":"') do set /a procs=%%a
set /a meshcount=procs - 1
) else (
set INPFILE=
IF NOT "%Extension%"=="" set INPFILE=a.%Extension%
echo %Extension% extension
IF "%Extension%"=="" set INPFILE=a.inp
set /a meshcount=0
echo am i here?
echo 123 !INPFILE!
)- Marked as answer by GK-F3D Monday, September 20, 2010 7:21 PM
Monday, September 20, 2010 7:07 PM
All replies
-
Hi,
I am not exactly sure what is going wrong in your scenario. Could you share at least some fragments of your script and how are you running it?
Maybe you will also find the following pieces of information helpful:
- You can set environment variables accessible by applications running as Windows HPC Server tasks on different levels:
- Cluster wide - by use cluscfg setenvs command,
- Job level (starting from Windows HPC Server 2008 R2) - create new job by using job new or job submit command with /jobenv option (or similar from Powershell, API or GUI),
- Task level - add each task by using job add command with /env option.
- You can set environment variables permanently with use of setx command:
- Settings will be saved in registry,
- You can use /M switch to save variables for a local machine instead of current user account,
- You can use clusrun to set your variables on all computenodes.
Best regards,
Łukasz TomczykMonday, September 20, 2010 4:43 PM - You can set environment variables accessible by applications running as Windows HPC Server tasks on different levels:
-
Thanks Lukasz.
We can't use any admin commands since the script is run at user level. I've created a script to replicate the error. Here is the script:
@echo off
if exist b.inp (
echo b.inp exists
set /a procs=0
for /f %%a in ('findstr /N "." b.inp ^| find /c ":"') do set /a procs=%%a
set /a meshcount=procs - 1
) else (
set INPFILE=
IF NOT "%Extension%"=="" set INPFILE=a.%Extension%
echo %Extension% extension
IF "%Extension%"=="" set INPFILE=a.inp
set /a meshcount=0
echo am i here?
echo 123 %INPFILE%
)When I run the script first time I get this:
C:\Users\Administrator>batfile
extension
am i here?
123
When I run it again, I get what I expect:C:\Users\Administrator>batfile
extension
am i here?
123 a.inpMonday, September 20, 2010 5:19 PM -
GK-F3D wrote:
We can't use any admin commands since the script is run at user level.
Only cluscfg and setx with /M switch will require admin privilages.
About your script problem:
- I believe, that variables are expanded once per commandline and in your script whole outlining IF statement is treated as such, so what is displayed by last echo command is actually INPFILE variable state before any of the SET operations took place.
- To fix this I suggest the following modifications, which will cause last INPFILE to be expanded at execution time (delayed expansion):
@echo off
SETLOCAL EnableDelayedExpansionif exist b.inp (
echo b.inp exists
set /a procs=0
for /f %%a in ('findstr /N "." b.inp ^| find /c ":"') do set /a procs=%%a
set /a meshcount=procs - 1
) else (
set INPFILE=
IF NOT "%Extension%"=="" set INPFILE=a.%Extension%
echo %Extension% extension
IF "%Extension%"=="" set INPFILE=a.inp
set /a meshcount=0
echo am i here?
echo 123 !INPFILE!
)- Marked as answer by GK-F3D Monday, September 20, 2010 7:21 PM
Monday, September 20, 2010 7:07 PM -
AWESOME!!
This solves my problem. Thank you :)
Monday, September 20, 2010 7:22 PM