CMD variable is not processed. How to fix

Started by Mike

Mike

CMD variable is not processed. How to fix   22 February 2019, 01:50

The following statement shows different results when run from NetScan and from command prompt:
cmd /k set "target=10.10.7.7" & set tmp=%temp% & echo %target% & echo %tmp%

From NetScan:
cmd /k set "target=10.10.7.7" & set tmp=%temp% & echo %target% & echo %tmp%

%target%
C:\Users\mike\AppData\Local\Temp\NetScanPortableTemp

From CMD window:
set "target=10.10.7.7" & set tmp=%temp% & echo %target% & echo %tmp%

10.10.7.7
C:\Users\mike\AppData\Local\Temp\NetScanPortableTemp
SoftPerfect Support forum - Andrew avatar image

Re: CMD variable is not processed. How to fix   22 February 2019, 10:24

This issue is not connected to Network Scanner. It is just how the command processor evaluates environment variables. For example, open a new command prompt and type the above statement. It will result in something like this, where in the first set of commands the variable is not evaluated:

SoftPerfect support forum

To work around this, you need delayed variable expansion and a different syntax, like this:
cmd /V /k set "target=10.10.7.7" & set tmp=%temp% & echo !target! & echo %tmp%
Here /V turns on the delayed expansion and !target! refers to a delayed-expansion variable. I also recommend using && instead of & to ensure the commands continue executing only if all prior commands succeeded.

Sometimes you can find a solution faster if you try the forum search, have a look at the knowledge base, or check the software user manual to see if your question has already been answered.

Our forum rules are simple:

  • Be polite.
  • Do not spam.
  • Write in English. If possible, check your spelling and grammar.

Author:

Subject

A brief and informative title for your message, approximately 4–8 words:

     

Spam prevention: please enter the following code in the input field below.

 ********   **     **  **    **  **     **        ** 
 **     **  **     **  **   **   ***   ***        ** 
 **     **  **     **  **  **    **** ****        ** 
 **     **  **     **  *****     ** *** **        ** 
 **     **  **     **  **  **    **     **  **    ** 
 **     **  **     **  **   **   **     **  **    ** 
 ********    *******   **    **  **     **   ******  

Message: