Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Dos_Probie
- Posts: 233
- Joined: 21 Nov 2010 08:07
- Location: At My Computer
#1
Post
by Dos_Probie » 12 Apr 2014 23:07
Got the following scripts with the echo working , but when creating variable get the "Eastern (US 'Canada)' is not recognized as an internal or external command, operable program or batch file"
Anyone run into this before or have any ideas..Thanks DP
WORKING:
Code: Select all
For /F "Tokens=2,*" %%A In ('Systeminfo 2^>nul^|find "Time Zone"') Do Echo %%B
ERROR:
Code: Select all
For /F "Tokens=2,*" %%A In ('Systeminfo 2^>nul^|find "Time Zone"') Do Set "SYS_TZ=%%B"
echo %SYS_TZ%
Last edited by
Dos_Probie on 13 Apr 2014 15:15, edited 1 time in total.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 13 Apr 2014 00:21
If they have ampersands then it needs to be quoted when used,
or used in a for loop variable
Code: Select all
for %%a in ("%SYS_TZ%") do echo %%~a
or used in delayed expansion.
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "sys_tz=1 & 2"
echo !SYS_TZ!
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#3
Post
by Compo » 13 Apr 2014 05:18
Instead of using SystemInfo you could use WMIC.
Code: Select all
@Echo off
Setlocal
For /f "tokens=1* delims==" %%a In ('WMIC TimeZone get Caption /value'
) Do If "%%b" Neq "" (Call Set "_TZ=%%b")
Echo("%_TZ%"
Timeout 5 >Nul
Unfortunately you'll need to know that your variable may contain an ampersand and deal with it accordingly.
[Edit]
As far as I can tell, currently the only Caption's which contain an ampersand are:
Caption : (UTC-05:00) Eastern Time (US & Canada)
StandardName : Eastern Standard Time
Caption : (UTC-06:00) Central Time (US & Canada)
StandardName : Central Standard Time
Caption : (UTC-07:00) Mountain Time (US & Canada)
StandardName : Mountain Standard Time
Caption : (UTC-08:00) Pacific Time (US & Canada)
StandardName : Pacific Standard Time
I have included the StandardName with each for reference, (you may potentially use those with WHERE and OR in order to make a judgement on whether you need to deal with ampersands).
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#4
Post
by Ed Dyreen » 13 Apr 2014 07:32
foxidrive wrote:If they have ampersands then it needs to be quoted when used,
Special characters need to be escaped, quoting is a form of escaping them.
Alternatively you can escape only the ampersand, then no quotes are echoed.
-
Dos_Probie
- Posts: 233
- Joined: 21 Nov 2010 08:07
- Location: At My Computer
#5
Post
by Dos_Probie » 13 Apr 2014 08:10
Thanks guys for all of your input, My first option was wmic because of the direct output and speed instead of systeminfo which is a lot slower from all the scanning, Eds is a good solution but Compos wmic script would be my first choice if the ampersand could somehow be parsed out..

-
aGerman
- Expert
- Posts: 4744
- Joined: 22 Jan 2010 18:01
- Location: Germany
#6
Post
by aGerman » 13 Apr 2014 08:20
Can you explain how you plan to process SYS_TZ? Actually an ampersand is not the end of the world as already stated in the answers above.
Regards
aGerman
-
Dos_Probie
- Posts: 233
- Joined: 21 Nov 2010 08:07
- Location: At My Computer
#7
Post
by Dos_Probie » 13 Apr 2014 08:33
aGerman wrote:Can you explain how you plan to process SYS_TZ? Actually an ampersand is not the end of the world as already stated in the answers above.
Regards
aGerman
Sure, here is the Computer Info script I have so far with the time zone included.
Code: Select all
@echo off
:: SYSINFO COMBO COMMANDS
For /F "delims=: tokens=1*" %%A in ('systeminfo 2^>nul') Do (
For /F "tokens=*" %%S in ("%%B") Do (
if "%%A"=="OS Name" set "OSNAME=%%S"
if "%%A"=="System Type" set "OSBIT=%%S"
if "%%A"=="OS Version" set "OSVER=%%S"
if "%%A"=="Original Install Date" set "INSTDATE=%%S"
if "%%A"=="System Manufacturer" set "SYSMFG=%%S"
if "%%A"=="System Model" set "SYSMDL=%%S"
if "%%A"=="BIOS Version" set "SYSDATE=%%S"
if "%%A"=="Time Zone" set "SYS_TZ=%%S"
if "%%A"=="Total Physical Memory" set "SYSMEM=%%S"
if "%%A"=="System Locale" set "SYSLOC=%%S"
if "%%A"=="Up Time" set "UPTIME=%%S"
)
)
echo(
set "sp=echo."
:: GET PROCESSOR INFO
for /f "tokens=2 delims==" %%I in (
'wmic cpu where "manufacturer!=\"Microsoft\" and name is not null" get name /format:list 2^>NUL'
) do set SYSPROC=%%I
:: OS ARCHITECTURE
Echo %PROCESSOR_ARCHITECTURE%|Findstr AMD64>Nul && SET arch=(64-bit)||SET arch=(32-bit)
:: OUTPUT
TITLE, SYSTEM INFO FOR: %USERNAME% OS: %ARCH%
echo OPERATING SYSTEM: %OSNAME% %OSBIT%
%sp%
echo PROCESSOR INFO: %SYSPROC%
%sp%
echo TIME ZONE: %SYS_TZ:&=^&%
%sp%
echo OS VERSION: %OSVER%
%sp%
echo ORIGINAL INSTALL DATE: %INSTDATE%
%sp%
echo SYSTEM LOCALE: %SYSLOC%
%sp%
echo OEM BUILD DATE: %SYSDATE%
%sp%
echo MODEL INFO: %SYSMFG% %SYSMDL%
%sp%
echo USEABLE MEMORY: %SYSMEM%
pause>NUL
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#8
Post
by Ed Dyreen » 13 Apr 2014 08:35
I assume 'WMIC TimeZone get Caption /value' returns:
Code: Select all
Caption=(UTC-05:00) Eastern Time (US & Canada)
So I assume you want to chop this return value and save it in a variable like this:
Code: Select all
@echo off &setlocal disableDelayedExpansion
for /f "tokens=2* delims==" %%? in (
'echo.Caption^^^=^^^(UTC-05:00^^^) Eastern Time ^^^(US ^^^& Canada^^^)'
) do set "caption=%%?"
set "caption"
echo.caption=%caption:&=^&%_
pause
exit
Code: Select all
caption=(UTC-05:00) Eastern Time (US & Canada)
caption=(UTC-05:00) Eastern Time (US & Canada)_
Druk op een toets om door te gaan. . .
But if I were you I'd just enable delayedExpansion

-
aGerman
- Expert
- Posts: 4744
- Joined: 22 Jan 2010 18:01
- Location: Germany
#9
Post
by aGerman » 13 Apr 2014 08:42
So the only issue is how to safely output the values? As foxi already mentioned you can use the Delayed Variable Expansion.
Code: Select all
...
:: OUTPUT
setlocal EnableDelayedExpansion
TITLE, SYSTEM INFO FOR: !USERNAME! OS: !ARCH!
...
Just replace all the other percent signs with exclamation marks for variables in your OUTPUT block.
Regards
aGerman
-
Dos_Probie
- Posts: 233
- Joined: 21 Nov 2010 08:07
- Location: At My Computer
#10
Post
by Dos_Probie » 13 Apr 2014 10:01
Thanks aGerman, but when using wmic and based on parts of the above scripts this will omit the quotation marks (which I want) but still shows the "amp;" and not "&" in the results..
Code: Select all
@echo off &setlocal
For /F "Tokens=1* Delims==" %%A In ('WMIC TimeZone get Caption /value') Do If '%%B Neq ' Set SYS_TZ=%%B
Echo TIME ZONE: %SYS_TZ:&=^%
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#11
Post
by Ed Dyreen » 13 Apr 2014 10:20
Dos_Probie wrote:Thanks aGerman, but when using wmic and based on parts of the above scripts this will omit the quotation marks (which I want) but still shows the "amp;" and not "&" in the results..
Code: Select all
@echo off &setlocal
For /F "Tokens=1* Delims==" %%A In ('WMIC TimeZone get Caption /value') Do If '%%B Neq ' Set SYS_TZ=%%B
Echo TIME ZONE: %SYS_TZ:&=^%
You've been given several working examples and you succeeded in messing it up
This 'If '%%B Neq '' makes no sense at all !
What is the difference between;
if VALUE IS NOT NULL then set VALUE=NOT NULL
versus;
set VALUE=NOT NULL OR NULL
Dos_Probie wrote:still shows the "amp;" and not "&" in the results..
Really ?

-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#12
Post
by Compo » 13 Apr 2014 10:23
I would strongly suggest that you use the format I provided in my response,
(I used it for a reason):
Code: Select all
If "%%B" Neq "" (Call Set "SYS_TZ=%%B")
[Edit]
I've also told you before,
in another place, a more robust way of determining the OS architecture.
In your example I'd suggest, if Windows 7 and above:
Code: Select all
Set arch=64-bit
If Not Defined ProgramW6432 (Set arch=32-bit)
if pre-Windows 7
Code: Select all
Set arch=64-bit
If %Processor_Architecture%==x86 (If Not Defined Processor_Architew6432 (
Set arch=32-bit))
-
aGerman
- Expert
- Posts: 4744
- Joined: 22 Jan 2010 18:01
- Location: Germany
#13
Post
by aGerman » 13 Apr 2014 10:32
It may have to do with the garbled unicode linebreak when using WMIC in a FOR /F loop.
Try to use a nested loop:
Code: Select all
For /F "Tokens=1* Delims==" %%A In ('WMIC TimeZone get Caption /value') Do For /F "Delims=" %%C In ("%%B") Do Set "SYS_TZ=%%C"
Regards
aGerman
-
Compo
- Posts: 600
- Joined: 21 Mar 2014 08:50
#14
Post
by Compo » 13 Apr 2014 11:46
aGerman wrote:It may have to do with the garbled unicode linebreak when using WMIC in a FOR /F loop.
Here is a test script just to show how the output varies
Code: Select all
@Echo off
For /F "Tokens=1* Delims==" %%A In ('WMIC TimeZone get StandardName /value'
) Do Echo=[%%B]
Echo( Look at how many times you get an output
Timeout 5 >Nul
Echo(
For /F "Tokens=1* Delims==" %%A In ('WMIC TimeZone get StandardName /value'
) Do If "%%B" Neq "" Echo=[%%B]
Echo( Only one output now but something strange has happened
Timeout 5 >Nul
Echo(
For /F "Tokens=1* Delims==" %%A In ('WMIC TimeZone get StandardName /value'
) Do If "%%B" Neq "" (Call Echo=[%%B])
Echo( Now you have the expected output
Timeout 5 >Nul
I used StandardName in order that those exhibiting the ampersand problem, especially Dos_Probie, could still run the test.
-
aGerman
- Expert
- Posts: 4744
- Joined: 22 Jan 2010 18:01
- Location: Germany
#15
Post
by aGerman » 13 Apr 2014 14:25
Here we already discussed that issue. I just did some tests to illustrate the behavior.
Regards
aGerman