Need advise on the Batch Script with the output: - windows

Need your expert advice on the below:
Here is the code:
Note: serial is set to value of 100 which is default and it is pulled from the another script where all the server details are stored.
setlocal enabledelayedexpansion
SET serverlist=
SET env=TBD
if /I "%2" EQU "D" (set env=dev&& set env_dir=dev)
if /I "%2" EQU "U" (set env=uat&& set env_dir=uat)
if /I "%2" EQU "P" (set env=prod&& set env_dir=prod)
echo Here are the server details for %env% %1:
echo These are for DEV:
FOR /l %%a IN (1,1,50) DO (if defined %env%_%serial%_DEVser_%%a_ (for /f "tokens=2,3,4 delims==, " %%a in ('set %env%_%serial%_DEVser_%%a_') do (if %%c NEQ dup (echo %%a ^(%%b^,%%c^) & set serverdetails=!serverdetails! %%a %%b %%c) )))
echo These are for UAT:
FOR /l %%a IN (1,1,50) DO (if defined %env%_%serial%_UATser_%%a_ (for /f "tokens=2,3,4 delims==, " %%a in ('set %env%_%serial%_UATser_%%a_') do (if %%c NEQ dup (echo %%a ^(%%b^,%%c^) & set serverdetails=!serverdetails! %%a %%b %%c) )))
echo These are for PROD:
FOR /l %%a IN (1,1,50) DO (if defined %env%_%serial%_PRODser_%%a_ (for /f "tokens=2,3,4 delims==, " %%a in ('set %env%_%serial%_PRODser_%%a_') do (if %%c NEQ dup (echo %%a ^(%%b^,%%c^) & set serverdetails=!serverdetails! %%a %%b %%c) )))
echo details of all the server:
echo %serverdetails%
Current output:
These are for DEV:
D00123 (testing1)
D00456 (testing2)
D00789 (testing3)
These are for UAT:
UAT001 (UAT-env1)
UAT002 (UAT-env2)
UAT003 (UAT-env3)
These are for PROD:
PRD001 (PRD-env1)
PRD002 (PRD-env2)
PRD003 (PRD-env3)
details of all the server:
D00123 testing1 D00456 testing2 D00789 testing3 UAT001 UAT-env1 UAT002 UAT-env2 UAT003 UAT-env3 PRD001 PRD-env1 PRD002 PRD-env2 PRD003 PRD-env3
Question:
For the details of all the server output: I would like to get the output below
D00123
D00456
D00789
UAT001
UAT002
UAT003
PRD001
PRD002
PRD003
If the Dev server details are requested, then UAT / PROD details should not be visible
similarly, if PROD is requested, then DEV and UAT should not be visible.
can you please help me with this?

#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
:: Dummy data established outside of routine
SET "serial=100"
SET "ser_=xyz"
SET "dev_%serial%_dev%ser_%1_=D00123 something1,testing1"
SET "dev_%serial%_dev%ser_%16_=D00456 something2,testing2"
SET "dev_%serial%_dev%ser_%22_=D00789 something3,testing3"
SET "dev_%serial%_dev%ser_%35_=D00987 something4,dup"
SET "uat_%serial%_uat%ser_%1_=UAT123 something11,testing4"
SET "uat_%serial%_uat%ser_%16_=UAT456 something12,testing5"
SET "uat_%serial%_uat%ser_%22_=UAT789 something13,dup"
SET "uat_%serial%_uat%ser_%35_=UAT987 something14,testing7"
SET "prod_%serial%_prod%ser_%1_=PRD123 something21,dup"
SET "prod_%serial%_prod%ser_%16_=PRD456 something22,testing9"
SET "prod_%serial%_prod%ser_%22_=PRD789 something23,testing10"
SET "prod_%serial%_prod%ser_%35_=PRD987 something24,testing11"
SET |FIND /i "something"
IF /i "%1"=="d" SET "env=dev"
IF /i "%1"=="u" SET "env=uat"
IF /i "%1"=="p" SET "env=prod"
for %%e in (dev uat prod) do (
SET "report="
if /i "%%e"=="%env%" SET "report=Y"
IF DEFINED report echo These are for %env%:
FOR /l %%o IN (1,1,50) DO if defined %%e_%serial%_%%e%ser_%%%o_ (
for /f "tokens=2,3,4 delims==, " %%u in (
'set %%e_%serial%_%%e%ser_%%%o_'
) do if %%w NEQ dup set "%%eserverdetails=!%%eserverdetails! %%u"&IF DEFINED report echo %%u
)
SET "serverdetails=!serverdetails! !%%eserverdetails!"
)
echo These are for ALL:
FOR %%e IN (%serverdetails%) DO ECHO %%e
GOTO :EOF
Still not really clear on the values in the variables. Test setup shown. Routine accepts d, u or p as first param.
Don't know what ser_ is set to.
Your code contains for..%%a within a for..%%a - decidedly not good practice.
Speaking of which, a couple of proved "good practice" principles:
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.
Prefer to avoid ADFNPSTXZ (in either case) as metavariables (loop-control variables) ADFNPSTXZ are also metavariable-modifiers which can lead to difficult-to-find bugs (See for/f from the prompt for documentation)
So...
Having established env, process the variables for each of the three possibilities (in %%e). Filter the values for %%e _ %serial% _ %%e %ser_% %%o _ where %%o is 1..50 and append to %%eserverdetails if the third token of the variable is not dup. If %%e matches %env% then set the report flag which controls whether the data is echoed.
Finally, list the serverdetails data.
Of course, it would also be possible to list !%env%serverdetails! for the individual lists, which would make the report flag and reporting the data within the %%u loop redundant.
Append each %%eserverdetails to serverdetails after each %%e is processed

