Guest Will Posted September 21, 2008 Posted September 21, 2008 I need help with a simple script. I want to put the output of a command into an environment variable, and I tried this construction in a CMD file: for /f "usebackq delims=" %%i in (`D:\USR\BINTU\MKSNT\date +%a`) do set dayofweek=%%i The moment this line of the script executes, the script is terminated with no error. It looks to me like the date.exe command I am calling is exiting in a way that doesn't allow a return back to the script that called it. The %dayofweek% environment variable is NOT being set. Any thoughts on how I can get this to work? -- Will
Guest Pegasus \(MVP\) Posted September 21, 2008 Posted September 21, 2008 Re: Need Help With Script "Will" <westes-usc@noemail.nospam> wrote in message news:zeidnW4I44EhB0vVnZ2dnUVZ_oWdnZ2d@giganews.com... >I need help with a simple script. I want to put the output of a command > into an environment variable, and I tried this construction in a CMD file: > > for /f "usebackq delims=" %%i in (`D:\USR\BINTU\MKSNT\date +%a`) do set > dayofweek=%%i > > The moment this line of the script executes, the script is terminated with > no error. It looks to me like the date.exe command I am calling is > exiting > in a way that doesn't allow a return back to the script that called it. > The %dayofweek% environment variable is NOT being set. > > Any thoughts on how I can get this to work? > > -- > Will > Did you try this somewhat simplified syntax? @echo off for /f "delims=" %%i in ('D:\USR\BINTU\MKSNT\date +%a') do set dayofweek=%%i A few questions: - What's %a? If it's a loop variable (perhaps from an outer loop) then it should be %%a. - What's the + in front of %a? A switch marker? - When you execute the command D:\USR\BINTU\MKSNT\date > c:\test.txt and examine c:\test.txt with a binary viewer (e.g. debug.exe), what exactly do you see? Is it an ASCII file? Unicode? What are the last few characters in the file?
Guest Will Posted September 21, 2008 Posted September 21, 2008 Re: Need Help With Script "Pegasus (MVP)" <I.can@fly.com.oz> wrote in message news:ufm5Q7BHJHA.740@TK2MSFTNGP03.phx.gbl... > > "Will" <westes-usc@noemail.nospam> wrote in message > news:zeidnW4I44EhB0vVnZ2dnUVZ_oWdnZ2d@giganews.com... > >I need help with a simple script. I want to put the output of a command > > into an environment variable, and I tried this construction in a CMD file: > > > > for /f "usebackq delims=" %%i in (`D:\USR\BINTU\MKSNT\date +%a`) do set > > dayofweek=%%i > > > > The moment this line of the script executes, the script is terminated with > > no error. It looks to me like the date.exe command I am calling is > > exiting > > in a way that doesn't allow a return back to the script that called it. > > The %dayofweek% environment variable is NOT being set. > > > > Any thoughts on how I can get this to work? > > > > -- > > Will > > > > Did you try this somewhat simplified syntax? > @echo off > for /f "delims=" %%i in ('D:\USR\BINTU\MKSNT\date +%a') do set dayofweek=%%i > > A few questions: > - What's %a? If it's a loop variable (perhaps from an outer loop) > then it should be %%a. > - What's the + in front of %a? A switch marker? > - When you execute the command > D:\USR\BINTU\MKSNT\date > c:\test.txt > and examine c:\test.txt with a binary viewer (e.g. debug.exe), what > exactly do you see? Is it an ASCII file? Unicode? What are the last > few characters in the file? The date.exe I am executing here is a Windows version of the Unix Date command. The command: date +%a will give the three letter code for the day of the week. On Sunday it would give output: Sun So to answer your question "%a" is NOT a loop variable from elsewhere in the script. It is a literal text input string that is interpreted by the date command for purposes of selecting an output. Redirection of this command from the command line to a file put the appropriate output there without any problems. Inspecting this output file with debug gave the expected result of three ASCII characters followed by 0xD 0xA. I tried your simplified syntax without usebackq or backquotes and the script still dies in the same place. -- Will
Guest Pegasus \(MVP\) Posted September 21, 2008 Posted September 21, 2008 Re: Need Help With Script "Will" <westes-usc@noemail.nospam> wrote in message news:VcidnWSj0vlKXUvVnZ2dnUVZ_vOdnZ2d@giganews.com... > "Pegasus (MVP)" <I.can@fly.com.oz> wrote in message > news:ufm5Q7BHJHA.740@TK2MSFTNGP03.phx.gbl... >> >> "Will" <westes-usc@noemail.nospam> wrote in message >> news:zeidnW4I44EhB0vVnZ2dnUVZ_oWdnZ2d@giganews.com... >> >I need help with a simple script. I want to put the output of a >> >command >> > into an environment variable, and I tried this construction in a CMD > file: >> > >> > for /f "usebackq delims=" %%i in (`D:\USR\BINTU\MKSNT\date +%a`) do set >> > dayofweek=%%i >> > >> > The moment this line of the script executes, the script is terminated > with >> > no error. It looks to me like the date.exe command I am calling is >> > exiting >> > in a way that doesn't allow a return back to the script that called it. >> > The %dayofweek% environment variable is NOT being set. >> > >> > Any thoughts on how I can get this to work? >> > >> > -- >> > Will >> > >> >> Did you try this somewhat simplified syntax? >> @echo off >> for /f "delims=" %%i in ('D:\USR\BINTU\MKSNT\date +%a') do set > dayofweek=%%i >> >> A few questions: >> - What's %a? If it's a loop variable (perhaps from an outer loop) >> then it should be %%a. >> - What's the + in front of %a? A switch marker? >> - When you execute the command >> D:\USR\BINTU\MKSNT\date > c:\test.txt >> and examine c:\test.txt with a binary viewer (e.g. debug.exe), what >> exactly do you see? Is it an ASCII file? Unicode? What are the last >> few characters in the file? > > The date.exe I am executing here is a Windows version of the Unix Date > command. The command: > > date +%a > > will give the three letter code for the day of the week. On Sunday it > would give output: > > Sun > > So to answer your question "%a" is NOT a loop variable from elsewhere in > the > script. It is a literal text input string that is interpreted by the > date > command for purposes of selecting an output. > > Redirection of this command from the command line to a file put the > appropriate output there without any problems. Inspecting this output > file > with debug gave the expected result of three ASCII characters followed by > 0xD 0xA. > > I tried your simplified syntax without usebackq or backquotes and the > script > still dies in the same place. > > -- > Will If you modify your batch file to read echo D:\USR\BINTU\MKSNT\date +%a then you will immediately see that "+%a" translates in "+a". To maintain the % character, you must code it like so: echo D:\USR\BINTU\MKSNT\date +%%a
Guest Will Posted September 22, 2008 Posted September 22, 2008 Re: Need Help With Script "Pegasus (MVP)" <I.can@fly.com.oz> wrote in message news:%23dMwkvDHJHA.3932@TK2MSFTNGP03.phx.gbl... > If you modify your batch file to read > echo D:\USR\BINTU\MKSNT\date +%a > then you will immediately see that "+%a" translates in "+a". To maintain the > % character, you must code it like so: > echo D:\USR\BINTU\MKSNT\date +%%a That was it. Thanks! -- Will
Recommended Posts