11

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.

tonip
  • 178
  • 1
  • 10

5 Answers5

5

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.

Aacini
  • 65,180
  • 12
  • 72
  • 108
2
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.

Squashman
  • 13,649
  • 5
  • 27
  • 36
TheRealKernel
  • 351
  • 3
  • 11
  • This will fail on file names with spaces. You also need to add the /S option to recursively search. – Squashman Aug 09 '16 at 16:26
  • Is there a fix to the command to work with spaces in filenames that you know off the top of your head? – TheRealKernel Aug 09 '16 at 16:28
  • Thanks for the edit @Squashman, just tested it with spaces in the file name and it worked for me. – TheRealKernel Aug 09 '16 at 16:32
  • 1
    @dxxAnthonyxxb Can you tell me what `-d` is doing? Also, if anyone can explain why my own code **doesn't** work, that would be appreciated. – tonip Aug 09 '16 at 16:38
  • @dxxAnthonyxxb , that works like a charm. I will definitely give this answer credit unless someone can tweak my attempt and get it to work. Would definitely note any explanation for why mine failed. Thanks! – tonip Aug 09 '16 at 16:59
  • 1
    Note that you can just use `for /r %a in (*.csv)` instead. Your way is overly convoluted and likely to break with spaces (it already has in its previous iteration) or non-ASCII characters. `for` is perfectly capable of iterating over a list of files without the need to call `dir`. – Joey Aug 09 '16 at 17:22
  • 1
    This method run notepad++ one time _for each file_ (several executions). The original description execute notepad++ just one time _with several files_. – Aacini Aug 09 '16 at 18:13
  • @Joey Thanks for the input. That seems to add something to the discussion, but when I ran it, the results were not good. It attempted to created a bunch of new files (not something I wanted). I used the full command: `for /r %a in (*.csv) do call notepad++ "%a"` While @TheRealKernel 's solution doesn't do it the way I wanted, it definitely gets the result I want, so I think I'll stick with it. I guess I didn't say it explicitly, but I wanted it to open them all as a single command. – tonip Aug 09 '16 at 19:32
1

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.

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

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
Quin
  • 87
  • 10
-1

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

Chris Smith
  • 688
  • 8
  • 20