Batch Split each line into multiples lines - batch-file

My txt file looks like:
A1;A2;A3
B1;B2
I want to split it like:
A1;;;A2
A2;;;A3
A3;;;A3
B1;;;B2
B2;;;B2
The rule is: For each line take two adjacent elements and create a new output line with them, for the last element of a line: create also a new output line, but use the element twice.
Here is my try:
(
for /f "tokens=1-4 delims=;" %%a in (%FilePath%) do (
for /f "tokens=1-4 delims=;" %%x in (%FilePath%) do (
echo %%a;;;%%y
)
)
)>%FilePath_Final%
But it gives the wrong format:
A1;;;A2
A1;;;B2
B1;;;A2
B1;;;B2
How can I use batch commands to split the lines so that the expected result is obtained?
PS: A1, A2, B1 etc. are just some string examples, I can have various strings
Here is an example of a file content:
XB8998901;XB8998900;8051191;24048271;24048270
XB0134812;XB0134810;XB0134801;XB0134800
XB6312701;XB6312700
XB6314201;XB6314200
The ouput should look like:
XB8998901;;;XB8998900
XB8998900;;;8051191
8051191;;;24048271
24048271;;;24048270
24048270;;;24048270
XB0134812;;;XB0134810
XB0134810;;;XB0134801
XB0134801;;;XB0134800
XB0134800;;;XB0134800
XB6312701;;;XB6312700
XB6312700;;;XB6312700
XB6314201;;;XB6314200
XB6314200;;;XB6314200

#echo off
setlocal enabledelayedexpansion
set "last="
(for /f "delims=" %%a in (old.txt) do (
for %%b in (%%a) do (
if defined last echo !last!;;;%%b
set "last=%%b"
)
echo !last!;;;!last!
set "last="
))>new.txt
fc new.txt compare.txt
old.txt (your example file):
XB8998901;XB8998900;8051191;24048271;24048270
XB0134812;XB0134810;XB0134801;XB0134800
XB6312701;XB6312700
XB6314201;XB6314200
compare.txt (desired output from your example):
XB8998901;;;XB8998900
XB8998900;;;8051191
8051191;;;24048271
24048271;;;24048270
24048270;;;24048270
XB0134812;;;XB0134810
XB0134810;;;XB0134801
XB0134801;;;XB0134800
XB0134800;;;XB0134800
XB6312701;;;XB6312700
XB6312700;;;XB6312700
XB6314201;;;XB6314200
XB6314200;;;XB6314200
Output:
Vergleichen der Dateien new.txt und compare.txt
FC: Keine Unterschiede gefunden
(Translation: Comparing files new.txt and compare.txt; FC: no differences encountered)

Use this:
#echo off
Setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims=;" %%a in ('type pathoffile ^| find "A"') do (set 1=%%a & set 2=%%b & set 3=%%c)
for /f "tokens=1,2 delims=;" %%a in ('type pathoffile ^| find "B"') do (set 4=%%a & set 5=%%b)
set n=1
:loop
set /a m=n+1
if "!%n%:~0,1!"=="!%m%:~0,1!" (
echo !%n%!;;;!%m%! >>filepath_final
) else (
echo!%n%!;;; >>filepath_final
)
set /a n=n+1
if %m% equ 5 goto :eof
goto loop

Related

Batch remove part of string after “-1” is found or any other number “-[0-9]”

