Computing.Net > Forums > Programming > Combining files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Combining files

Reply to Message Icon

Name: drmkahn
Date: August 13, 2009 at 14:40:22 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.txt

and I want to end up with two files

MIKE_files.txt
JOE_files.txt

It 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



Sponsored Link
Ads by Google

Response Number 1
Name: xterm11
Date: August 13, 2009 at 22:51:45 Pacific
Reply:

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 off

set 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.


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Combining files

combine two files into one file www.computing.net/answers/programming/combine-two-files-into-one-file/17093.html

combine files into one file www.computing.net/answers/programming/combine-files-into-one-file/17100.html

remove carriage return at file end www.computing.net/answers/programming/remove-carriage-return-at-file-end/17101.html