0

I've added a Batch file to the right click "send to" menu which opens a command prompt in the current directory. The command to concatenate all of the .txt files in that folder and any sub folders does not execute properly when used from the batch file (produces a 0 byte file called output.txt), but does work properly if I type it into the cmd prompt.

%~d1
cd "%~p1"
start cmd.exe /K 
"for %f in (*.txt) do type "%f" >> output.txt"

Windows 10. Any help is appreciated. Thanks.

edit look here, double the tuples for some reason: https://stackoverflow.com/questions/11711569/windows-batch-file-concatenate-all-files-in-subdirectories

1
  • Sorry the code doesn't appear correctly formatted. It's actually 4 lines.
    – Sean
    Mar 3, 2017 at 0:00

2 Answers 2

1

Would the following .reg and batch file help as a starter:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Run Batch script]
@="&Run text file concatenate"

[HKEY_CLASSES_ROOT\Directory\shell\Run Batch script\command]
@="\"E:\\menu.bat\" \"%1\""

Batch file "E:\menu.bat":

@ECHO OFF
cd  %*
forfiles /s /m *.txt /c "cmd /c type @path >> %*\output.txt
2
  • Thanks, could you explain why you need to edit the registry? I'm super new to this. What language is this?
    – Sean
    Mar 3, 2017 at 0:47
  • The registry entries add the right click context menu to call your batch file, using Windows registry language. You would put the data into a text file with extension.reg, and double click it to add to your registry. Or, you can open RegEdit and browse to the Classes and add the keys yourself, but that's a fair bit harder. Mar 3, 2017 at 1:26
1
  • Batch files require doubled percent signs using the for variables.
  • As I see it there is no need for the cmd /k,
  • your command didn't recurse into subdirs
  • you've to avoid processing the newly generated output.txt and (re-)initialize it on successive runs.

Change the batch to this:

@Echo off
Pushd "%~dp1"
Type Nul >output.txt
for /F "delims=" %%f in (
  ' Dir /B/S/A-D *.txt ^|find /v /i "output.txt" '
) do type "%%f" >> output.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .