Loop through "array" in batch file to shift elements - arrays

I have a batch file that is passed commands in the form of a string array from a Java file. The commands contain something like the following:
String[] commands = {"A",
"B",
"C",
"C:\users\user\Documents",
"C:\users\user\Pictures"}
The commands array is dynamic, as it changes every time the java program is run. In the batch file, I create variables to take the values of the first three elements (A, B, and C in this case). Then I need to shift the directory strings to take up the first three elements of the array. Here is the batch code I have so far:
#echo off
setlocal enableDelayedExpansion
set /A paramCount=0
for %%x in (%*) do (
set list[!paramCount!]=%%x
set /A paramCount=paramCount+1
)
set argA=%list[0]%
set argB=%list[1]%
set argC=%list[2]%
set /A old=0
set /A new=!old!+3
for /F "tokens=2 delims==" %%a in ('set list[') do (
echo old=!old!
echo new=!new!
set list[!old!]=!list[%new%]!
echo !list[%old%]!
set /A old=!old!+1
set /A new=!new!+1 )
The problem I am having is with the line set list[!old!]=!list[%new%]!. As you can see, I have delayed expansion enabled. However, the !!'s are needed for the list[...] variable that is emulating an element in an array. However, I believe I need to use delayed expansion for "new" as well. What am I to do in this case? Or perhaps that's not the actual problem? The "old" and "new" variables are incrementing correctly, but the echo !list[%old%]! line returns the same value every time. I expect the same issue exists in that line, with "old"--It should have !'s surrounding it but the !'s are already being used for the list[...] variable. So what happens if you need nested !'s in a statement? Thanks for the aid!

