delayedvariableexpansion

Missing “!” string when EnableDelayedExpansion

半世苍凉 提交于 2019-12-24 13:01:56
问题 When I enabled DelayedExpansion in the script, it doesn't echo out the " ! " string in the file name. For instance: Original File01-TEXT!.txt Echo out File01-TEXT.txt I guess it's because of the setlocal EnableDelayedExpansion , but I can't remove because I need it. @echo off setlocal EnableDelayedExpansion cd "C:\Files" for %%a in (*.txt) do ( REM Here's the problem... echo %%a set "str=%%a" set new_str=!str:0,3! ) echo %new_string% pause >nul 回答1: Depending on the real code, you can work

How would I call a dynamic variable name?

爱⌒轻易说出口 提交于 2019-12-20 05:46:10
问题 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=

Why are my set commands resulting in nothing getting stored?

夙愿已清 提交于 2019-12-17 16:19:36
问题 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

windows batch SET inside IF not working

对着背影说爱祢 提交于 2019-12-17 02:34:35
问题 when I'm running this script (from a .bat file): set var1=true if "%var1%"=="true" ( set var2=myvalue echo %var2% ) I always get: ECHO is on. Meaning the var2 variable was not really set. Can anyone please help me understand why? 回答1: var2 is set, but the expansion in the line echo %var2% occurs before the block is executed. At this time var2 is empty. Therefore the delayedExpansion syntax exists, it uses ! instead of % and it is evaluated at execution time, not parse time. Please note that

Why the loop is not running as expected?

懵懂的女人 提交于 2019-12-13 15:30:50
问题 I have folders like E:\Backups\code\Hazard\test1 ... testn And inside these test folders something like E:\Backups\code\Hazard\test1\it0 ... itn The root folder is E:\Backups\code from where the code runs. The below code runs on each subfolders and copies summary.yml from it0 folder to latest it(n) folder. Why the code runs just for test1 folder and then hangs? setlocal ENABLEDELAYEDEXPANSION set root=%cd% for /D %%X in (%root%\*) do ( echo %%X cd %%X for /D /r %%b in (*) do ( cd %%b echo %%b

Variable is not set [duplicate]

泪湿孤枕 提交于 2019-12-12 05:20:39
问题 This question already has an answer here : Variables are not behaving as expected (1 answer) Closed last year . This is extension of the code from Why the loop is not running as expected? Ultimately I want to read values from Report.txt (a comma separated file having values like 1,2,3,4 in a single line) and update these values in some fields of summary.yml (...field10 : var1, field11 :var2,... field25 :var3..) etc. For that I am trying to store values from Report.txt but the variable are not

Windows batch file - Pick (up to) four random files in a folder

二次信任 提交于 2019-12-11 07:57:15
问题 As the title says, I'm trying to pick up to four random files (wallpapers) from a folder, for further processing. The folder does not contain subfolders, just *.jpg's, *.bmp's and *.png's (it may contain a Thumbs.db file, but I already took care of that). I read all the files with a for loop making something similar to an array, then I'd like to run another for loop for making the random numbers that will act as indexes for choosing the files. setlocal enabledelayedexpansion set "wps=1 2 3 4"

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

心不动则不痛 提交于 2019-12-08 03:36:42
问题 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

Nested variable name using EnableDelayedExpansion

烈酒焚心 提交于 2019-12-07 18:01:30
I am working on a script to get max lengths of each column, I'm trying to store lengths of max length in _c1...n vars. number of columns unknown. I was able to get length for each column, create variables to store each with set _c!i! = !n!, n is the length but in order to set the max length for a particular column I need to compare current with max and use something like !_c!!i!! which doesn't work, any ideas how to refer a variable which part of it's name coming from another variable? Thanks... I assume that you are using the delayed expansion character because you are working inside a set of

Dos inline IF test for errorlevel, without use of Delayed Expansion

て烟熏妆下的殇ゞ 提交于 2019-12-06 08:23:46
Is there anyway to do the below without delayed expansion (one line, broken for readability)? %comspec% /v:on /c %windir%\System32\reg.exe import c:\temp\test.reg & if !errorlevel! neq 0 pause If it were an echo, I know that call can be used, but doesn't seem to be available for use with the if . Andriy M One way is to use this older syntax: %comspec% ... & if errorlevel 1 pause In this case, a special variant of if statement is used, one that tests the current errorlevel state. Another option might be this: %comspec% ... || pause The || command delimiter is roughly equivalent to & if