MERGE.BAT
@echo off
>"%~1_all.txt" (
for %%F in ("%~1*.txt") do if /i "%%F" neq "%~1_all.txt" type "%%F"
)
Usage:
merge apple
merge "water melon"
Or, putting everything in one batch script:
@echo off
call :merge apple
call :merge "water melon"
exit /b
:merge
>"%~1_all.txt" (
for %%F in ("%~1*.txt") do if /i "%%F" neq "%~1_all.txt" type "%%F"
)
exit /b
Programmatically figuring out all the fruit in the folder is not trivial, since there is no easy way in batch to trim all of the trailing digits from the base name of a file. Here is once solution (untested):
@echo off
setlocal disableDelayedExpansion
del *_all.txt 2>nul
for /f "delims=" %%F in (
'dir /b /a-d *.txt^|findstr "[^0-9][0-9]*\.txt$"'
) do call :proc "%%F"
exit /b
:proc
set "file=%~1"
set "fruit=%~n1"
setlocal enableDelayedExpansion
:trimNum
if "!fruit:~-1!" geq "0" if !fruit:~-1!" leq "9" (
set "fruit=!fruit:~0,-1!"
goto :trimNum
)
type "!file!" >>"!fruit!_all.txt"
exit /b