#echo off
setlocal ENABLEDELAYEDEXPANSION
set /A paramCount=-3
for %%x in (%*) do (
set list[!paramCount!]=%%x
set /A paramCount=paramCount+1
)
set argA=%list[-3]%
set argB=%list[-2]%
set argC=%list[-1]%
for /F "tokens=2 delims==" %%a in ('set list[-') do SET "%%a="
SET arg
SET list
ENDLOCAL
echo==================
setlocal ENABLEDELAYEDEXPANSION
set /A paramCount=0
for %%x in (%*) do (
set list[!paramCount!]=%%x
set /A paramCount=paramCount+1
)
set argA=%list[0]%
set argB=%list[1]%
set argC=%list[2]%
set /A old=0
set /A new=!old!+3
for /F "tokens=2 delims==" %%a in ('set list[') do (
echo old=!old!
echo new=!new!
CALL set list[%%old%%]=%%list[!new!]%%
CALL ECHO(%%list[!old!]%%
set /A old=!old!+1
set /A new=!new!+1
)
SET arg
SET list
GOTO :EOF
This should work for you - the easy way and the hard way.

Related

Double Expansion on Array Index Windows Batch

Inside the for loop I'm trying to access the element at index count in CLs (this line of code: echo !!CLs[!count!]!!) , but I'm not sure how to do this. I don't really understand how expansion works in this case, so what you see below it me trying something out of no where.
#ECHO off
setlocal enableextensions enabledelayedexpansion
SET CLs[0]=#
SET /A count = 0
FOR /F "tokens=5" %%I IN ('some command') DO (
echo !!CLs[!count!]!! :: THIS LINE
IF NOT %%I == CLs[!count!] (
SET /A count += 1
SET CLs[!count!]=%%I
)
)
echo The item is %CLs[10]%
endlocal
Thanks
According to the post How does the Windows Command Interpreter (CMD.EXE) parse scripts? (see phase 5), the line echo !!CLs[!count!]!! cannot work, because the opening !! are collapsed to a single !, then !CLs[! is expanded to an empty string (assuming such variable is not defined), then count is returned literally, then !]! is expanded to an empty string and the final ! is dismissed. Or in other words, delayed expansion cannot be nested.
You can use call though to introduce another parsing phase, like this:
call echo %%CLs[!count!]%%
The line IF NOT %%I == CLs[!count!] ( ... ) is wrong, you must expand the right value too. However, call if will not help unfortunately, because if (like for and rem) is a special command that is recognised by the parser earlier than others, like call.
To work around that you can store the value of !count! in a for meta-variable, like %%J, for instance, to introduce another parsing phase, and use !CLs[%%J]! then, like this:
set /A "count=0"
for /F "tokens=5" %%I in ('some command') do (
for %%J in (!count!) do (
echo !CLs[%%J]!
if not "%%I" == "!CLs[%%J]!" (
set /A "count+=1"
set "CLs[!count!]=%%I"
)
)
)
Another yet slower possibility is to put the relevant code into a sub-routine:
set /A "count=0"
for /F "tokens=5" %%I in ('some command') do (
call :SUB !count!
)
goto :EOF
:SUB
echo !CLs[%~1]!
if not "%%I" == "!CLs[%~1]!" (
set /A "count+=1"
set "CLs[%~1]=%%I"
)
goto :EOF
You may also take a look at the post Arrays, linked lists and other data structures in cmd.exe (batch) script about how to deal with such pseudo-arrays.
ECHO ------------- START AT %time%
REM <!-- language: lang-dos -->
#ECHO Off
setlocal enableextensions ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q58209698.txt"
SET CLs[0]=#
SET /a clscnt[0]=0
SET /A count = 0
FOR /F "tokens=*" %%I IN ('type %filename1%') DO (
SET "processed="
FOR /f "tokens=1,2,3delims=[]=" %%a IN ('set cls[') DO IF /i "%%a"=="cls" (
IF "%%I"=="%%c" (SET /a clscnt[%%b]+=1&SET "processed=y")
)
IF not DEFINED processed SET /a count+=1&SET "cls[!count!]=%%I"&SET /a clscnt[!count!]=1
)
FOR /L %%a IN (0,1,%count%) DO ECHO !clscnt[%%a]! times !cls[%%a]!
ENDLOCAL
ECHO -------------------------Second way -----------------
#ECHO Off
setlocal enableextensions ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q58209698.txt"
SET CLs[0]=#
SET /a clscnt[0]=0
SET /A count = 0
FOR /F "tokens=*" %%I IN ('type %filename1%') DO (
SET "processed="
FOR /L %%a IN (0,1,!count!) DO (
IF "%%I"=="!cls[%%a]!" (SET /a clscnt[%%a]+=1&SET "processed=y")
)
IF not DEFINED processed SET /a count+=1&SET "cls[!count!]=%%I"&SET /a clscnt[!count!]=1
)
FOR /L %%a IN (0,1,%count%) DO ECHO !clscnt[%%a]! times !cls[%%a]!
ENDLOCAL
GOTO :EOF
I used a file named q58209698.txt containing some dummy data for my testing and chose to use the entire data line, having no suitable files where token 5 existed.
Note that as a bonus, I've added clscnt - an array of occurence-counts.
Shown: two separate ways of achieving the aim of finding/counting the unique tokens. Naturally, if the cls array is pre-loaded with the required tokens, then it's basic-programmer's-play to adjust the code to detect/report occurrences of those tokens.
The two methods are similar. In the first, set is used to list the established variables starting cls[. The first if ensures processing only the array-name cls, then either it's a repeat (set prcoessed to a value and increment the occurrences-counter) or it's a new value (when the for...%%a loop ends, processed is still undefined) so record it.
The second way is more direct, using the value of count to specifically interrogate the values in the cls array.

How to return an array of values across ENDLOCAL

I have two variable length arrays of values, TargetName[] and TargetCpu[], which I need to return across the ENDLOCAL boundary. I've tried the following, but only the first value on the first array gets returned.
for /L %%i in (0,1,%MaxIndex%) do (
for /f "delims=" %%j in (""!TargetName[%%i]!"") do (
for /f "delims=" %%k in (""!TargetCpu[%%i]!"") do (
endlocal
set TargetName[%%i]=%%j
set TargetCpu[%%i]=%%k
)
)
)
Below is a print of the values returned.
Number Targets: 3
TargetName[0]: "Computer1"
TargetCpu[0] : "x64"
TargetName[1]: "!TargetName[1]!"
TargetCpu[1] : "!TargetCpu[1]!"
TargetName[2]: "!TargetName[2]!"
TargetCpu[2] : "!TargetCpu[2]!"
I've read about everything I can find, but nothing I've tried works for a variable length array.
#echo off
setlocal
set "MaxIndex=6"
call :CreateArrays
set TargetName
set TargetCPU
goto :EOF
:CreateArrays
setlocal EnableDelayedExpansion
for /L %%i in (1,1,%MaxIndex%) do (
set /A TargetName[%%i]=!random!, TargetCpu[%%i]=!random!
)
rem Return the arrays to the calling scope
set "currentScope=1"
for /F "delims=" %%a in ('set TargetName[ ^& set TargetCPU[') do (
if defined currentScope endlocal
set "%%a"
)
exit /B
set target>tempfile
rem insert your endlocal here
for /f "delims=" %%a in (tempfile) do set "%%a"
set target
the first set will list all variable names that start target into a tempfile.
Then execute your endlocal
then read each line of the file, which is of the form name=value and execute it prefixed by the set keyword.
Final set is to display results.
clearing up the tempfile isyour affair. Naturally, if you have other elements you don't want restored, you could use for instance
set targetname>>tempfile
set targetcpu>>tempfile

Can I save my Findstr result in a variable?

I have this CSV:
The;Quick;Brown;Fox
I know how to get the String:
findstr /v "*" brownfox.csv
But how can i safe this string in a variable without using a For loop.
I want to process the string here to get the token count %i%
set var1=(the string from brownfox.csv)
set var2=%var1%
set i=0
:loopprocess
for /F "tokens=1* delims=;" %%A in ( "%var1%" ) do (
set /A i+=1
set var1=%%B
goto loopprocess )
echo The string contains %i% tokens.
I just need the whole text in the CSV in a variable.
I'm not sure why you cannot use for, but if you absolutely cannot use it, you need a temporary file:
>%TEMP%\foo.tmp findstr /v "*" brownfox.csv
<%TEMP%\foo.tmp set /p "var1="
However, from what you describe you don't need the variable at all. And you use a for loop anyway. Why not simply do
for /f "tokens=1* delims=;" %%A in ('findstr /v "*" brownfox.csv') do (
set /a i+=1
set "var1=%%B"
)
<brownfox.csv set /p "line="
FOR %%G IN (%line%) DO set /a count+=1
echo The string contains %count% tokens.
first line to get the (first line of) the file, second to count the tokens, third to write it to screen.
Just one other option for you. This splits the string up into a pseudo array of variables and also gives you a count of the number of variables in the array.
#echo off
setlocal EnableDelayedExpansion
set i=1
set /p "x="<brownfox.csv
set "x!i!=%x:;=" & set /A i+=1 & set "x!i!=%"
set x
echo %i%
pause
This technique was discovered in this thread on DosTips.com.

remove a particular filename from the output of for loop

I have various files date wise as:
cpms_2015_09_01.txt
lms_2015_07_10.txt
kmps_2015_10_07.txt
lmps_2015_10_07.txt
cpmgs_2015_10_07.txt
I wanted to remove from the "for loop" files of today's date name
How can I do this, I wrote a code for which I can store the file paths of all files in:
XCOUNT_1=D:\check\cpms_2015_09_01.txt ...etc
But, there shouldn't be no XCOUNT for today's file name like %_2015_10_07%
I am struck at this section of code pointed below
DO ( IF NOT "_%year%_%day%_%month%.txt"=="!FTRIM_%%J!"
My code is below
SETLOCAL ENABLEDELAYEDEXPANSION
setlocal
SET /A MAXJ=1
SET /A J=1
echo %DATE%
set year=%date:~-4,4%
set year=%year: =%
set month=%date:~7,2%
set month=%month: =%
set day=%date:~4,2%
set day=%day: =%
FOR /F "usebackq tokens=*" %%i IN (`DIR /S /B D:\check\*.txt`) DO ( IF NOT "_%year%_%day%_%month%.txt"=="!FTRIM_%%J!" (
SET XCOUNT_!J!=%%~i
SET FNAME_!J!=%%~ni
SET MAXJ=!J!
SET /A J+=1
SET FTRIM_!nxi!=%%~nxi:~-10,10%
PAUSE
)
)
C:\Users\pwatson\bin>type atest.bat
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A J=1
SET /A MAXJ=0
FOR /F "usebackq tokens=*" %%i IN (`DIR /S /B C:\Python27-32\*.exe`) DO (
SET XCOUNT_!J!=%%~i
SET MAXJ=!J!
SET /A J+=1
)
SET XCOUNT
This produces:
C:\Users\pwatson\bin>call atest.bat
XCOUNT_1=C:\Python27-32\python.exe
XCOUNT_10=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\t64.exe
XCOUNT_11=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\w32.exe
XCOUNT_12=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\w64.exe
XCOUNT_13=C:\Python27-32\Lib\site-packages\setuptools\cli-32.exe
XCOUNT_14=C:\Python27-32\Lib\site-packages\setuptools\cli-64.exe
XCOUNT_15=C:\Python27-32\Lib\site-packages\setuptools\cli-arm-32.exe
XCOUNT_16=C:\Python27-32\Lib\site-packages\setuptools\cli.exe
XCOUNT_17=C:\Python27-32\Lib\site-packages\setuptools\gui-32.exe
XCOUNT_18=C:\Python27-32\Lib\site-packages\setuptools\gui-64.exe
XCOUNT_19=C:\Python27-32\Lib\site-packages\setuptools\gui-arm-32.exe
XCOUNT_2=C:\Python27-32\pythonw.exe
XCOUNT_20=C:\Python27-32\Lib\site-packages\setuptools\gui.exe
XCOUNT_21=C:\Python27-32\Scripts\easy_install-2.7.exe
XCOUNT_22=C:\Python27-32\Scripts\easy_install.exe
XCOUNT_23=C:\Python27-32\Scripts\pip.exe
XCOUNT_24=C:\Python27-32\Scripts\pip2.7.exe
XCOUNT_25=C:\Python27-32\Scripts\pip2.exe
XCOUNT_3=C:\Python27-32\w9xpopen.exe
XCOUNT_4=C:\Python27-32\Lib\distutils\command\wininst-6.0.exe
XCOUNT_5=C:\Python27-32\Lib\distutils\command\wininst-7.1.exe
XCOUNT_6=C:\Python27-32\Lib\distutils\command\wininst-8.0.exe
XCOUNT_7=C:\Python27-32\Lib\distutils\command\wininst-9.0-amd64.exe
XCOUNT_8=C:\Python27-32\Lib\distutils\command\wininst-9.0.exe
XCOUNT_9=C:\Python27-32\Lib\site-packages\pip\_vendor\distlib\t32.exe
Getting these back into the parent environment is another issue. I hope that dbenham might respond.
First of all, you can't have all of that code on the same line like that unless you tell batch where one command stops and the next command begins. Secondly, the code you have will only work on the command prompt and not in a batch file, because you need two %s when using for loop variables inside of batch files. Third, set /a is only used for math; use a regular set command when storing a string value.
Finally (and most importantly), you need delayed expansion to properly iterate your counter variable inside of your for loop.
This batch script should work:
#echo off
setlocal enabledelayedexpansion
set J=1
for /R "D:\TEST" %%I in (*.txt) do (
set XCOUNT_!J!=%%I
set /a J+=1
)
If you want to exclude a specific file from the output, wrap the set code in an if statement.
#echo off
setlocal enabledelayedexpansion
set J=1
for /R "D:\TEST" %%I in (*.txt) do (
if not "%%~nxI"=="test.txt" (
set XCOUNT_!J!=%%I
set /a J+=1
)
)

Batch: Checking if any variables are equal

Thanks to Aacini, I now have a way to sort variables from greatest to least.
Link:
Comparing and ordering multiple numbers in batch
However, if 2 or more of my variables are the same value, they won't be sorted. I'm trying test to see if two variables in the set are equal. I tried using if statements against each variable in any combination I could think of, but that isn't very efficient and is hard to change.
Is there a way I can achieve this?
#echo off
setlocal EnableDelayedExpansion
set speed1=190
set speed2=78
set speed3=78
set speed4=23
rem Get the descending order of previous elements via "order" array
for /L %%i in (1,1,4) do (
set /A num=1000-speed%%i
set order!num!=%%i
)
rem Show the elements of "speed" array in descending order
for /F "tokens=2 delims==" %%i in ('set order') do (
echo speed%%i = !speed%%i!
)
Output will only display:
speed1 = 190
speed3 = 78
speed4 = 23
Excuse me. I don't know if you are really interested to know if two elements have the same value, or just to fix the bug of my previous solution (that don't include the elements with the same value), so I opted for solve previous bug:
#echo off
setlocal EnableDelayedExpansion
set speed1=190
set speed2=78
set speed3=78
set speed4=23
rem Get the descending order of previous elements via "order" array
REM Insert a second index to differentiate elements with the same value
for /L %%i in (1,1,4) do (
set /A num=1000-speed%%i
set order[!num!][%%i]=%%i
)
rem Show the elements of "speed" array in descending order
for /F "tokens=2 delims==" %%i in ('set order') do (
echo speed%%i = !speed%%i!
)
#echo off
setlocal enableextensions enabledelayedexpansion
set speed1=190
set speed2=78
set speed3=78
set speed4=23
for /f "usebackq tokens=1,2 delims=/" %%a in (`
cmd /q /e /c "for /f tokens^=1^,2^ delims^=^= %%c in ('set speed') do (set /a %%d + 10000000 & echo /%%c)"
^| sort /r
`) do (
set /a "value=%%a-10000000"
echo %%b=!value!
)

Resources