0

I have a .bat script that concats some text files into one big file. I would like to have the resulting file go to the current directory instead of the hardcoded path.

Here is my script:

@echo off
:loop
if ($1) == () goto end
more +9 %1% >> %cd%\bigfile.txt
shift
goto loop
:end

I have this saved in the "Send To" folder, so I can select a bunch of files in DirectoryA, and right-click and "send to" my batch script. This works, but I want bigfile.txt to be in the same directory as all the little files. Ideas?

1
  • 1
    Use %~dp0 instead of %cd% and see if it works. Btw use it like this: %~dp0bigfile.txt
    – Haplo
    Oct 27, 2014 at 21:41

2 Answers 2

1
@echo off
    setlocal enableextensions disabledelayedexpansion

:loop
    if "%~1"=="" goto :eof
    more +9 "%~1" >> "%~dp1bigfile.txt"

    shift
    goto loop

Append the output to a file (bigfile.txt) in the same drive and path as the source file

3
  • Hm, this only works for less than about 20 files at a time. If I try to do more, it says Windows cannot access the specified device, file, or path... Is there some limit on the number of input arguments? Oct 28, 2014 at 15:06
  • @hoosierEE, yes, there is a limit. The command line in windows is limited to 2048 characters in XP and 8192 characters from Vista and later. And the full list of files you select (with its paths) is passed as argument to the batch file in one command line.
    – MC ND
    Oct 28, 2014 at 15:08
  • Ah well that is a problem. I ended up using a for loop in the end to avoid passing in all the filenames as arguments, but the %~dp1 still came in handy. Oct 28, 2014 at 18:01
0

that can be achieved by replacing

%cd%\bigfile.txt

with

%cd:~0,2%%~p1

it obtains the string to the path up to the parent directory (with drive specified)

You must log in to answer this question.

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