Open multiple files from cmd dir output with notepad++ Open multiple files from cmd dir output with notepad++ windows windows

Open multiple files from cmd dir output with notepad++


This works for me in the command-line:

(for /F "delims=" %a in ('dir /s/a/b "zed.txt") do call set "list=%list% "%a"") & call notepad++ %list%

Just be sure that list variable does not exist before execute this line.


for /f "delims=" %a IN ('dir /a-d /b /s *.csv') do call notepad++ "%a"

This seems to work for me. Of course you will need to modify your 'dir /b' criteria to whatever files you are trying to match.

This command loops through all the files output by dir (only csv files in my case) and opens them with notepad++. Be sure to include the /b after your dir command to only output filenames without all of the other information.


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 offsetlocal EnableExtensions EnableDelayedExpansionset "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 offsetlocal EnableExtensions EnableDelayedExpansionset "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 article 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.