Batch file - dynamically display huge list of variables? - batch-file

I want to make a 100x100 grid of variables that are similar to this (name of variables):
1x1 1x2 1x3
2x1 2x2 2x3
3x1 3x2 3x3
And so on, all the way to 100x100. Is there an easy way to do this without having to type in echo for 10,000 variables, and just use some for loop?
Keep in mind I'm wanting to display a row of 100, then the next row of 100, so they're not all in one long list, unless I set the mode to 100x100? I know this is really strange, but I'm trying to see if I can make a graphing function within batch files.
Thank you.
EDIT:
How can I use the for /L loop to echo multiple variables in one line?
for /L %%a in (0, 1, 5) do echo %%a
Desired output:
0 1 2 3 4 5
Actual output:
0
1
2
3
4
5

You may concatenate several values in the same variable (that represent a row of values), and then just show it in a simple echo command:
#echo off
setlocal EnableDelayedExpansion
rem Get NxM dimensions from Batch file parameters
set /a N=%1, M=%2
rem Create the two-dimensional array with 4-digits random numbers
for /L %%i in (1,1,%N%) do (
for /L %%j in (1,1,%M%) do (
set /A "number=!random! %% 10000"
set "number= !number!"
set "a%%ix%%j=!number:~-4!"
)
)
rem Show the array line by line
for /L %%i in (1,1,%N%) do (
set "line="
for /L %%j in (1,1,%M%) do (
set "line=!line! !a%%ix%%j!"
)
echo !line!
)
Output example:
C:\> test.bat 10 15
4216 3058 9311 5626 1461 464 3926 3597 5312 5074 2797 7654 3306 5763 3359
1203 8313 8271 3591 3588 2415 6424 9730 8095 5958 8599 3062 4165 6671 6192
7140 9204 60 8649 9962 3374 1690 3500 331 6314 2579 3194 8451 6682 3202
2275 6582 877 8424 3732 2152 6741 1791 2544 2979 4763 1949 3282 5284 2578
9628 2193 4806 8505 3480 2517 6596 9029 2776 2377 6105 3007 8464 3826 2090
281 2278 2559 7318 3207 500 98 2061 8572 4653 9646 6815 5218 2067 2512
9862 8686 3945 5059 1191 947 9589 1983 8213 8246 408 5458 3286 7890 1280
1297 6154 8701 5214 769 1305 1946 3172 5201 5245 2113 2865 5866 8864 6476
1760 3050 3014 8195 1325 4029 2302 9466 5002 2622 741 8665 7090 9580 3388
5245 7004 9264 1708 4173 9041 8462 2055 9215 3809 2362 4400 1308 3411 5677

Here's how to get your desired output for your example above. Note that the quotes are not required, but recommended so that you are aware of the trailing space after %%a.
for /L %%a in (0, 1, 5) do <nul set/p "=%%a "

Related

how to select exactly part of a number as a simple character and exclude all the rest in CMD?

i will explain below what i want to do with CMD
I have this name of file:
set "File=0119315314"
with 2 in 2, i want to separete file name as this line: 01 19 31 53 14
the problem here, is the number 31 because of "53" and "14"
when its joined, we have "5314" and looks the mirror of "31" into "5314"
i tryed it:
set "New=%File:31=%"
when i exclude the number "31" I burn the characters "5314" too
now, i dont get "53" and "14" for my sequence: 01 19 31 53 14
you undestand? Please, how to solve it? is possible?
If I understand correctly, perhaps you're looking for something like this:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "f=0119315314"
Set "i=0"
For /F Delims^=^ EOL^= %%G In (
'%__AppDir__%cmd.exe /U/D/C Echo(!f!^|%__AppDir__%find.exe /V ""') Do (
Set /A "i+=1, m=i%%2"
If !m! Equ 0 (Set "s=!s!%%G ") Else Set "s=!s!%%G")
If %m% Equ 0 Set "s=%s:~,-1%"
Echo(%s%
Pause
Expected result:
01 19 31 53 14
Or perhaps you're looking for something a little simpler:
#Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set "f=0119315314"
For /L %%G In (0,2,20) Do If Not "!f:~%%G,2!" == "" Echo(!f:~%%G,2!
Pause
Expected output:
01
19
31
53
14
In this example you can increase 20 if you intend to use a string of more than twenty characters.
I read upto "with 2 in 2, i want to separete file name as this line: 01 19 31 53 14" line, because the rest is nonsense...
#echo off
setlocal
set "File=0119315314"
set "new="
:next
set "new=%new% %file:~0,2%" & set "file=%file:~2%" & if defined file goto next
echo %new:~1%
Output:
01 19 31 53 14
This method can separate any number of digits pairs; just put they in "File" variable...

If query for number sections in Batch

is there a way to make a If query for specifig
number sections in Batch?
Something Like this:
IF "Var1"=="1-10" (
do something
)
the 1-10 should stand for 1,2,3,4,5,6,7,8,9,10.
I want to make 10 queries (1-10,11-20,21-30, ... , 91-100)
Is that possible?
You can do this:
#echo off
set Var1=1
for /l %%i in (0,1,10) do if %%i==%Var1% echo 10 or below
for /l %%i in (11,1,20) do if %%i==%Var1% echo 11 - 20
for /l %%i in (21,1,30) do if %%i==%Var1% echo 21 - 30
for /l %%i in (31,1,40) do if %%i==%Var1% echo 31 - 40
for /l %%i in (41,1,50) do if %%i==%Var1% echo 41 - 50
for /l %%i in (51,1,60) do if %%i==%Var1% echo 51 - 60
for /l %%i in (61,1,70) do if %%i==%Var1% echo 61 - 70
for /l %%i in (71,1,80) do if %%i==%Var1% echo 71 - 80
for /l %%i in (81,1,90) do if %%i==%Var1% echo 81 - 90
for /l %%i in (91,1,100) do if %%i==%Var1% echo 91 - 100
Here you can change set Var1=1 to any other number and it will correspond. You can replace echo N - N with your commands.
Also, set Var1=1 can be removed from the above example if used with your code as I simply set it to demonstrate the behaviour. Here is an extract from the help when running for /? so you understand the numeric behaviour.
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)

Fetch a specific column values from a .txt file with pipe delimiter and load into a new text file using batch script

I have a text file with N number of rows and columns, whereas I need to get particular columns with their values and load it into a new text file using batch script, e.g.:
input.txt
col1|col2|col3.....col71|col72
ew|ds|343.....csdk|gfdf
xc|gh|657.....sdfs|utyy
qw|zx|345.....ffds|xzcz
output.txt
col71|col3
csdk|343
sdfs|657
ffds|345
To split text into tokens by (a) certain delimiter(s), use the for /F loop. However, this can only handle up to 31 tokens, so you cannot simply state tokens=71, but you can nest multiple loops:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "output.txt" (
rem // Split off the first 31 tokens, pass the rest to the next loop:
for /F "usebackq delims=| eol=| tokens=3,31*" %%A in ("input.txt") do (
rem // Split off the next 31 tokens, pass the rest to the next loop:
for /F "delims=| eol=| tokens=31*" %%D in ("%%C") do (
rem /* Extract the proper token from the remaining ones (remember
rem that 31 + 31 = 62 tokens have been split off before): */
for /F "delims=| eol=| tokens=9" %%F in ("%%E") do (
echo(%%F^|%%A
)
)
)
)
endlocal
If there may be empty columns, the above approach fails, because for /F treats consecutive delimiters as one. To overcome this, you could do the following:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "output.txt" (
rem // Read complete lines:
for /F usebackq^ delims^=^ eol^= %%L in ("input.txt") do (
rem // Store current line string in interim variable:
set "LINE=%%L"
setlocal EnableDelayedExpansion
rem /* Split off the first 31 tokens, pass the rest to the next loop;
rem to avoid consecutive delimiters `|`, replace every single one by
rem :`"|"`, so `||` becomes `"|""|"`; then enclose the entire result
rem within `""`, thus achieving individual tokens enclosed within `""`: */
for /F "delims=| tokens=3,31*" %%A in (^""!LINE:|="^|"!"^") do (
endlocal
rem // Split off the next 31 tokens, pass the rest to the next loop:
for /F "delims=| tokens=31*" %%D in ("%%C") do (
rem /* Extract the proper token from the remaining ones (remember
rem that 31 + 31 = 62 tokens have been split off before): */
for /F "delims=| tokens=9" %%F in ("%%E") do (
rem // Remove the previously added surrounding `""` by `~`:
echo(%%~F^|%%~A
)
)
setlocal EnableDelayedExpansion
)
endlocal
)
)
endlocal
This approach will still fail if there are already quoted field values that contain | on their own.
Linux
You could use awk -F "|" '{ print $70 "|" $2 }' input.txt > output.txt.
Usually one would probably execute cut -d"|" -f2,70 input.txt > output.txt, the only problem is that cut (as far as I know) doesn't support reordering columns.
Powershell
On Windows' powershell (also available for Linux) you can use the following snippet:
Get-Content 'input.txt' | ForEach-Object {
$array = $_.split("|")
$array[70] + '|' + $array[2]
} | Out-File 'output.txt'
The following Batch file is a general-purpose program that use a series of nested FOR /F commands that allows access to up to 177 tokens, but in a very simple way:
#echo off
setlocal EnableDelayedExpansion
rem Method to use up to 177 tokens in a FOR /F command in a simple way
rem Antonio Perez Ayala
rem Create an example file with lines with 180 tokens each
(for %%a in (A B C) do (
set "line="
for /L %%i in (1,1,180) do set "line=!line! %%a%%i"
echo !line!
)) > test.txt
set "line="
rem Load the string of tokens characters from FOR-FcharsCP850.txt file
chcp 850 > NUL
if exist FOR-FcharsCP850.txt goto readChars
echo Creating FOR-F characters file, please wait...
set "options=/d compress=off /d reserveperdatablocksize=26"
type nul > t.tmp
> FOR-FcharsCP850.txt (
set /P "=0" < NUL
rem Create 87 characters in 38..124 range for 3 FOR's with "tokens=1-28*"
set "i=0"
for /L %%i in (38,1,124) do (
set /A i+=1, mod=i%%29
if !mod! neq 0 (
call :genchr %%i
type %%i.chr
del %%i.chr
)
)
rem Create 95 characters for 3 FOR's with "tokens=1-31*"
rem This is the tokens sequence used when code page = 850
set "i=0"
for %%i in (173 189 156 207 190 221 245 249 184 166 174 170 240 169 238 248
241 253 252 239 230 244 250 247 251 167 175 172 171 243 168 183
181 182 199 142 143 146 128 212 144 210 211 222 214 215 216 209
165 227 224 226 229 153 158 157 235 233 234 154 237 232 225 133
160 131 198 132 134 145 135 138 130 136 137 141 161 140 139 208
164 149 162 147 228 148 246 155 151 163 150 129 236 231 152 ) do (
set /A i+=1, mod=i%%32
if !mod! neq 0 (
call :genchr %%i
type %%i.chr
del %%i.chr
)
))
del t.tmp temp.tmp
set "options="
:readChars
set /P "char=" < FOR-FcharsCP850.txt
set "lastToken=177"
cls
echo Enter tokens definition string in the same way of FOR /F "tokens=x,y,m-n" one
echo/
echo You may define a tokens range in descending order: "tokens=10-6" = 10 9 8 7 6
echo or add an increment different than 1: "tokens=10-35+5" = 10 15 20 25 30 35
echo Combine them: "tokens=10,28-32,170-161-3" = 10 28 29 30 31 32 170 167 164 161
echo/
echo The maximum token number is 177
:nextSet
echo/
set /P "tokens=tokens="
if errorlevel 1 goto :EOF
rem Expand the given tokens string into a series of individual FOR tokens values
set "tokensValues="
for %%t in (%tokens%) do (
for /F "tokens=1-3 delims=-+" %%i in ("%%t") do (
if "%%j" equ "" (
if %%i leq %lastToken% set "tokensValues=!tokensValues! %%!char:~%%i,1!"
) else (
if "%%k" equ "" (set "k=1") else set "k=%%k"
if %%i gtr %%j set "k=-!k!"
for /L %%n in (%%i,!k!,%%j) do if %%n leq %lastToken% set "tokensValues=!tokensValues! %%!char:~%%n,1!"
)
)
)
rem First three FOR's use as tokens the ASCII chars in 38..124 (&..|) range: 28*3 = 84 tokens + 3 tokens for next FOR
rem Next three FOR's use as tokens Extended chars: 31*3 = 93 tokens + 2 tokens for next FOR
rem based on the tokens sequence used when code page = 850
rem Total: 177 tokens
for /F "eol= tokens=1-28*" %%^& in (test.txt) do ^
for /F "eol= tokens=1-28*" %%C in ("%%B") do ^
for /F "eol= tokens=1-28*" %%` in ("%%_") do ^
for /F "eol= tokens=1-31*" %%­ in ("%%|") do ^
for /F "eol= tokens=1-31*" %%µ in ("%%·") do ^
for /F "eol= tokens=1-31" %%  in ("%%…") do (
call :getTokens result=
rem Process here the "result" string:
echo !result!
)
goto nextSet
:getTokens result=
for %%# in (-) do set "%1=%tokensValues%"
exit /B
REM This code creates one single byte. Parameter: int
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8
:genchr
if %~1 neq 26 (
makecab %options% /d reserveperfoldersize=%~1 t.tmp %~1.chr > nul
type %~1.chr | ( (for /l %%N in (1,1,38) do pause)>nul & findstr "^" > temp.tmp )
>nul copy /y temp.tmp /a %~1.chr /b
) else (
copy /y nul + nul /a 26.chr /a >nul
)
goto :eof
IMPORTANT: The series of six nested FOR /F commands use the following ASCII characters in the replaceable parameter and the character between quotes:
for /F "eol= tokens=1-28*" %%^& in (test.txt) do ^ %%^38
for /F "eol= tokens=1-28*" %%C in ("%%B") do ^ %%67 in ("66")
for /F "eol= tokens=1-28*" %%` in ("%%_") do ^ %%96 in ("95")
for /F "eol= tokens=1-31*" %%­ in ("%%|") do ^ %%173 in ("124")
for /F "eol= tokens=1-31*" %%µ in ("%%·") do ^ %%181 in ("183")
for /F "eol= tokens=1-31" %%  in ("%%…") do ( %%160 in ("133")
However, it seems that some web browser don't correctly copy-paste some extended characters. If the program don't works correctly, you should check that these characters were correctly copied and fix they if necessary. You may try to copy the lines above (in pink background) and test if they were correctly copied...
Output example:
Enter tokens definition string in the same way of FOR /F "tokens=x,y,m-n" one
You may define a tokens range in descending order: "tokens=10-6" = 10 9 8 7 6
or add an increment different than 1: "tokens=10-35+5" = 10 15 20 25 30 35
Combine them: "tokens=10,28-32,170-161-3" = 10 28 29 30 31 32 170 167 164 161
The maximum token number is 177
tokens=10-6
A10 A9 A8 A7 A6
B10 B9 B8 B7 B6
C10 C9 C8 C7 C6
tokens=10-35+5
A10 A15 A20 A25 A30 A35
B10 B15 B20 B25 B30 B35
C10 C15 C20 C25 C30 C35
tokens=10,28-32,170-161-3
A10 A28 A29 A30 A31 A32 A170 A167 A164 A161
B10 B28 B29 B30 B31 B32 B170 B167 B164 B161
C10 C28 C29 C30 C31 C32 C170 C167 C164 C161
tokens=71,3
A71 A3
B71 B3
C71 C3
If your application requires less than 177 tokens, you may modify this program and eliminate the code sections of the not required tokens; that is, with 2 FOR's you may access up to 56 tokens, with 3 up to 84, with 4 up to 115, and with 5 up to 146.
You may review a detailed explanation of this method here; you may also download (a previous version of) this program in a .zip file from this post that would allow to fix the problem of the extended characters in the six FOR /F commands in a simple way...

Nested For Loop in Batch

Ive look over many threads and cannot seem to put together a solution to my problem.
What i would like to do is use two lists to create one output.
set Client_ID=BJCH,BORG,FGMS,SVIN,JEFF,NWIL
set PartNo=1,2,9,10,12,20
for %%A in (%Client_ID%) do (
for %%B in (%PartNo%) do (
echo %%A %%B
)
)
But the output I get is:
BJCH 1
BJCH 2
BJCH 9
BJCH 10
BJCH 12
BJCH 20
BORG 1
BORG 2
BORG 9
BORG 10
BORG 12
BORG 20
FGMS 1
FGMS 2
FGMS 9
etc........
What i need is
BJCH 1
BORG 2
FGMS 9
SVIN 10
JEFF 12
NWIL 20
Any idea what I'm doing wrong? Any help is much appreciated.
Your two lists are separated ones: if you nest one for into the other one, you are multiplying the number of results. There is no way to process both lists in the same for, unless you convert the lists into two arrays and then process both arrays in the same for, that is, process the elements of the two arrays with same index. For example:
setlocal EnableDelayedExpansion
set Client_ID[1]=BJCH
set Client_ID[2]=BORG
etc...
set PartNo[1]=1
set PartNo[2]=2
etc...
for /L %%i in (1,1,6) do echo !Client_ID[%%i]! !PartNo[%%i]!
You may also simulate previous processing ("two elements with same index") this way:
#echo off
setlocal EnableDelayedExpansion
set Client_ID=BJCH,BORG,FGMS,SVIN,JEFF,NWIL
set PartNo=1,2,9,10,12,20
set i=0
for %%A in (%Client_ID%) do (
set /A i+=1, j=0
for %%B in (%PartNo%) do (
set /A j+=1
if !i! equ !j! echo %%A %%B
)
)
EDIT: Output example added
BJCH 1
BORG 2
FGMS 9
SVIN 10
JEFF 12
NWIL 20

Assessing a letter in an array element using a variable

I'm trying to get this program to get the first letter of one of the array elements using a number from a different string list. However, it is returning nothing.
#echo off
setlocal enableDelayedExpansion
set s=1234 1243 1324 1342 1432 1423 2134 2143 2314 2341 2431 2413 3214 3241 3124 3142 3412 3421 4231 4213 4321 4312 4132 4123
set num[1]=123
set num[2]=456
set num[3]=789
set num[4]=101
for %%a in (!s!) do (
set w=%%a
echo !w!
set fnum=!num[%w:~0,1%]!
echo !fnum!
)
#echo off
setlocal enableDelayedExpansion
set s=1234 1243 1324 1342 1432 1423 2134 2143 2314 2341 2431 2413 3214 3241 3124 3142 3412 3421 4231 4213 4321 4312 4132 4123
set num[1]=123
set num[2]=456
set num[3]=789
set num[4]=101
for %%a in (!s!) do (
set w=%%a
echo !w!
for /f %%g in ("num[!w:~0,1!]") do set fnum=!%%g!
echo !fnum!
)
#ECHO OFF
setlocal enableDelayedExpansion
set s=1234 1243 1324 1342 1432 1423 2134 2143 2314 2341 2431 2413 3214 3241 3124 3142 3412 3421 4231 4213 4321 4312 4132 4123
set num[1]=123
set num[2]=456
set num[3]=789
set num[4]=101
for %%a in (%s%) do (
set w=%%a
echo !w!
CALL set fnum=%%num[!w:~0,1!]%%
echo !fnum!
)
GOTO :EOF
#echo off
setlocal enableDelayedExpansion
set s=1234 1243 1324 1342 1432 1423 2134 2143 2314 2341 2431 2413 3214 3241 3124 3142 3412 3421 4231 4213 4321 4312 4132 4123
set num[1]=123
set num[2]=456
set num[3]=789
set num[4]=101
for %%a in (!s!) do (
set w=%%a
echo !w!
for /F %%w in ("!w:~0,1!") do set fnum=!num[%%w]!
echo !fnum!
)
Arrays, linked lists and other data structures in cmd.exe (batch) script

Resources