Redirect Environment variables in a batch file - batch-file

I have three variables lets say SOURCE_SUCCESS, JULIANDAYS, APPROVEREJECT
Value of SOURCE_SUCCESS=a,b,c,d,e,f
Value of JULIANDAYS=1,2,3,4,5
Value Of APPROVEREJECT=A,R,A,A,R
Now what i want to do it here is to print the values of the three variables in the format as shown below using batch file commands
a 1 A
b 2 R
and so on
One more point i want to redirect this to a file

This DOS-based approach works for your specific request:
#echo off
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
:ROW1
for /f "tokens=1" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=1" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=1" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% > myfile.txt
:ROW2
for /f "tokens=2" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=2" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=2" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
:ROW3
for /f "tokens=3" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=3" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=3" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
:ROW4
for /f "tokens=4" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=4" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=4" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
:ROW5
for /f "tokens=5" %%j in ('echo %SOURCE_SUCCESS%') do set col1=%%j
for /f "tokens=5" %%j in ('echo %JULIANDAYS%') do set col2=%%j
for /f "tokens=5" %%j in ('echo %APPROVEREJECT%') do set col3=%%j
echo %col1% %col2% %col3% >> myfile.txt
Output:
a 1 A
b 2 R
c 3 A
d 4 A
e 5 R
I had also tried the following to be more flexible in case you ever have a variable number of tokens, but it appears DOS rejects variables in the token specifier (whether using delayed variable expansion or not). Maybe someone else knows why?:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
REM The following doesn't work because variable as 'tokens' are rejected
for /L %%i in (1,1,5) Do (
set fieldnum=%%i
for /f "tokens=!fieldnum!" %%j in ('echo %SOURCE_SUCCESS%') do echo %%j
for /f "tokens=!fieldnum!" %%k in ('echo %JULIANDAYS%') do echo %%k
for /f "tokens=!fieldnum!" %%l in ('echo %APPROVEREJECT%') do echo %%l
)

The Batch file below work with any number of tokens/fields:
#echo off
setlocal EnableDelayedExpansion
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
set i=0
for %%a in (%SOURCE_SUCCESS%) do (
set /A i+=1
set result[!i!]=%%a
)
set /A n=i, i=0
for %%a in (%JULIANDAYS%) do (
set /A i+=1
for %%i in (!i!) do set result[%%i]=!result[%%i]! %%a
)
if %i% gtr %n% set n=%i%
set i=0
for %%a in (%APPROVEREJECT%) do (
set /A i+=1
for %%i in (!i!) do set result[%%i]=!result[%%i]! %%a
)
if %i% gtr %n% set n=%i%
for /L %%i in (1,1,%n%) do echo !result[%%i]! >> thefile.txt
This is the result:
a 1 A
b 2 R
c 3 A
d 4 A
e 5 R
f
EDIT: New method added
The Batch file below is smaller and allows an easier processing of any number of variables:
#echo off
setlocal EnableDelayedExpansion
set SOURCE_SUCCESS=a,b,c,d,e,f
set JULIANDAYS=1,2,3,4,5
set APPROVEREJECT=A,R,A,A,R
set n=0
for %%v in (SOURCE_SUCCESS JULIANDAYS APPROVEREJECT) do (
set i=0
for %%a in (!%%v!) do (
set /A i+=1
for %%i in (!i!) do set result[%%i]=!result[%%i]! %%a
)
if !i! gtr !n! set n=!i!
)
for /L %%i in (1,1,%n%) do echo !result[%%i]! >> thefile.txt

$ paste <(echo $SOURCE_SUCCESS | tr ',' '\n') <(echo $JULIANDAYS | tr ',' '\n') <(echo $APPROVEREJECT | tr ',' '\n') >output.txt
$ cat output.txt
a 1 A
b 2 R
c 3 A
d 4 A
e 5 R
f
Note: It would be easier if you put all values line by line into separated files.

Related

batch script adaption with following code

