The file contents copying task could be done with:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
echo(
if "%~1" == "" echo INFO: %~n0 must be called with directory name as first parameter.& exit /B
if "%~2" == "" echo INFO: %~n0 must be called with file extension as second parameter.& exit /B
pushd "%~dp0%~1" 2>nul || (echo ERROR: Directory not found: "%~dp0%~1"& exit /B)
set "FileExtension=%~2"
if not "%FileExtension:~0,1%" == "." set "FileExtension=.%FileExtension%"
del /A /F "concat.txt" 2>nul
if exist "*%FileExtension%" (
copy /B "*%FileExtension%" "concat.txt" >nul
echo INFO: Copied files to "concat.txt" in: "%CD%"
) else echo INFO: No file matching "*%FileExtension%" found in: "%CD%"
popd
endlocal
There is defined completely with the first two command lines the required execution environment and next an empty line is output.
Then is verified that the batch file is called with at least two non-empty argument strings.
The next line pushes path of current directory on stack and changes the current directory to the directory specified by the first parameter which is expected (but not verified) to be a subdirectory of the batch file directory. An error message is output if that fails with showing the used directory path.
Your code missed to change the current directory to scripts2
respectively the destination file is specified without directory name scripts2
and therefore created in the current directory whatever is the current directory on starting cmd.exe
to process the batch file.
Next the second parameter is assigned to an environment variable named FileExtension
and if the first character is not a dot, the variable is redefined with a dot at the beginning.
The destination file is deleted with suppressing all error messages for the use case that the destination file concat.txt
does not exist at all. That is required in case of entered file extension is .txt
and the directory contains already concat.txt
from a previous execution of the batch file.
The last IF condition checks if any file (or directory) exists matching the wildcard pattern and copies them together to the destination file concat.txt
and outputs a small information message or outputs a different information message on no directory entry matches the wildcard pattern.
The path of the initial current directory is popped from stack and set again as current directory on still existing as expected by the code before finally restoring the initial execution environment.
Another solution would be:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
echo(
if "%~1" == "" echo INFO: %~n0 must be called with directory name as first parameter.& exit /B
if "%~2" == "" echo INFO: %~n0 must be called with file extension as second parameter.& exit /B
pushd "%~dp0%~1" 2>nul || (echo ERROR: Directory not found: "%~dp0%~1"& exit /B)
set "FileCount="
set "FileExtension=%~2"
if not "%FileExtension:~0,1%" == "." set "FileExtension=.%FileExtension%"
for /F "eol=| delims=" %%I in ('dir "*%FileExtension%" /A-D /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /X /C:"concat.txt"') do (
if not defined FileCount ( copy /B /Y "%%I" "concat.txt" >nul ) else copy /B /Y "concat.txt"+"%%I" "concat.txt" >nul
set /A FileCount+=1
)
if not defined FileCount (
echo INFO: No file matching "*%FileExtension%" found in: "%CD%"
goto Finish
)
if %FileCount% == 1 (
echo INFO: Copied one file to "concat.txt" in: "%CD%"
) else (
echo INFO: Copied %FileCount% files to "concat.txt" in: "%CD%"
)
:Finish
popd
endlocal
This second solution is slower than the first solution, but gives more control about the order of files concatenated together and the final information is a bit better.
Please note that both solutions are neither 100% fail safe nor 100% secure.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
call /?
... explains how batch file arguments can be referenced.
copy /?
del /?
dir /?
echo /?
endlocal /?
exit /?
findstr /?
for /?
goto /?
if /?
popd /?
pushd /?
set /?
setlocal /?
See also: