batch script adaption with following code - batch-file

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%"

Related

Adding a increasing value

I know this one is an easy one, but it's driving me nuts
Need help with setting up a counter to increase by 1
I have a txt file with a 100x timestamp and when I enter 1000 all I get is 1001
and I'm trying to get 1001,1002,1003
Here is the script I am using
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
SET /a TimeCount=%Stamp%
SET /a TimeCount+=1
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: %TimeCount%,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
Thanks guys
Thank you Magoo for pointing out !Timecount!
Here is the correct way to set up the script to increase by 1
This script will add to your number if you enter 100 and you have 10 repeated codes within the same file you will get 101,102,103,...110, etc...
Now the adding will carry over to all other files so..
If you have 2 files in the same folder and each have 10 repeated codes each the script will count from 101 to 120
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
SET /a TimeCount=%Stamp%
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
SET /a TimeCount+=1
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: !TimeCount!,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
)
Now by moving this SET /a TimeCount=%Stamp% under the For Loop
you will 2 results each file will get 101 to 110
#ECHO Off
setlocal ENABLEDELAYEDEXPANSION
#For %%G In ("%~dp0New Folder1") Do Set "sourcedir=%%~fG"
#For %%G In ("%~dp0New Folder2") Do Set "destdir=%%~fG"
Set /p Stamp=Enter TimeStamp:
FOR /f "delims=" %%q IN ('dir /b /a-d "%sourcedir%\*.txt"') DO (
SET /a TimeCount=%Stamp%
(
for /F "tokens=1* delims=:" %%b in ('findstr /N "^" "%sourcedir%\%%q"') do (
set "line=%%c"
if defined line (
IF "%%c" equ "!line:timestamp=!" (ECHO %%c) ELSE (
SET /a TimeCount+=1
for /F "tokens=1* delims=:" %%v in ("%%c") do ECHO %%v: !TimeCount!,
)
) ELSE ECHO(
)
)>"%destdir%\%%q"
)

CMD: extract variable string from .txt file

I have myfile.txt containing this:
line 1: keyword=string
line 2
line 3
I need to echo that variable string via constant keyword.
And this string I need is always in the end of the 1st line.
So far I have:
for /f "usebackq tokens=*" %%a in ("myfile.txt") do (
for /f "usebackq tokens=*" %%b in ('findstr /i "keyword=" %%a') do (
echo %%b & goto 108
)
)
:108
But I guess that findstr syntax is completely wrong.
Please, help.
You can do rhis very simply as:
Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
For /F "tokens=*" %%_ IN ('Type "c:\my\path\myfile.txt" ^| FIND /I "keyword="
) DO (
SET "Tmp_Line=%%_"
For /F "Tokens=* delims==" %%A in ('ECHO=!Tmp_Line:*keyword=!') DO (
echo %%a
GOTO :108
Pause
)
GOTO :EOF
)
Goto :EOF

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.

Redirect Environment variables in a 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.

Resources