I have a Batch file with the following code:
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO for /L %%z in (1 1 %%i) do echo %%a;%%b;%%c;%%d;%%e;%%f;%%g;%%h;%%i;)
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Output:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
Now I would like to have the following Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
because in %%i has the value 3.
If in %%i the value is 4 the output should be:
TEST0;TEST1;WERT1;;;;;;4;
TEST0;TEST1;WERT2;;;;;;4;
TEST0;TEST1;WERT3;;;;;;4;
TEST0;TEST1;WERT4;;;;;;4;
etc.
#echo off
SETLOCAL EnableDelayedExpansion
set "quelle=t.csv"
set "ziel=t1.csv"
>"%ziel%" (
for /f "usebackq tokens=1,2,* delims=;" %%a in ("%quelle%") DO (
set "i=0"
for %%k in (%%c) do set "nr=%%k"
for %%m in (%%c) do (
set /a i+=1
if !i! leq !nr! echo %%a;%%b;%%m;;;;;;!nr!;
)
)
)
type "%ziel%"
Input:
TEST0;TEST1;WERT1;WERT2;WERT3;WERT4;WERT5;WERT6;3;
TEST0;TEST1;WERTa;WERTb;WERTc;WERTd;WERTe;WERTf;4;
Output:
TEST0;TEST1;WERT1;;;;;;3;
TEST0;TEST1;WERT2;;;;;;3;
TEST0;TEST1;WERT3;;;;;;3;
TEST0;TEST1;WERTa;;;;;;4;
TEST0;TEST1;WERTb;;;;;;4;
TEST0;TEST1;WERTc;;;;;;4;
TEST0;TEST1;WERTd;;;;;;4;
#echo off
set "quelle=C:\Users\User-01\quelle_001.txt"
set "ziel=C:\Users\User-01\ziel_001.csv"
>"%ziel%" (for /f "usebackq tokens=1-9 delims=;" %%a in ("%quelle%") DO (
echo %%a;%%b;%%c;;;;;;%%i;
echo %%a;%%b;%%d;;;;;;%%i;
echo %%a;%%b;%%e;;;;;;%%i;
echo %%a;%%b;%%f;;;;;;%%i;
echo %%a;%%b;%%g;;;;;;%%i;
echo %%a;%%b;%%h;;;;;;%%i;
)
)
I've removed the For /L loop as it's purpose isn't clear, and the %%z value does not get used. The only thing it appears to do is cause the same line to be echoed multiple times depending on the value of the %%i token retrieved in the first loop.
This seems as if it will do what your samples show, is it what you mean?
#Set "quelle=C:\Users\User-01\quelle_001.txt"
#Set "ziel=C:\Users\User-01\ziel_001.csv"
#(For /F "UseBackQ Tokens=1-3,9 Delims=;" %%G In ("%quelle%") Do #For /F "Delims=0123456789" %%K In ("%%I") Do #For /L %%L In (1 1 %%J) Do #Echo %%G;%%H;%%K%%L;;;;;;%%J;)>"%ziel%"

batch file: Parse string after substring occurence, not delimeter

I have and batch file script shows me output of adb utility.
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO echo %%x
2 output lines
0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12
F9NPFP084096 device product:WW_P023 model:P023 device:P023_1 transport_id:11
I want parse non-space substring after "transport_id:".
For the first line i want get 12, for second 11. How can I do that?
If I understand correctly what you've asked, the following batch-file should output the information you require.
#Echo Off
For /F "Skip=1 Tokens=1*" %%G In ('adb.exe devices -l') Do (Set "DI=%%H"
SetLocal EnableDelayedExpansion
For /F %%I In ("!DI:*transport_id:=!") Do EndLocal & Echo %%I)
Pause
And, if you wanted to do that from cmd:
For /F "Skip=1Tokens=1*" %G In ('adb.exe devices -l')Do #Set "DI=%H"&For /F %I In ('Cmd /D/V/C Echo "!DI:*transport_id:=!"')Do #Echo %~I
You may easily extract all variables no matter their positions and show what you want:
#echo off
setlocal EnableDelayedExpansion
FOR /F "skip=1 delims=" %%a IN ('adb devices -l') DO (
for %%b in (%%a) do for /F "tokens=1,2 delims=:" %%x in ("%%b") do set "%%x=%%y"
echo Product=!product!
echo Transport=!transport_id!
)
For example:
Product=java_joyplus_qb7
Transport=12
Product=WW_P023
Transport=11
SETLOCAL EnableDelayedExpansion
REM …
FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO (
set "_ID="&set "_transport_id="
call :parse "" "%%~x"
echo transport_id !_transport_id! (!_ID!^)
)
REM … remaining part of your script here …
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Tested using the following script with hard-coded hypothetical adb output (not having adb installed):
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
(
REM last token
set "_ID="&set "_transport_id="
call :parse "" "0123456789ABCDEF device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12"
echo transport_id !_transport_id! (!_ID!^)
REM penult token
set "_ID="&set "_transport_id="
call :parse "" "F9NPFP084096 device product:WW_P023 model:P023 transport_id:11 device:P023_1"
echo transport_id !_transport_id! (!_ID!^)
REM elsewhere token
set "_ID="&set "_transport_id="
call :parse "" "abc123def567 transport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
REM nowhere token
set "_ID="&set "_transport_id="
call :parse "" "noTransportId sport_id:10 device product:XX_P023 model:P023 device:P023_2"
echo transport_id !_transport_id! (!_ID!^)
)
goto :eof
:parse
if "%~2"=="" goto :eof
for /F "tokens=1,*" %%G in ("%~2") do (
if "%~1"=="" (
set "_ID=%%~G"
call :parse "%%~G" "%%~H"
) else (
for /F "tokens=1,2 delims=: " %%g in ("%~2") do (
if /I "%%~g"=="transport_id" (
set "_transport_id=%%~h"
) else (
call :parse "%%~G" "%%~H"
)
)
)
)
goto :eof
Output: D:\bat\SO\61588773.bat
transport_id 12 (0123456789ABCDEF)
transport_id 11 (F9NPFP084096)
transport_id 10 (abc123def567)
transport_id (noTransportId)

Why my batch script doesn´t replace correctly?

I have this script to replace some words at text file.
I don't know if is correct:
#echo off
setlocal EnableDelayedExpansion
set count=0
set j=0
set i=0
for /f "tokens=* delims=" %%c in ( '"type test.txt"') do (
SET string=%%b
echo %%c >>test2.txt
)
for /F %%a in (params.txt) do (
set array[!i!]=%%a
set /A i+=1
)
for /F "tokens=1 delims==" %%p in (params.txt) do (
set array2[!j!]=%%p
set /A j+=1
)
set /A i-=1
for /l %%r in (%count% , 1, %i%) do (
set replace=!array2[%%r]!
echo %repace%
set replaced=!array[%%r]!
echo replaced
set /A count+=1
call :reemplaza
)
:reemplaza
if %count% LSS %i% (
for /f "tokens=* delims=" %%b in ( '"type test2.txt"') do (
SET string=%%b
SET modified=!string:%replace%=%replaced%!s
echo.!modified! >>auxiliar.txt
)
pause
del test2.txt
ren auxiliar.txt test2.txt
)
First loop is correct but at second loop do this:
I wanna replace:
name=awachens for name=awachenss
So take next parameter and print:
name=awachens=surname=lol
Regards !

How to use parameter 2 in a for loop nested in Batch file?

I could not use the second parameter(%%b) in the first for loop to compare (if !count! GTR %%b) in the for second loop, hoping the experts to help, sorry about my English is not good.
File text1.txt
# #
#Employee*.PDF,3
School*.PDF,4
Family*.PDF,5
Folder c:\user\text
xxxxxxxxx.pdf
Employee1.pdf
Employee3.pdf
Employee2.pdf
Employee4.pdf
Employee5.pdf
Employee6.pdf
Employee7.pdf
School1.pdf
School3.pdf
School2.pdf
School4.pdf
School5.pdf
School6.pdf
School7.pdf
Total Code:
#Echo off
#SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
for /f %%x in ('dir C:\user\text\%%a /b') do (
set /a count+=1
if !count! GTR %%b (
del "C:\user\text\%%x"
)
)
)
I cannot use %%b in the second for loop.
output after run All files are deleted.
but i just want to delete :
Employee4.pdf
Employee5.pdf
Employee6.pdf
Employee7.pdf
School5.pdf
School6.pdf
School7.pdf
Help me, please!
I tried... add set /a flag=%%b and change Comparative conditions = if !count! GTR !flag!
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
set /a flag=%%b
for /f %%x in ('dir C:\user\text\%%a /b') do (
set /a count+=1
if !count! GTR !flag! (
del "C:\user\text\%%x"
)
)
)
But output !flag! = 5,
equal to its final value Family*.PDF,5.
Your first code seems to work fine (I added some code to add demo files and a minor change in the dir command to match environment - adapt to your needs):
#echo off
setlocal enabledelayedexpansion
REM create test environment:
for /l %%a in (1,1,7) do break>School%%a.pdf
for /l %%a in (1,1,7) do break>Family%%a.pdf
dir /b *.pdf
echo ---------
type text1.txt
echo ---------
pause
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
for /f %%x in ('dir %~dp0%%a /b') do (
set /a count+=1
if !count! GEQ %%b echo "%%~fx"
)
)
Output:
Family1.pdf
Family2.pdf
Family3.pdf
Family4.pdf
Family5.pdf
Family6.pdf
Family7.pdf
School1.pdf
School2.pdf
School3.pdf
School4.pdf
School5.pdf
School6.pdf
School7.pdf
---------
# #
#Employee*.PDF,3
School*.PDF,4
Family*.PDF,5
---------
Drücken Sie eine beliebige Taste . . .
"D:\tmp\test\School4.pdf"
"D:\tmp\test\School5.pdf"
"D:\tmp\test\School6.pdf"
"D:\tmp\test\School7.pdf"
"D:\tmp\test\Family5.pdf"
"D:\tmp\test\Family6.pdf"
"D:\tmp\test\Family7.pdf"
Oh yes - and I changed GTR to GEQ to match your example.
The dir command needs a wildcard following %%a
for /f %%x in ('dir /b "C:\user\text\%%a*"') do (
Also %%b read from text1.txt isn't just the number, but has the extension .pdf attached.
Either use
if !count! GTR %%~nb to strip off the extension, or
modify test1.txt to have another comma between the number and the extension.

Windows Batch: If condition is not validated

Here is my code
ECHO off
CLS
ECHO List of VMs:
ECHO .............
cd /D "F:\VMs"
setlocal enabledelayedexpansion
set num=0
for /f "tokens=*" %%i in ('dir /s/b *.vmx') do set /a num+=1&set VM[!num!]=%%i
set numberOfVMs=0
for /F "tokens=2 delims==" %%s in ('set VM[') do (
set /a numberOfVMs+=1
echo !numberOfVMs! -^> %%s
)
ECHO.
ECHO List of Running VMs:
ECHO .....................
set num=-1
for /f "tokens=*" %%i in ('vmrun -T ws list') do (
set /a num+=1
if !num! NEQ 0 set RUNNINGVM[!num!]=%%i
)
set numberOfRunningVMs=0
for /F "tokens=2 delims==" %%s in ('set RUNNINGVM[') do (
set /a numberOfRunningVMs+=1
echo !numberOfRunningVMs! -^> %%s
)
ECHO.
ECHO List of Available VMs:
ECHO .......................
for /l %%x in (1, 1, %numberOfVMs%) do (
::echo !VM[%%x]!
for /l %%y in (1, 1, %numberOfRunningVMs%) do (
echo !VM[%%x]!
echo !RUNNINGVM[%%y]!
if "!VM[%%x]!"=="!RUNNINGVM[%%y]!" (
echo "success"
)
)
)
Array varibale 'VM' has below values:
F:\VMs\PD-UI-Tests-Win10-x64-Office2016-32bit.vmwarevm\Windows-10-x64.vmx
F:\VMs\PD-UI-Tests-Win10-x64-Office2016-64bit.vmwarevm\Windows-10-x64.vmx
F:\VMs\PD-UI-Tests-Win7-x64-Office2013-32bit.vmwarevm\Windows7-x64-RTM.vmx
Array varibale 'RUNNINGVM' has below values:
F:\VMs\PD-UI-Tests-Win7-x64-Office2013-32bit.vmwarevm\Windows7-x64-RTM.vmx
But, the final if condition never becomes 'true' although the VM[3] is equal to RUNNINGVM[1]. What am i missing? Please help
Don't use ::-style comments within a code-block as it is actually a broken label, and labels break code-blocks. Use rem instead.

Resources