Variable do not get set, to %errorlevel%

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Variable do not get set, to %errorlevel%

#1 Post by BoQsc » 18 Mar 2018 07:50

Here, I'm passing DiskPart output to the FOR loop, to filter and rearrange the output.
Suddenly I've got an idea to make direct menu out of that output:

Code: Select all

	::Choosing PhysicalDrive
	choice /C 0123456789 /M "Enter PhysicalDrive Number: "
		set "errorLevelOutput = %errorlevel%"
However,

Code: Select all

	::Choosing PhysicalDrive
	choice /C 0123456789 /M "Enter PhysicalDrive Number: 
	set "errorLevelOutput = %errorlevel%"
	echo !errorLevelOutput! error level out 
	echo %errorLevelOutput% error level out
Seems to not output %errorlevel% into variable.
variables %errorLevelOutput%, !errorLevelOutput! are empty






Full batch code - gdiskGuide.cmd

Code: Select all

@ECHO OFF & cd %~dp0
SETLOCAL EnableDelayedExpansion	


::Required to be run as administrator

:GainInformationFromDiskPart
cls
	::Always run with administrator privilegies.
	::Withouh administrator privilegies, diskpart relaunch in a seperate commandLine window.
	
	::Capture "DiskPart list disk" output using FOR loop
	set "previousSelection=!currentSelection!"
	FOR /F "USEBACKQ tokens=1,2,3,4,5,6,7 skip=9" %%a IN (`
		
		^( echo list disk ^) ^| diskpart 
		
		
	`) DO (
			set "outputLine=%%a"
			IF not [!outputLine!]==[DISKPART^>] (
				echo [!selection[%%b]!] %%b  !outputLine! %%d%%e %%f%%g
			)
			
		)
	
	::Choosing PhysicalDrive
	choice /C 0123456789 /M "Enter PhysicalDrive Number: "
		set "errorLevelOutput = %errorlevel%"
		echo !errorLevelOutput! error level out 
		echo %errorLevelOutput% error level out
	
	::Clear previous selection
	set "selection[!previousSelection!]="
	
	::Set new selection  
	set "currentSelection=!errorLevelOutput!"
	set "selection[!errorLevelOutput!]=+"


	
call :GainInformationFromDiskPart !currentSelection!




::Physical drive examples 
::  \\.\physicaldrive0
::  \\.\physicaldrive1
::  \\.\physicaldrive2
::  1:
::  2:
::  3:

set "physicalDrive=1:"


::Execute commands inside gdisk32.exe, 
::on the selected physical drive.

(
	::Print Help Menu 
	echo ?
	
	
	::write table to disk and exit
	::echo w
	
	
	::quit without saving changes
	echo q 
	
	
) | gdisk32.exe %physicalDrive%
echo.

Last edited by BoQsc on 18 Mar 2018 08:00, edited 2 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Variable do not get set, to %errorlevel%

#2 Post by aGerman » 18 Mar 2018 07:56

Using ...

Code: Select all

set "errorLevelOutput = %errorlevel%"
... you set a variable %errorLevelOutput % (notice the space before the last percent sign).
Rather use ...

Code: Select all

set "errorLevelOutput=%errorlevel%"
... without spaces around the equal sign.

Steffen

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Variable do not get set, to %errorlevel%

#3 Post by BoQsc » 18 Mar 2018 08:15

aGerman wrote:
18 Mar 2018 07:56
Using ...

Code: Select all

set "errorLevelOutput = %errorlevel%"
... you set a variable %errorLevelOutput % (notice the space before the last percent sign).
Rather use ...

Code: Select all

set "errorLevelOutput=%errorlevel%"
... without spaces aroud the equal sign.

Steffen
... this mistake is worth a laughtware.
My brain didn't even flinched over this, while I wasted more than 20 minutes of intense testing.
I might be just tired of many other quirks with batch that frustated me today, that's why I didn't noticed that.

Thanks agerman, everything back to normal. Just got shot of dopamine and endorphines after finding out, this simple.

Post Reply