- “Main” folder
- Folder-“Other”
- Folder-“A”
Folder-“B”
(…)
Folder-“N”
- Folder-“A”
- Folder-“Other”
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 ...
There’s a couple of problems here:
FORloop and that’s not working as it is – and not necessary too, because you can just useTYPEto write/append each .txt file’s contents to another file.SetLocal EnableDelayedExpansionfor 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 becauseCMD.EXEthinks it’s part of a variable name, corrupting the filename and thus causing the contents of the file not to be included inBig.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: