0

This is a very basic batch script. It is my first day of dealing with this topic.

I want to create a script which takes a directory name and an extension as parameters, and if that directory exists, the contents of each file with that extension will be written into another file.

Example:

scripts (DIR)
   |
   ----->myFirstBatch.bat
   ----->scripts2 (DIR)
         |
         ----->test.xyz, test2.xyz, concat.txt

Let's say my batch script is called myFirstBatch.bat.

I will run it like this:

myFirstBatch scripts2 .xyz

I expect concat.txt to have the contents of test.xyz and test2.xyz.

Here's my script:

set var=%2
shift
if exist %0 (
type .\%0\*%var%>>concat.txt
)

But there is nothing inside concat.txt.

What am I doing wrong?

Mofi
  • 46,139
  • 17
  • 80
  • 143

3 Answers3

1

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:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

Because you never enter the scripts2 directory, concat.txt is created in the same location that you're in when you call the script (most likely scripts).

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
0
@echo off
echo extension
set /p ext=
echo directory
set /p paf=
if not exist "*%paf:"=%" goto no
for /r "%paf:"=%" %%i in (*%ext:"=%) do (
    type %%i >>output.txt
)
goto :eof
:no
echo files do not exist
pause

this is how i've done it, if you want to run this directly from the prompt you can just replace the variables and remove the input statement

Arsenic
  • 1
  • 3