What I want to do: Use a dir command to find a bunch of text files, then open them all in Notepad++. Preferably in a single command without creating extra files. Normally, you can just say
notepad++ zed.txt ./ted/zed.txt ./red/zed.txt
But I need to open many files in a complex directory structure.
I feel like I should be able to do something like this:
dir /s/a/b "zed.txt" | notepad++
This does give me the output I want (recursively finds files and spits out just the filenames), but each on its own line and Notepad++ doesn't open them, it just opens and asks to create the file "notepad++" - which is empty.
Even using an extra file (undesirable, but not preferable) does not work:
c:\Users\tonip\Desktop\some_dir>dir /s/a/b "cmakelists.txt" > np.txt && notepad < np.txt
or
dir /s/a/b "cmakelists.txt" > np.txt
notepad++ < np.txt
N.B. Notepad++ directory is on my path.
Obviously, my re-direction skills are not good. Help is greatly appreciated.
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.
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.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With