How can I make batch regex expression (in FINDSTR /r /c:"ng") match this window title only? It matches also MongoDB terminal window.
"ng\>" didn't work.
Full code for context:
for /f "tokens=2 delims=," %%a in ('
TASKLIST /fi "imagename eq cmd.exe" /v /fo:csv /nh ^| FINDSTR /r /c:"ng"
') do taskkill /PID %%a
EDIT:
Additional info from my comment under Squashman's response:
How to avoid returning multiple PIDs from last call? script examples screenshot
List of opened windows: windows screenshot
Using the option to find at the beginning of the word worked in my testing.
TASKLIST /fi "imagename eq cmd.exe" /v /fo:csv /nh | FINDSTR "\<ng"
This also worked in my testing.
TASKLIST /fi "imagename eq cmd.exe" /v /fo:csv /nh | FINDSTR "\"ng\""
Related
In a CMD script, I am trying to determine if a string has a "/" as the last character or not, using a regular expression with the findstr command. However, in both cases, I am getting a return code of 1.
#echo off
SETLOCAL enabledelayedexpansion
SET Project=/Folder1
ECHO.%Project% | findstr /R /C:"/$" 1>nul
ECHO %errorlevel%
SET Project=/Folder1/
ECHO.%Project% | findstr /R /C:"/$" 1>nul
ECHO %errorlevel%
Is there an issue here with the syntax?
I'm trying to echo to the screen all the sub-folder/directories only in the current path with a certain type of regex (that starts with "JobResults"), but nothing is showing on the screen.
For /d %i in ("C:\Interns 2013 Projs\JobResults*") DO echo %~ni
If I replace /d with /r, only files not directories are echoed to the screen
try this:
For /d /r "C:\Interns 2013 Projs" %i in (JobResults*) DO echo %~ni
If you want the results only to be echoed to the screen, this will do:
dir /s /ad /b "C:\Interns 2013 Projs\JobResults*"
If you're looking for all subfolders whose name starts with JobResults, like
C:\Interns 2013 Projs\JobResults 23
C:\Interns 2013 Projs\bar\JobResults_42
something like this should work:
#echo off
for /r %%d in (.) do (
echo %%~nd | findstr /r "^JobResults" >nul 2>&1 && echo %%~fd
)
If you're looking for all subfolders of those folders in the working directory whose name starts with JobResults, like
C:\Interns 2013 Projs\JobResults 23
C:\Interns 2013 Projs\JobResults 23\foo
C:\Interns 2013 Projs\JobResults 23\foo\bar
something like this should work:
#echo off
for /f "tokens=*" %%d in ('dir /b /a:d /s') do (
echo %%~d | findstr /r "^%CD:\=\\%\\JobResults" && echo %%~d
)
we used Regex in batch by using following command,
Dir "C:\Test\Res345_45664_1335" /s /b /a:-d | findstr /R "[(\d+)_(\d+)_(\d+)]" > filelist.txt
The "C:\Test\Res345_45664_1335" directory contains following files,
Res345_45664_1335.txt
Output.txt
list.txt
We need file that’s in the format
But the above dir command with regex displaying all files present in the "C:\Test\Res345_45664_1335" directory. Because "C:\Test\Res345_45664_1335" directory contains the same format "Res345_45664_1335". But We need files only(with full path).
Thanks.
\d, () and + are not a valid meta characters in findstr. See findstr /? fore more advanced help. You should substitute it with [0-9][0-9]* .
Dir "C:\Test\Res345_45664_1335" /s /b /a:-d | findstr /ER "[0-9][0-9]*_[0-9][0-9]*_[0-9][0-9]*.txt" > filelist.txt
Do the files have extensions?
Dir "c:\test\Res345_45664_1335" /s /b /a:-d | findstr /R "[0-9]*_[0-9]*_[0-9]*\."
try
Dir /s /b /a:-d *_*_*
Not really sure what you mean by "the format"
Ah - filename in format string_string_string...
FOR /f "delims=" %%i IN ('dir /s /b /a-d *_*_*') DO ECHO "%%~ni"|FINDSTR /r "..*_..*_..*" >nul&IF NOT ERRORLEVEL 1 ECHO %%i
(that's as a batch line - reduce %% to % to run from the prompt)
Batch scripting does not have such feasibility as like programming languages. Some simple regular expression can be worked with 'findstr' but it has some limitations like '+' character has replacement as '.*'. But don't worry, I have a solution for you.
you can simply use power-shell inside a batch script and here below your solution:
#powershell -command "if (($string -match $regex) -eq $true) {write-host matched!}"
It will work for you and if you want to throw an exception then simply throw and assert in batch script as:
#powershell -command "if (($string -match $regex) -eq $true) {throw [System.ArgumentException] invalid input}"
I hope this would suffice for you and others who may in need of this.
I have a list of keywords in a keywords.txt file. I have another file list.txt with the keywords in the beginning of each line. How can I sort the lines in list.txt to the same order they appear in keywords.txt?
keywords.txt
house
car
tree
woods
mailbox
list.txt
car bbdfbdfbdfbdf
tree gdfgvsgsgs
mailbox gsgsdfsdf
woods gsgsdgsdgsdgsdgsddsd
house gsdgfsdgsdgsdgsdg
final result in list.txt
house gsdgfsdgsdgsdgsdg
car bbdfbdfbdfbdf
tree gdfgvsgsgs
woods gsgsdgsdgsdgsdgsddsd
mailbox gsgsdfsdf
Here is an improved and simplified version of kiswa's answer.
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bl "%%A" list.txt
)>sorted.txt
REM move /y sorted.txt list.txt
The FINDSTR command only matches lines that begin with the keyword, and it forces the search to be a literal search. (FINDSTR could give the wrong result if the /L option is not specified and the keyword happens to contain a regex meta-character.)
The code to replace the original file with the sorted file is commented out. Simply remove the REM statement to activate the MOVE statement.
As with kiswa's answer, the above will only output lines from list.txt that match a keyword in keywords.txt.
You might have lines in list.txt that do not match a keyword. If you want to preserve those lines at the bottom of the sorted output, then use:
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bli "%%A" "list.txt"
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
Note that the /I (case insensitive) option must be used because of a FINDSTR bug dealing with multiple literal search strings of different lengths. The /I option avoids the bug, but it would cause problems if your keywords are case sensitive. See What are the undocumented features and limitations of the Windows FINDSTR command?.
You might have keywords that are missing from list.txt. If you want to include those keywords without any data following them, then use:
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bl "%%A" "list.txt" || echo %%A
)>sorted.txt
::move /y sorted.txt list.txt
Obviously you can combine both techniques to make sure you preserve the union of both files:
#echo off
(
for /f "usebackq" %%A in ("keywords.txt") do findstr /bli "%%A" "list.txt" || echo %%A
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
All of the above assume the keywords do not contain space or tab characters. If they do, then the FOR /F options and FINDSTR options must change:
#echo off
(
for /f "usebackq delims=" %%A in ("keywords.txt") do findstr /bic:"%%A" "list.txt" || echo %%A
findstr /vblig:"keywords.txt" "list.txt"
)>sorted.txt
::move /y sorted.txt list.txt
$ join -1 2 -2 1 <(cat -n keywords.txt | sort -k2) <(sort list.txt) | sort -k2n | cut -d ' ' -f 1,3-
house gsdgfsdgsdgsdgsdg
car bbdfbdfbdfbdf
tree gdfgvsgsgs
woods gsgsdgsdgsdgsdgsddsd
mailbox gsgsdfsdf
Here's a Windows batch file. It's probably not the most efficient, but I think it's nicely readable.
#echo off
for /F "tokens=*" %%A in (keywords.txt) do (
for /F "tokens=*" %%B in ('findstr /i /C:"%%A" list.txt') do (
echo %%B >> sorted.txt
)
)
del list.txt
rename sorted.txt list.txt
This creates a sorted file, then removes the list file and renames the sorted file.
I have a DOS batch file that performs an action for files beginning with text "SP", as follows:
FOR /F "tokens=*" %%A IN ( 'DIR SP*.sql /s /b' ) DO ECHO .compile_file = "%%A" >> output.txt
The key bit here is obviously:
DIR SP*.sql /s /b
I need to do a similar thing before the "FOR" line above, but for all other files not starting with SP*.sql
Something like:
FOR /F "tokens=*" %%A IN ( 'DIR [^SP]*.sql /s /b' ) DO ECHO .run_file = "%%A" >> output.txt
FOR /F "tokens=*" %%A IN ( 'DIR SP*.sql /s /b' ) DO ECHO .compile_file = "%%A" >> output.txt
Does anyone know how I can do this?
Can I use regex for this sort of thing?
The batch file will run under CMD in Windows XP.
Cheers
In batch, usually you can use findstr with /V to negate regex , eg
.... | findstr /V "^SPL"
try passing the filename to findstr and see how it goes
%~nI - expands %I to a file name only