I got a file containing a string on each line like this:
fruit-apple-1.5.6
vegtable-sla-mc5-6.5-16515
extra-huh-9.5-511-515
extra-3.2
I am iterating over it and want it to remove the part of the string on the right after in find "-" + any number "-0","-1","-2","-9",...
so output should be
fruit-apple
vegtable-sla-mc5
extra-huh
extra
this is code i have but it only works with a "-" i cant combine it so it takes "-" + any number like "-1","-5","-2",...
for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
echo stripped string = %%A
)
complete code not necessary i think but in case u need it below
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set "RawPath=%~dp0"
FOR /F "USEBACKQ TOKENS=*" %%M IN ("%RawPath%/mods") DO (
REM for %%f in (*.jar) do (
Set "fileNameCheck=%%M"
for /f "delims=|" %%A in ("!fileNameCheck:-=|!") do (
Echo [46m%%A[0m
if exist "%~dp0%%A*.jar" (
REM echo [32mFound %%A "%~dp0%%A*.jar"[0m
if exist "%~dp0%%M" (
REM echo [42mUp to Date[0m [32m%%A "%~dp0%%M"[0m
) else (
for %%j in (*.jar) do (
echo %%j |find "%%A" >nul
if not errorlevel 1 (
echo [41mDifferent Version[0m [31m%%j [0m[90mNewer version[0m [32m%%M[0m
)
)
)
) else (
REM echo [31mMissing %%A[0m
)
)
)
pause
Given that the strings do not contain any of the caracters ?, *, < and >, the following script should do the trick:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_ROOT=%~dp0."
set "_FILE=mods.txt"
rem // Read file line by line:
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
rem // Store current line, initialise variables:
set "LINE=%%L" & set "PREV=-" & set "COLL=-"
setlocal EnableDelayedExpansion
rem /* Split line string at every `-` and iterate through items;
rem (`?`, `*`, `<`, `>` must not occur in the string!): */
for %%I in ("!LINE:-=" "!") do (
endlocal & set "ITEM=%%~I"
rem // Check whether item only consists of numerals and `.`:
(
rem // This loop executes when there are other characters:
for /F "delims=0123456789. eol=." %%J in ("%%~I") do (
setlocal EnableDelayedExpansion
rem /* Rebuilt string with currently found items; if there
rem are other items following numeric ones, keep them: */
for /F "delims=" %%K in ("!COLL!!PREV:~1!-!ITEM!") do (
endlocal & set "COLL=%%K" & set "PREV=-"
)
)
) || (
rem // This section runs when there are numerals and `.`:
setlocal EnableDelayedExpansion
for /F "delims=" %%K in ("!PREV!-!ITEM!") do (
rem /* Store numeric items in a separate buffer; if there
rem are no other items following, this is dismissed: */
endlocal & set "PREV=%%K"
)
)
setlocal EnableDelayedExpansion
)
rem // Return rebuilt line string:
echo(!COLL:~2!
endlocal
)
endlocal
exit /B
The example input file mods.txt:
fruit-apple-1.5.6
vegtable-sla-mc5-6.5
extra-huh-9.5
extra-3.2
test-9.15.5
keep-forge
name-subname-10.5-55.5
globalxp-1.16.5
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more-3.4
mix-1.2-string-10.5-55.5-more-3.4-5.6-7
8.9
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult
leads to the following output:
fruit-apple
vegtable-sla-mc5
extra-huh
extra
test
keep-forge
name-subname
globalxp
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
mix-1.2-string-10.5-55.5-more
1.2.3-lead
1.2.3-4.5.6-7.8.9-lead-mult
One way is to replace the - with \ and then use variable substitution to strip the %%~ni part from the string. We just need to remove the first and last occurrence then from the string. findstr will determine that the end of string needs to be -num.*
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%A in ('dir /b /s *.jar ^| findstr /RC:"-[0-9].*$"') do (
set "line=%%~nxA"
for %%i in ("!line:-=\!") do set "final=%%~pi"
set "final=!final:~0,-1!-"
echo !final! | findstr /RC:"\\[0-9].*$" >nul 2>&1
if !errorlevel! equ 0 (
for %%s in ("!final!") do set "final=%%~ps"
set final=!final!
)
set "final=!final:~0,-1!"
set "final=!final:~1!"
echo !final:\=-!
)
the edited code should take care of the additional - as per your comment example name-subname-10.5-55.5
Here's an untested idea, which may not be particularly quick, especially if you have a large number of strings in your mods file:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F %%G In ('Echo Prompt $E ^| %SystemRoot%\System32\cmd.exe /D'
) Do Set "$E=%%G"
For /F Delims^=^ EOL^=^ UseBackQ %%G In ("%~dp0mods") Do (Set "Line=%%G"
SetLocal EnableDelayedExpansion
For /L %%H In (0 1 9) Do For /F Delims^=^ EOL^= %%I In (
'(Echo(^) ^| Set /P "=!Line:-%%H="^&:"!" 0^<NUL') Do Set "Line=%%I"
Echo(%$E%[46m!Line!%$E%[0m
EndLocal
)
Pause

Removing carriage returns from a text file using a batch file based on a value

I have a text file that I would like to edit and therefore would like to remove the last line. I have the following code for this:
for /f "delims=" %%a in (input.txt) do (
echo/|set /p ="%%a%"
)>>output.txt
input:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Now I would like to edit the data in groups for example by the first value, so that I have the following output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
If I replace the FOR /F "delims=" %%a in (input.txt) do … loop with an equivalent FOR %%a in … loop:
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_gruppeName="
(
for %%a in (
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
) do (
for /f "tokens=1 delims=;" %%A in ("%%~a") do (
if /I NOT "%%~A"=="!_gruppeName!" (
if defined _gruppeName echo(
set "_gruppeName=%%~A"
)
)
echo/|set /p ="%%~a"
)
echo(
)>>output.txt
REM debugging output follows
type output.txt
Output:
1st run: 2>NUL del output.txt & D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Next run: D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Your question is not clear
based on ... the first value (GRUPPEA)
is it SORTed? or just write duplicates on the same line?
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
::VARIABLES
set "in=input.txt" IN file
set "out=output.txt" OUT file
set/a"#lines=0"
set "gruppe="
set "prevContent="
::Count lines
FOR /F %%L in ('
"findstr /N "^^" "%in%" %= do not skip empty lines =%"
') do set/a"#lines+=1" %= Get the # of lines =%
::Read IN via SET /P #LINES number of times
::Bangs (!) will be lost
<"%in%" >"%out%" (FOR /L %%# in (1 1 %#lines%) do (
set "data=" ::clear DATA
set/p"data=" ::read from IN
FOR /F tokens^=1^ delims^=^;^ eol^= %%T in ("!data!") do set "gruppe=%%T"
if NOT "!prevContent!" == "!gruppe!" (
set "prevContent=!gruppe!"
echo(
)
<nul set/p"=!data!" ::does not work with leading space, tabs, or equal signs
)) %= read file by lines via SET /P =%
exit /b
The script counts the number of lines using FINDSTR /N and a FOR /F loop to count the # of lines, which is required to execute SET /P that many times.
Tips:
Use ECHO( instead of the problematic ECHO.
Using a pipe | is very slow, as described by #jeb here. Use <nul instead

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

Replace a matching string in each line with another string

I am trying to replace the number at the end of every line of a given file with an incremented number(+1).
My Input file looks like
C:\documents\a.txt,1988,15.01.00.0059
C:\documents\we.txt,1988,15.01.00.0059
C:\documents\gh.txt,1987,1988,15.01.00.0059
my out put file should be
C:\documents\a.txt,1988,15.01.00.0060
C:\documents\we.txt,1988,15.01.00.0060
C:\documents\gh.txt,1988,15.01.00.0060
My attempt is like
#echo off
setlocal enableextensions disabledelayedexpansion
set "search="
for /F "tokens=1-3 delims=," %%i in (%1) do (
#echo %%i %%j %%k
set "search=%%k"
)
set "replace="
REM #echo %replace%
set "textFile=%1"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line:%search%=%replace%!
endlocal
)
i could able to get 15.01.00.0059 from the input file but dont know how to change to 15.01.00.0060 .
replacing to the same input file is also working . I just need to know how to increment the value and assign to "replace".
Thank you
Bhargav.k
This should be flexible enough:
#echo off
setlocal EnableDelayedExpansion EnableExtensions
1>out.txt (
FOR /F tokens^=*^ delims^=.^ eol^= %%L in (input.txt) do (
set "line=%%L"
set "x=!line:.= !"
FOR %%T in (!x!) do set "lastToken=%%T"
cmd /c exit /b !lastToken!
set/a"lstrip=!ERRORLEVEL!+1"
>getlen.tmp echo(!lasttoken!
FOR %%L in (getlen.tmp) do set/a"len=%%~zL-2" %= get length of last token, minus CRLF =%
FOR %%N in (1 1 !len!) do set "lstrip=0!lstrip!" PREFIX lstrip with zeroes
FOR %%N in (!len!) do (
set "lstrip=!lstrip:~-%%N!"
echo !line:~0,-%%N!!lstrip!
)
)
)
del getlen.tmp
ENDLOCAL
This is not a straightforward approach.
The script counts the # of zeroes prefixed before the last token by the ERRORLEVEL returned by EXIT /B, compared with the original token.

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.

Resources