Automatically selecting files with searched word in it
-
Hi guys, I’m fairly new with this program and I’ve got a question.
I searched for a word in a directory, and it found occurrences in 220 files. Is there a way to automatically select and copy/paste those files in another directory?
Thanks a lot! -
Automatically? No.
But with a “few” steps, not so hard:
- right-click in the Find result panel whitespace (anywhere)
- choose Select All from the popup menu
- press ctrl+c to copy
- create a new file called
test.bat
in your desired destination directory - open
test.bat
in Notepad++ - press ctrl+v to paste into N++'s buffer for
test.bat
- choose Edit (menu) > Line Operations > …and pick the topmost sort option
Your file buffer should now have a group of lines at the top that start out with
Line
and a group of lines at the bottom that start withSearch
. You want the grouping of lines in the middle, the ones that start out with actual pathnames and end with(# hits)
- manually remove the
Line
lines at the top of the file - manually remove the
Search
lines at the bottom of the file - run this regular-expression replace on the whole file: Find:
(?-i)\s\(\d+\shits?\)(?=\R)
Repl:" .
- run this regular-expression replace on the whole file: Find:
^\s+
Repl:copy "
- press ctrl+s to save
test.bat
- get a cmd prompt in the directory containing
test.bat
- type
test
at the prompt and press Enter - WATCH THE CMD OUTPUT AS ALL OF YOUR FILES ARE COPIED INTO THAT SINGLE DIRECTORY!
- delete the
test.bat
file
Note that this won’t work so well if any files have the same name (just the name.ext part) which can happen. Be careful.
Wow. I usually don’t give explanations like that. I think I am channeling @guy038 while he is off fishing. :)
-
I didn’t quite manage to get the last part right, anyway just simply having the paths displayed like that helped me save quite a bit of time.
Thanks a lot! -
Good that you got something you could use from that!
Actually, this is probably a good opportunity to show off macros in Notepad++.
First, do some (one-time) setup:
- Press the toolbar button for starting macro recording
- In an editing tab view, press an arrow key to move the caret (this is just a dummy action)
- Press the toolbar button for stopping macro recording
- Press the toolbar button for saving a recorded macro; give it the name
Bat file from Find results
and save it - Exit Notepad++
- Restart Notepad++
- Locate your
shortcuts.xml
file and open it in Notepad++ - Do a find on
Bat file from Find results
and you should be left looking at something like this:
<Macro name="Bat file from Find results" Ctrl="no" Alt="no" Shift="no" Key="0"> <Action type="0" message="2306" wParam="0" lParam="0" sParam="" /> </Macro>
- Replace the
Action
line with the following block, making the indent level of the block match that of the oldAction
line:
<Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^Search.+\R" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="770" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="(?-s)^\tLine.+\R" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1702" wParam="0" lParam="770" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="\s\(\d+\shits?\)$" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam='" .' /> <Action type="3" message="1702" wParam="0" lParam="770" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" /> <Action type="3" message="1700" wParam="0" lParam="0" sParam="" /> <Action type="3" message="1601" wParam="0" lParam="0" sParam="^\s+" /> <Action type="3" message="1625" wParam="0" lParam="2" sParam="" /> <Action type="3" message="1602" wParam="0" lParam="0" sParam='copy "' /> <Action type="3" message="1702" wParam="0" lParam="770" sParam="" /> <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
- Save the
shortcuts.xml
file - Exit Notepad++
- Restart Notepad++
That’s the end of the one-time setup. That will remain until you delete the macro created.
To use it, each time you need to:
- Run a Find-in-Files search that results in several hit files
- Copy and paste the Find-result panel results (per the earlier instructions on how to do that) into an empty Notepad++ tab (an empty file called
test.bat
might be appropriate!) - Go to the Macro menu and choose
Bat file from Find results
from near the bottom; you should observe your file of search results has changed into a bunch ofcopy
batch file commands (this step “runs” the macro consisting of several replacement operations!) - Save the file
- Continue from the earlier instructions at step
get a cmd prompt in the directory containing...