Open multiple files from cmd output file with notepad ++

What I want to do: Use the dir command to find a bunch of text files, then open them all in Notepad ++. Preferably in one command without creating additional files. You can usually just say

notepad++ zed.txt ./ted/zed.txt ./red/zed.txt 

But I need to open many files in a complex directory structure.

It seems to me that I can do something like this:

 dir /s/a/b "zed.txt" | notepad++ 

This gives me the result that I want (recursively finds files and spits out only the file names), but each in its own line and Notepad ++ does not open them, it just opens and asks to create a file "notepad ++" - which is empty.

Even using an additional file (unwanted, but not preferred) 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 

NB The Notepad ++ directory is on my way.

Obviously, my reorientation skills are not very good. Help is appreciated.

+6
source share
4 answers
 for /f "delims=" %a IN ('dir /ad /b /s *.csv') do call notepad++ "%a" 

It seems to work for me. Of course, you will need to change the "dir / b" criteria to all the files you are trying to map.

This command skips all files output using dir (only csv files in my case) and opens them using notepad ++. Be sure to include / b after the dir command only to display file names without any other information.

+2
source

This works for me on the command line:

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

Just make sure the list variable does not exist before executing this line.

+4
source

Redirecting file names using a path without double quotes from the DIR command in Notepad ++ does not work, because Notepad ++ is a GUI application that cannot be read in file names from the standard STDIN input stream line by line.

Notepad ++ expects a space-separated list (with double quotes) of the file name (s) as command line parameters, in which double quotes around the file name without a path, relative path or absolute path are not always necessary depending on the characters in the file name and file paths.

My first suggestion:

 @echo off setlocal EnableExtensions EnableDelayedExpansion set "Files=" for /F "delims=" %%I in ('dir zed.txt /AD /B /S 2^>nul') do set "Files=!Files! "%%I"" if not "!Files!" == "" notepad++.exe%Files% endlocal 

This command code combines the found files into a long list of parameters, always starting with a space, and launches Notepad ++ with this list.

The problem with this simple code is that the length of the parameter list may exceed the maximum length for the string of the environment variable, if in fact there are many files with the file name zed.txt .

A simple solution would be to limit the number of files in Notepad ++, for example, to 25.

 @echo off setlocal EnableExtensions EnableDelayedExpansion set "Files=" set "FileCount=0" for /F "delims=" %%I in ('dir zed.txt /AD /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 

A better solution would be to check that each loop executes the current length of the environment variable before adding the next file name. But this will slow down the batch file, and usually the file names with paths are not so large that 25 files to execute Notepad ++ are really a problem.

Well, the theoretical single file name with a path may already be too long to assign an environment variable.

Note. . If the execution of the batch file is stopped after starting Notepad ++ and this behavior is not required, insert each Notepad++.exe line start "" to the left to start Notepad ++ in a separate process using an empty header line. Do not miss the "" after the START command before Notepad++.exe , as otherwise the first double quote (= the name of the first file with the path) will be interpreted as a header line.

To understand the commands used and how they work, open a command prompt window, run the following commands there, and carefully read all the help pages displayed for each command.

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • set /?
  • setlocal /?
  • start /?

See also a Microsoft article on Using Command Redirection Operators to Explain 2>nul . The redirection operator > escaped here with ^ to apply the STDERR redirection to the NUL device when executing the DIR command. This redirection is used to suppress the output of the error message with the DIR command if it cannot find zed.txt in the current directory or in any subdirectory.

+1
source

This may be a solution from my old script in this thread: Open the file with cmd and display the selected one in a special editor

I just modified it to open Notepad ++ instead of the Sublime Text 3 editor

 @ECHO OFF Title Edit the selected file with Notepad++ :MenuLoop Cls & Color 0A SETLOCAL SET "ROOT=%userprofile%\Desktop" SET "EXT=*.txt" SET "Count=0" SETLOCAL enabledelayedexpansion REM Iterates throw the files on this current folder. REM And Populate the array with existent files in folder FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\%EXT%"') DO ( SET /a "Count+=1" set "list[!Count!]=%%~nf" set "listpath[!Count!]=%%~dpFf" ) echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs" for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a") If %cols% LSS 50 set /a cols=%cols% + 15 set Files=%Count% set /a lines=%Count% + 10 Mode con cols=%cols% lines=%lines% ECHO ******************************************************* ECHO Folder : "%ROOT%" ECHO ******************************************************* rem Display array elements for /L %%i in (1,1,%Files%) do echo [%%i] : !list[%%i]! SET /a "COUNT_TOT=%Count%" ECHO. ECHO Total of [%EXT%] files(s) : %Count% file(s) echo( echo Type the number of what file did you want to edit ? set /p "Input=" set "MyEditor=%programfiles%\notepad++\notepad++.exe" For /L %%i in (1,1,%Count%) Do ( If "%INPUT%" EQU "%%i" ( Rem Testing if Notepad++.exe exist to open with it the text file If Exist "%MyEditor%" ( Start "Sublime" "%MyEditor%" "!listpath[%%i]!" Rem Otherwise we open the text file with defalut application like notepad ) else ( Start "" Notepad.exe "!listpath[%%i]!" ) ) ) EndLocal Goto:MenuLoop 
0
source

All Articles