delayedvariableexpansion

using a DelayedExpansion index variable for an array within an IF statement fails

倖福魔咒の 提交于 2019-12-06 07:52:30
I have a batch file where I am displaying all *.pem files in a directory, giving the user an option to choose one, and at this time just trying to show the user what file they picked by using the number they chose to ECHO the contents of the array. I believe that somehow because it is inside of the IF statement, the index variable "selectedPem" is not expanding in the array's index brackets because it is using the % and not using the delayed expansion !, which doesnt appear to work in the array's index brackets. I think this because if I set the "selectedPem" variable before the IF statement,

How would I call a dynamic variable name?

拥有回忆 提交于 2019-12-02 03:32:28
Okay, so I'm trying to make a program that "understands" user input and does what they tell it to do. People usually just use specific commands such as "open this file" and it only works if the user types EXACTLY that. I'm trying to give my users a little bit of leeway, so that they can type something like what they want to happen, and the computer will get the general idea. With that block of rambling aside, I've run into a problem. set word%wordNum%=%word% :fileExtension set extChk= %letterNum% - 2 REM Includes the period of the extension call set extension=%%_albaiRec:~%extChk%,4%% ::extChk

Variables not set in the batch file [duplicate]

不想你离开。 提交于 2019-12-02 02:01:19
This question already has an answer here: Variables are not behaving as expected 1 answer setlocal enabledelayedexpansion If "%computername%"=="USER-PC" ( set abc = ZZZ.bat echo %abc% pause ) Here abc always shows blank. What could be the possible reason? You're halfway there, inasmuch as you've enabled delayed expansion. However, delayed expansion uses the ! characters rather than % , so what you need is: setlocal enabledelayedexpansion if "%computername%"=="USER-PC" ( set abc=ZZZ.bat echo !abc! pause ) Note also that: set abc = ZZZ.bat does not create an abc variable, it creates an abc space

Variables not set in the batch file [duplicate]

白昼怎懂夜的黑 提交于 2019-12-02 01:40:51
问题 This question already has an answer here : Variables are not behaving as expected (1 answer) Closed last year . setlocal enabledelayedexpansion If "%computername%"=="USER-PC" ( set abc = ZZZ.bat echo %abc% pause ) Here abc always shows blank. What could be the possible reason? 回答1: You're halfway there, inasmuch as you've enabled delayed expansion. However, delayed expansion uses the ! characters rather than % , so what you need is: setlocal enabledelayedexpansion if "%computername%"=="USER

Batch Scripting Help - Replace Substring of a DelayedExpansion Var with another DelayedExpansion Var

纵饮孤独 提交于 2019-12-01 19:03:41
Basically I'm trying to do !var1:SomeText=!var2!! but this code doesn't work. What am I missing? dbenham The order of expansion is critical when doing a search and replace operation that uses a variable for the search and/or the replace. The inner variable must be expanded before the outer search and replace expansion takes place. Trying to used delayed expansion for both obviously can't work because the delayed expansion occurs at one point in time. The classic method for expansion of a variable within another variable uses delayed expansion for the outer, and normal for the inner: echo !var1

Batch: Returning a value from a SETLOCAL EnableDelayedExpansion

半世苍凉 提交于 2019-12-01 06:32:53
I wonder why this code does not work as expected: @ECHO off SET S1=HELLO SETLOCAL EnableDelayedExpansion SET S2=!S1! WORLD^^! ECHO !S2! ENDLOCAL & SET S1=!S2! ECHO %S1% PAUSE Output: HELLO WORLD! !S2! Expected output: HELLO WORLD! HELLO WORLD! Thanks. It works as expected. The delayed expansion will expand variables at execution time, not parse time, so it interpret your line ENDLOCAL & SET S1=!S2! as endlocal But at the part of SET S1=!S2! the delayed expansion is off so it can't be expanded anymore. In your case you could use ENDLOCAL & SET S1=%S2% As the exclamation mark is S2 is "safe", as

Batch: Returning a value from a SETLOCAL EnableDelayedExpansion

你说的曾经没有我的故事 提交于 2019-12-01 04:38:39
问题 I wonder why this code does not work as expected: @ECHO off SET S1=HELLO SETLOCAL EnableDelayedExpansion SET S2=!S1! WORLD^^! ECHO !S2! ENDLOCAL & SET S1=!S2! ECHO %S1% PAUSE Output: HELLO WORLD! !S2! Expected output: HELLO WORLD! HELLO WORLD! Thanks. 回答1: It works as expected. The delayed expansion will expand variables at execution time, not parse time, so it interpret your line ENDLOCAL & SET S1=!S2! as endlocal But at the part of SET S1=!S2! the delayed expansion is off so it can't be

Why can I not get a substring of a delayed expansion variable in an if statement?

亡梦爱人 提交于 2019-11-30 14:49:03
问题 Why does string-manipulation work inline in an if statement without using delayed expansion variables - but fails otherwise. For example: set test=testString if %test:~0,4%==test echo Success This works correctly; returning Success . However if I do the following: setLocal enableDelayedExpansion set test=testString if !test:~0,4!==test echo Success I receive the error - 4!==test was unexpected at this time. Obviously you can get around this by doing something like set comp=!test:~0,4! then

Cmd and exclamation marks - Part II

与世无争的帅哥 提交于 2019-11-29 11:54:00
I'm really wondering why my string replacement procedure works when parsing text files containing any special characters including exclamation marks. I expected that delayed variable expansion would switch off special meaning of ampersand, percent sign etc. but will fail instead for exclamation marks... Code: @echo on & setlocal ENABLEEXTENSIONS set "InFile=%~1" set "OutFile=%~2" set "Replace=%~3" CALL :ParseCue "%%InFile%%" "%%OutFile%%" "%%Replace%%" endlocal &GOTO:EOF :ParseCue @echo on & setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION set "FileToParse=%~1" set "OutputFile=%~2" set

Why are my set commands resulting in nothing getting stored?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 01:42:21
I am trying to access the value of TOMCAT_VER later on, but it appears as an empty string. if exist "%_REALPATH%\tomcat-%TOMCAT_VER2%" ( set CATALINA_HOME=%_REALPATH%\tomcat-%TOMCAT_VER2% set TOMCAT_VER=%TOMCAT_VER2% echo "%TOMCAT_VER%" ) else if exist "%TOMCAT_VER2%" ( set CATALINA_HOME="%TOMCAT_VER2%" set TOMCAT_VER="%TOMCAT_VER2%" echo "%TOMCAT_VER%" ) To further debug, I inserted an echo statement right below where it gets set, but it doesn't seem to work. With echo off disabled, I can see the statement showing these variables getting set, and yet I can't seem to print them out. You found