Related

Batch Treat special characters as input errors

I want all non-numeric characters to be considered as input error. Alphabetic characters are well considered but not certain special characters like colons (and others ones).
Any idea how to achieve this ?
#echo off
setlocal EnableDelayedExpansion
set /a UserProfileNo=0
FOR /F "delims=" %%I IN ('dir /a:d /b "%SystemDrive%\Users\"') do (
set "UserProfileName=%%~I"
set /a UserProfileNo+=1
set "UserProfileName[!UserProfileNo!]=!UserProfileName!"
)
FOR /L %%k IN (1, 1, %UserProfileNo%) DO echo %%~k !UserProfileName[%%~k]!
set /p choice=Enter a Profile Number:
FOR %%f IN (%choice%) DO if "!UserProfileName[%%~f]!" == "" (
echo Error in the input
goto end
)
echo NO Error in the input
:end
pause
exit
Thank you
I'd offer an expansion of the code I've provided for you before, for performing this task.
The following code will list all standard local users and allow you to choose one from the list, by its numerical index. Only valid index numbers, (no other normal characters), will be accepted, including !, ", %, ^, &, ), =, |, <, and >
Using this method, it does not limit output only to user profiles located in the hardcoded path %SystemDrive%\Users, nor does it depend upon the directory name for the profile matching the account holder's name, (if a user changes their account name, their profile directory is not renamed to reflect that change).
#Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
Set "i=0"&For /F "Delims==" %%G In ('"(Set User[) 2>NUL"')Do Set "%%G="
For /F "Delims==" %%G In ('%__AppDir__%wbem\WMIC.exe OS Call /?
^| %__AppDir__%find.exe "=="') Do Set "#=%%G"
For /F Tokens^=4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount Where
"LocalAccount='TRUE'" Assoc:List /ResultRole:SID 2^>NUL'
)Do For /F Tokens^=1* %%H In ('%__AppDir__%wbem\WMIC.exe UserAccount Where
"Name='%%G'" Get SID /Value 2^>NUL^|%__AppDir__%find.exe "="'
)Do For %%I In (%%H)Do For /F "Tokens=1*Delims==" %%J In (
'%__AppDir__%wbem\WMIC.exe Path Win32_UserProfile Where
"SID='%%I' And Special!='TRUE' And LocalPath Is Not Null" Get LocalPath /Value
2^>NUL^|%__AppDir__%find.exe "="')Do For /F "Tokens=*" %%L In ("%%K")Do (
Set /A i+=1&SetLocal EnableDelayedExpansion&Echo !i!.%#:~-1%%%G
For %%M In (!i!)Do EndLocal&Set "User[%%M]Name=%%G"&Set "User[%%M]Path=%%L")
If Not Defined User[1]Name (Echo No standard users found, Press a key to end.
GoTo :EOF)
:Pick
Echo(&Set "#="
Set /P "#=Enter the number for your chosen Account Name>"
Set "#=%#:"=%"
(Set User[)|%__AppDir__%findstr.exe /BL "User[%#%]Name=">NUL||GoTo Pick
SetLocal EnableDelayedExpansion&For /F UseBackTokens^=1^,3Delims^=^" %%G In (
'"!User[%#%]Name!" "!User[%#%]Path!"')Do (EndLocal&Set "User[Name]=%%G"
Set "User[Profile]=%%H"&Set "#=")
(For /L %%G In (1,1,%i%)Do For %%H In (Name Path)Do Set User[%%G]%%H=)&Set "i="
Echo(&Echo You Selected %User[Name]% - [profile location is %User[Profile]%]
%__AppDir__%timeout.exe /T 5 /NoBreak>NUL
The last two lines are for demonstration purposes only, you would replace those with your own code, only after testing. Their inclusion was to demonstrate that a valid input entry will create two new local variables, %User[Name]%, (the %UserName% for the input index number), and %User[Profile]%, (the %UserProfile% for the input index number).
Please note: The code above should only be run and tested exactly as it exists above, with no omissions, additions, or modifications. If it fails to work as intended, I will accept changing all instances of %__AppDir__% with %SystemRoot%\System32\, before asking for support.
You can use findstr regex to restrict characters:
#Echo off
call :input rv[1] 0123456789 "enter a number"
call :input rv[2] a-zA-Z "enter a alphanumerical string"
call :input rv[3] "a-zA-Z " "enter an alpha sentence"
set rv[
Goto :Eof
:input [ReturnVar] [CharacterString] [prompt]
Setlocal EnableExtensions EnableDelayedExpansion
:retry
Set "input="
Set /P "input=%~3: "
If "!input!" == "" Goto :retry
Echo("!input!"| %__APPDIR__%findstr.exe /RXC:"[\"][%~2]*[\"]" >nul || Goto :retry
Endlocal & Set "%~1=%input%"
Exit /B 0

sorting files according to keywords, need a more database-y solution

I'm making a script that sorts video files into folders by checking for known keywords in the file. As the amount of keywords grows out of control the script becomes very slow, taking several seconds for each file to be processed.
#echo off
cd /d d:\videos\shorts
if /i not "%cd%"=="d:\videos\shorts" echo invalid shorts dir. && exit /b
:: auto detect folder name via anchor file
for /r %%i in (*spirit*science*chakras*) do set conspiracies=%%~dpi
if not exist "%conspiracies%" echo conscpiracies dir missing. && pause && exit /b
for /r %%i in (*modeselektor*evil*) do set musicvideos=%%~dpi
if not exist "%musicvideos%" echo musicvideos dir missing. && pause && exit /b
for %%s in (*) do set "file=%%~nxs" & set "full=%%s" & call :count
for %%v in (*) do echo can't sort "%%~nv"
exit /b
:count
set oldfile="%file%"
set newfile=%oldfile:&=and%
if not %oldfile%==%newfile% ren "%full%" %newfile%
set count=0
set words= & rem
echo "%~n1" | findstr /i /c:"music" >nul && set words=%words%, music&& set /a count+=1
echo "%~n1" | findstr /i /c:"official video" >nul && set words=%words%, official video&& set /a count+=2
set words=%words:has, =has %
set words=%words: , =%
if not %count%==0 echo "%file%" has "%words%" %count%p for music videos
set musicvideoscount=%count%
set count=0
set words= & rem
echo "%~n1" | findstr /i /c:"misinform" >nul && set words=%words%, misinform&& set /a count+=1
echo "%~n1" | findstr /i /c:"antikythera" >nul && set words=%words%, antikythera&& set /a count+=2
set words=%words:has, =has %
set words=%words: , =%
if not %count%==0 echo "%file%" has "%words%" %count%p for conspiracies
set conspiraciescount=%count%
set wanted=3
set winner=none
:loop
:: count points and set winner (in case of tie lowest in this list wins, sort accordingly)
if %conspiraciescount%==%wanted% set winner=%conspiracies%
if %musicvideoscount%==%wanted% set winner=%musicvideos%
set /a wanted+=1
if not %wanted%==15 goto loop
if not "%winner%"=="none" move "%full%" "%winner%" >nul && echo "%winner%%file%" && echo.
Notice the "weight value" for each keyword. It counts the total points for each category, finds the largest value and moves the file to the folder appointed to that category. It also displays the words it has found and lastly lists files it finds unsortable so I can add keywords or tweak weight values.
I have stripped the amount of folders and keywords in this sample to bare minimum. The full script has six folders and 64k size with all the keywords (and growing).
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "tempfile=%temp%\somename"
SET "categories=music conspiracies"
REM SET "categories=conspiracies music"
(
FOR /f "tokens=1,2,*delims=," %%s IN (q45196316.txt) DO (
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*%%u*" 2^>nul'
) DO (
ECHO %%a^|%%s^|%%t
)
)
)>"%tempfile%"
SET "lastname="
FOR /f "tokens=1,2,*delims=|" %%a IN ('sort "%tempfile%"') DO (
CALL :resolve %%b %%c "%%a"
)
:: and the last entry...
CALL :resolve dummy 0
GOTO :EOF
:resolve
IF "%~3" equ "%lastname%" GOTO accum
:: report and reset accumulators
IF NOT DEFINED lastname GOTO RESET
SET "winner="
SET /a maxfound=0
FOR %%v IN (%categories%) DO (
FOR /f "tokens=1,2delims=$=" %%w IN ('set $%%v') DO CALL :compare %%w %%x
)
IF DEFINED winner ECHO %winner% %lastname:&=and%
:RESET
FOR %%v IN (%categories%) DO SET /a $%%v=0
SET "lastname=%~3"
:accum
SET /a $%1+=%2
GOTO :eof
:compare
IF %2 lss %maxfound% GOTO :EOF
IF %2 gtr %maxfound% GOTO setwinner
:: equal scores use categories to determine
IF DEFINED winner GOTO :eof
:Setwinner
SET "winner=%1"
SET maxfound=%2
GOTO :eof
You would need to change the setting of sourcedir to suit your circumstances.
I used a file named q45196316.txt containing this category data for my testing.
music,6,music
music,8,Official video
conspiracies,3,misinform
conspiracies,6,antikythera
missing,0,not appearing in this directory
I believe your problem is that repeatedly executing findstr is time-consuming.
This approach uses a data file containing lines of category,weight,mask. The categories variable contains a list of the categories in order of preference (for when the score is equal)
Read the data file, assigning category to %%s, weight to %%t and mask to %%u and then do a directory-scan using the mask. This will echo a line to the tempfile in the format name|category|weight for each name-match found. dir seems to be very fast after the first scan.
The resultant tempfile will thus have one line for each filename+category plus the weight, so if a filename fits into more than one category, more than one entry will be created.
We then scan a sorted version of that file and resolve the score.
First, if the filename changes, we can report on the last filename. This is done by comparing the values in the variables $categoryname. Since these are scanned in the order %categories% then the first category in the list is chosen if there is an equivalence of scores. The scores are then reset and lastname initialised to the new filename.
We then accumulate the score into $categoryname
So - I believe that will be a bit faster.
Revision
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "tempfile=%temp%\somename"
SET "categories="rock music" music conspiracies"
REM SET "categories=conspiracies music"
:: set up sorting categories
SET "sortingcategories="
FOR %%a IN (%categories%) DO SET "sortingcategories=!sortingcategories!,%%~a"
SET "sortingcategories=%sortingcategories: =_%"
:: Create "tempfile" containing lines of name|sortingcategory|weight
(
FOR /f "tokens=1,2,*delims=," %%s IN (q45196316.txt) DO (
SET "sortingcategory=%%s"
SET "sortingcategory=!sortingcategory: =_!"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*%%u*" 2^>nul'
) DO (
ECHO %%a^|!sortingcategory!^|%%t^|%%s^|%%u
)
)
)>"%tempfile%"
SET "lastname="
SORT "%tempfile%">"%tempfile%.s"
FOR /f "usebackqtokens=1,2,3delims=|" %%a IN ("%tempfile%.s") DO (
CALL :resolve %%b %%c "%%a"
)
:: and the last entry...
CALL :resolve dummy 0
GOTO :EOF
:: resolve by totalling weights (%2) in sortingcategories (%1)
:: for each name (%3)
:resolve
IF "%~3" equ "%lastname%" GOTO accum
:: report and reset accumulators
IF NOT DEFINED lastname GOTO RESET
SET "winner=none"
SET /a maxfound=0
FOR %%v IN (%sortingcategories%) DO (
FOR /f "tokens=1,2delims=$=" %%w IN ('set $%%v') DO IF %%x gtr !maxfound! (SET "winner=%%v"&SET /a maxfound=%%x)
)
ECHO %winner:_= % %lastname:&=and%
:RESET
FOR %%v IN (%sortingcategories%) DO SET /a $%%v=0
SET "lastname=%~3"
:accum
SET /a $%1+=%2
GOTO :eof
I've added a few significant comments.
You can now have spaces in category names - you need to quote the name (for reporting purposes) within the set catagories... statement.
sortingcategories is automatically derived - it's only used for sorting and is simply the categories with any space in a name replaced by an underscore.
In creating the tempfile, the category is processed to contain underscores (the sortingcategory) and when the final placement is resolved, the underscores are removed returning the category name.
Negative weights should now be processed appropriately.
-- further revision for "not append *"
FOR /f "tokens=1-5delims=," %%s IN (q45196316.txt) DO (
SET "sortingcategory=%%s"
SET "sortingcategory=!sortingcategory: =_!"
FOR %%z IN ("!sortingcategory!") DO (
SETLOCAL disabledelayedexpansion
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\%%~v%%u%%~w" 2^>nul'
AND
add 2 extra columns to the q45196316 file
music,6,music,*,*
music,8,Official video,"",*
conspiracies,3,misinform,*,*
conspiracies,6,kythera,*anti,*
missing,0,not appearing in this directory,*,*
rock music,2,metal,*,*
conspiracies,-5,negative,*,*
The for /f ... %%s now generates %%v and %%w containing the last two columns (as tokens is nor 1-5)
These are applied as prefix and suffix to %%u in the dir command. Note that "" should be used for nothing as two successive , are parsed as a single separator. The ~ before the v/w in %%~v means remove the quotes.

Bypass native variables sorting in a batch FOR loop

I need to read an arbitrary number of variable "names" from one file, and later assign them "values" supplied by another file, with both sources not available at once. I tried using the code below, but the problem is, SET command natively sorts variables alphabetically, thus preventing correct value assignments. Is there an alternative approach to set variables in this case, or a way to block native Cmd vars sorting by SET? I don't want setting numbered variable arrays if possible, as they complicate the code by adding extra layer of variables:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=1" %%i in (%args1_file%) do (
set "%%i=0" & set "%%i=_%%i")
for /f "tokens=1 delims==" %%i in ('set _') do (
for /f "tokens=1" %%j in (%args2_file%) do (
set "%%i=%%j"
if not !%%i! equ 0 (echo %%i = %%j
) else (set /p "%%j=Enter %%i > " 2>nul)
call :validate
)
:: more code using vars %%i
exit /b
:validate
Assuming you mean the sorted order returned by set, there's no way around it.
It's not documented anywhere I can see, but the environment variable block maintained by GetEnvironmentStrings() and friends is maintained in sorted order, at least in every NT OS I've seen, and probably before then. When you add a new string to it's list, it's added in sorted position, so the order of addition is lost by the system.
I think you can set the variables based off names in one file with values in another file by:
#echo off
setlocal enabledelayedexpansion
set _i=0
for /f "tokens=1" %%i in (names.txt) do (
set _val_!_i!=%%i
set /a _i=!_i!+1
)
set _i=0
for /f "tokens=1" %%i in (vals.txt) do (
set _temp=_val_!_i!
call set __%%!_temp!%%=%%i
set /a _i=!_i!+1
)
echo one == !__one!
echo two == !__two!
echo three == !__three!
I found the approach that doesn't use a numbered array to read var names and values from separate files, and won't cause vars sorting to ensure correct value assignments:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=1" %%i in (%args1_file%) do (
set "%%i=0" & set "vars=!vars! _%%i")
for %%i in (!vars!) do (
for /f "tokens=1" %%j in (%args2_file%) do (
if not %%j.==. set %%i=%%j)
if not !%%i! equ 0 (echo %%i = %%j
) else (set /p "%%j=Enter %%i > " 2>nul)
call :validate
)
:: more code using vars %%i
exit /b

How to sort file names numerically in windows command line?

The windows explorer does display in correct order. For example: filename1.txt, filename2.txt, filename11.txt etc. When I tried the DIR command with different options could not get the same behaviour. It displays filename1.txt, filename11.txt, filename2.txt.
Is there any way to sort it so that numeric ordering is preserved?
#ECHO Off
:: dir list in numeric-value order
SETLOCAL
:: %1= directoryname; default current directory
SET "targetdir=%~1"
IF NOT DEFINED targetdir SET "targetdir=.\*.*"
:: remove variables starting $
FOR /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET /a maxnumlength=0
SET "manyzeroes=0000000000000000"
SET "manyzeroes=%manyzeroes%%manyzeroes%%manyzeroes%%manyzeroes%"
:: analyse list of filenames
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%" '
) DO CALL :detnlen "%%a"
:: build list of filenames
FOR /f "delims=" %%a IN (
'dir /b /a-d "%targetdir%" '
) DO CALL :setname "%%a"
FOR /F "tokens=1*delims=:" %%a In ('set $ 2^>Nul') DO ECHO %%b
GOTO :EOF
:detnlen
SET "name=%~1"
SET /a numlength=0
SET "action=L"
:detnloop
IF %action%==F SET "action=B"
SET "post=%name:~0,1%"&SET "name=%name:~1%"
IF "%post%" geq "0" IF "%post%" leq "9" (
SET /a numlength+=1
SET "action=F"
)
IF %action% neq B IF DEFINED name GOTO detnloop
IF %numlength% gtr %maxnumlength% SET /a maxnumlength=%numlength%
GOTO :EOF
:setname
SET "name=%~1"
SET "pre="
SET "nums="
SET "action=L"
:snloop
IF %action%==F SET "action=B"
SET "post=%name:~0,1%"&SET "name=%name:~1%"
IF "%post%" geq "0" IF "%post%" leq "9" (
SET /a numlength+=1
SET "action=F"
SET "nums=%nums%%post%"
)
IF %action%==L SET "pre=%pre%%post%"&SET "post="
IF %action% neq B IF DEFINED name GOTO snloop ELSE SET "post="
IF %action%==F SET "post="
IF DEFINED nums SET "nums=%manyzeroes%%nums%"
IF DEFINED nums CALL SET "nums=%%nums:~-%maxnumlength%%%"
SET "$%pre%%nums%%post%%name%=:%~1"
GOTO :EOF
Oh, my!
There are no inbuilt switches to show the directory in that odd way, but this has all the hallmarks of a challenge.
OK - it's insanely slow, but it appears to work.
The principle is to determine the longest sequence of initial-numerics as maxnumlength then assign the name of the detected file to an environment variable starting $ and with a name constructed from the filename with the numeric part leading-zero-padded. set $ then lists the $ variables in alphabetic order; select the value, which is the original filename.

Print array element value in a Batch file

There is a problem with printing array element values in the below code:
#echo off
setlocal enabledelayedexpansion enableextensions
for /F "tokens=2,3 delims= " %%a in ('findstr "associationMaxRtx maxIncomingStream maxOutgoingStream initialAdRecWin maxUserdataSize mBuffer nThreshold PathMaxRtx maxInitialRtrAtt minimumRto maximumRto initialRto rtoAlphaIndex tSack" C:\Users\ephajin\logs.txt') do (
set /A count+=1
set vartmp1=%%a
set vartmp2=%%b
set "array[!count!]="%%a %%b""
)
(for /L %%i in (1,1,%count%) do echo !array[%%i]!
) > result.txt
in the result file I get the output
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
It does not print the array values.
The problem is probably due to setlocal enabledelayedexpansion but how do you correct it?
FOR /L %%a IN (1,1,4) DO ECHO !array[%%a]!
FOR /f "tokens=1*delims==" %%a IN ('set array[') DO ECHO %%b
Either of these two lines should show you what you appear to require.
Since the first is identical in effect to your code, I suspect that the array[*] array isn't being established correctly. you can check this by executing
set array[
to show precisely what has been set. Actually,
set
should show you all defined user-variables.
set|more
would show the same, but allow you to page through them.
SET "result="
FOR /f "tokens=1*delims==" %%a IN ('set array[') DO SET "result=!result! %%b"
ECHO result: "%result%" or "%result:~1%"
echo===============
SET "result="
FOR /L %%a IN (1,1,4) DO SET "result=!result! !array[%%a]!"
ECHO result: "%result%" or "%result:~1%"
Two methods of setting result - the list of the values in the array. Naturally, the space in the set instruction could be almost any character you desire - comma, for instance. The result is shown both with a leading space and with that space removed.

Resources