Redirecting the file names with path without double quotes from command DIR to Notepad++ does not work because Notepad++ is a GUI application which does not read in file names from standard input stream STDIN line by line.
Notepad++ expects a space separated list of (double quoted) file name(s) as command line parameter(s) whereby the double quotes around a file name with no path, a relative path or an absolute path are not always needed depending on characters in file name and file path.
My first suggestion is:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Files="
for /F "delims=" %%I in ('dir zed.txt /A-D /B /S 2^>nul') do set "Files=!Files! "%%I""
if not "!Files!" == "" notepad++.exe%Files%
endlocal
This batch code concatenates the found files to a long parameter list starting always with a space and starts Notepad++ with that list.
The problem with this simple code is that the length of the parameter list could exceed the maximum length for an environment variable string if there are really many files with file name zed.txt
.
A simple solution would be to limit the number of files per Notepad++ run for example to 25.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Files="
set "FileCount=0"
for /F "delims=" %%I in ('dir zed.txt /A-D /B /S 2^>nul') do (
set "Files=!Files! "%%I""
set /A FileCount+=1
if !FileCount! == 25 (
notepad++.exe!Files!
set "Files="
set "FileCount=0"
)
)
if not %FileCount% == 0 notepad++.exe%Files%
endlocal
The best solution would be to check on each loop run the current length of the environment variable before appending next file name. But this would make the batch file slower and usually the file names with path are not so long that 25 files per Notepad++ execution is really a problem.
Well, theoretical a single file name with path could be already too long for being assigned to an environment variable.
Note: If the batch file execution is halted after starting Notepad++ and this behavior is not wanted, insert left to each Notepad++.exe
the string start ""
to start Notepad++ in a separate process with an empty title string. Don't omit ""
after command START before Notepad++.exe
as otherwise first double quoted string (= first file name with path) would be interpreted as title string.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
if /?
set /?
setlocal /?
start /?
See also the Microsoft documentation page about Using command redirection operators for an explanation of 2>nul
. The redirection operator >
is escaped here with ^
to apply the redirection of STDERR to device NUL on execution of command DIR. This redirection is used to suppress the error message output by command DIR if it can't find any zed.txt
in current directory or any subdirectory.