Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello, I am trying to write a batch file that will take all files that have a character string embeded in the name and combine those into a file. for example it these are the files in the directory:
aflksjfkj_JOE_0987098708.txt
987s089_JOE_hiu9087uig.txt
90a8hsdfan_JOE_asjkdfhlaj.txt
98089a7sfh_MIKE_adkajhdfkj.txt
askldakfjkl_MIKE_aldkjfklajdf.txt
aljkfdakl8907_MIKE_jksdf.txtand I want to end up with two files
MIKE_files.txt
JOE_files.txtIt has to be generic so whether it is _JOE_ or _MIKE_ or any name between the "_" it will combine all the files with that character string into a single file and does it for all the different names in the folder.
Thanks for any advice

Change variable "directory" from "test" to the directory of the
files. You don't need to put a full path including drive. If you were to run it as is, if the batch file was in a folder that contained a folder called "test" it would work.So, if your batch file is in a folder called "stuff" and inside that folder was a folder called "more stuff" that had all of your text files in it, you would change the variable "directory" from "test" to "more stuff".
BTW, the batch file cannot be in the same directory as the "directory" variable.
@echo offset directory=test
set charnum=0
setlocal enabledelayedexpansion
for %%a in ("!directory!\*.txt") do (
set fname=%%a
call :loop
)exit
:loop
if "!fname!" == "!directory!\!name!_files.txt" goto end
set /a charnum=!charnum!+1
call set char=%%fname:~!charnum!,1%%if "!char!" == "_" (
if not defined fchar set fchar=!charnum! && goto loop
set lchar=!charnum!
set /a fchar=!fchar!+1
set /a lchar=!lchar!-!fchar!
call set name=%%fname:~!fchar!,!lchar!%%
type "!fname!">>"!directory!\!name!_files.txt"
echo+>>"!directory!\!name!_files.txt"
set charnum=-1
set fchar=
goto end
)goto loop
:end
There are no stupid questions, just stupid people.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |