Reputation: 675
Hi, I have a Main folder, a secondary folder and its several child named "folder-A, folder-B ... folder-N". Each child has txt files with lots of information in them.
I would like to read each folder (only folders A to N), collect the information within each txt and merge that information to make a Big.txt. The user would also know how many txt files were analysed and merged.
No good results so far. Any help?
@echo off
set DRV1=c:\Main\Other
set DRV2=c:\Big
cd %DRV1%
setlocal enabledelayedexpansion
set /a count=0
for %%f in (*.txt) do (
for /f "delims= " %%a in (%%f) do (
set /a count+=1
echo %%a >> %DRV2%\Big.txt
)
)
endlocal
echo There were processed %count% txt files ...
Upvotes: 0
Views: 275
Reputation: 10500
There's a couple of problems here:
FOR
loop and that's not working as it is - and not necessary too, because you can just use TYPE
to write/append each .txt file's contents to another file. SetLocal EnableDelayedExpansion
for this task - it can even cause problems if any of the .txt files have an exclamation mark !
in the filename - exclamation marks will be eaten by the delayed expansion because CMD.EXE
thinks it's part of a variable name, corrupting the filename and thus causing the contents of the file not to be included in Big.txt
. By the way, I wouldn't have known that before it hit me while fiddling around for the solution - because I actually had .txt files with an exclamation mark in the filename for testing :)Long story short, here's what I ended up with:
@ECHO OFF
SET DRV1=c:\Main\Other
SET DRV2=c:\Big
SET /a count=0
:: iterate over all directories in %DRV1%
FOR /f "tokens=*" %%F IN ( 'dir /b /a:d %DRV1%' ) DO (
:: for each directory, iterate over all .txt files
FOR %%G IN ( %DRV1%\%%F\*.txt ) DO (
SET /a count+=1
TYPE %%G >> %DRV2%\Big.txt
)
)
ECHO There were processed %count% txt files ...
Upvotes: 1