tonip
tonip

Reputation: 188

Open multiple files from cmd dir output with notepad++

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.

Upvotes: 12

Views: 9493

Answers (5)

Mofi
Mofi

Reputation: 49085

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.

Upvotes: 1

Quin
Quin

Reputation: 97

You can do this on ubuntu (wsl) also

for file in $(find -type f)
do
    notepad++.exe $file
done

shorthand would be:

for file in $(find -name zed.txt); do notepad++.exe $file;done

variations on this would be to exclude certain subfolders. this script will exclude any temporary folders like .git

for dir in $(find -maxdepth 1 -type d;egrep -v '^\./\.')
do
    for file in $(find $dir -type f|egrep '(zed|foo)')
    do
        notepad++.exe $file
    done
done

or scan for text before opening the file:

for file in $(find -type f|egrep '(zed|foo)')
do
    for l in $(cat $file|egrep -i '(foo)')
    do
        notepad++.exe $file
        break
    done
done

Upvotes: 0

Chris Smith
Chris Smith

Reputation: 688

Here is an example opening files in notepad++ off a file share matching a pattern. Place this code into a "finder.bat" file and double click on it.

for /f "delims=" %%f IN ('dir /b /s "\\server\c$\Program Files (x86)\MyUtility\*.exe.config"') do call "C:\Program Files (x86)\Notepad++\notepad++.exe" %%~sdpnxf

Idea formulated from https://stackoverflow.com/a/5564349/194872

Upvotes: -1

Aacini
Aacini

Reputation: 67206

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.

Upvotes: 5

TheRealKernel
TheRealKernel

Reputation: 351

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.

Upvotes: 2

Related Questions