Code 3 is not bad but it won't work with spaces in a path because you use the standard delims
as you're not providing one. Also there a several other errors about working with spaces in a path.
The following code works and combine all txt files
in all subdirectories. It will create a new file list.txt
in the folder where this batch file is located. If there is already an existing list.txt
it will be overwritten. Note that it's a batch file:
@echo off
set "folder=C:\Users\user\Desktop\DummyFolder\"
rem create new empty file: list.txt in directory of batch file: %~dp0
break>"%~dp0list.txt"
rem loop through all output lines of the dir command, unset delimns
rem so that space will not separate
for /F "delims=" %%a in ('dir /b /s "%folder%"') do (
rem just look for txt files
if "%%~xa" == ".txt" (
rem don't use the list.txt
if not "%%a" == "%~dp0list.txt" (
rem append the output of the whole block into the file
(echo/------------------------------
type "%%a"
echo/)>>"%~dp0list.txt"
)
)
)
If you don't understand something it's quite easy to find something good on the internet because there are several great batch scripting sites. Further you can always use echo This is a message visible on the command prompt
to display something that might be useful e.g. variables etc. With that you can "debug" and look what happens.
Some explanations beyond the comments (rem This is a comment
) in the code:
1.
break
command:
To clear a file I use the break command which will produce no output at all. That empty output I redirect to a file, read it here: https://stackoverflow.com/a/19633987/8051589.
2.
General variables:
You set variables via set varname=Content
I prefer the way as I do it with quotes: set "varname=Content"
as it works with redirection characters also. Use the variable with one starting %
and one trailing %
e.g. echo %varname%
. You can read a lot of it on https://ss64.com/nt/set.html. I think ss64 is probably the best site for batch scripting out there.
3.
Redirection >
and >>
:
You can redirect the output of a command with >
or >>
where >
creates a new file and overwrites existing files and >>
appends to a file or create one if not existing. There are a lot more thing possible: https://ss64.com/nt/syntax-redirection.html.
4.
for /f
loop:
In a batch file you loop through the lines of a command output by using a for /f
loop. The variable that is used will be written with 2 %
in front of it, here %%a
. I also set the delimiter delimns
to nothing so that the command output will not be separated into several tokens.
You can read a lot of details about a for /f
loop at: https://ss64.com/nt/for_cmd.html.
5.
Special variable syntax %%~xa
and %~dp0
:
The variable %%a
which hold one line of the dir
command can be expand to the file extension only via: %%~xa
as explained here: https://stackoverflow.com/a/5034119/8051589. The %~dp0
variable contains the path where the batch file is located see here: https://stackoverflow.com/a/10290765/8051589.
6.
Block redirection ( ... )>>
:
To redirect multiple commands at once you can open a block (
, execute commands, close the block )
and use a redirection. You could also execute every command and redirect that only that would have the same effect.