I don't understand why findstr doesn't work as I want.
I have the following files in my test directory:
aaa.jpg
bbb.png
ccc.svg
aaa_s.jpg
bbb_s.png
ccc_s.svg
aaa_small.jpg
bbb_small.png
ccc_small.svg
And I have the following line to pass directly to cmd.exe:
for /f "delims=" %f in ('dir /b /a:-d ^| findstr /ile "gif jpg png svg" ^| findstr /ie "_s.*"') do echo "%f"
To my opinion, it should match the following files:
aaa_s.jpg
bbb_s.png
ccc_s.svg
However, it's actually matches
aaa_s.jpg
bbb_s.png
ccc_s.svg
aaa_small.jpg
bbb_small.png
ccc_small.svg
What I'm doing wrong?
. is a FindStr wildcard for any character, and * is for zero or more occurrences of the previous character. So obviously _s.* matches _s followed by any character zero or more times; which covers _sm.
Please open a Command Prompt window, type findstr /?, press the 'enter' key, and read the usage information.
BTW, what's wrong with using:
Dir /B /A:-D *_s.*
If needs be you could pipe that to FindStr with /I /L /E ".gif .jpg .png .svg" for example:
Dir /B /A:-D *_s.* | FindStr /I /L /E ".gif .jpg .png .svg"
Alternatively you could include multiple matches to your Dir command and forget about using FindStr entirely:
Dir /B /A:-D "*_s.gif" "*_s.jpg" "*_s.png" "*_s.svg"
Related
I have multiple files with the same filename under various subdirectories, for example:
c:\kjhsd\client.txt
c:\ekjjs\client.txt
c:\oiwnk\client.txt
I do not know the middle part of the path represented by random letters above, but the filename is always consistent.
I need a way to use this command: type client.txt (or) more client.txt but to display the content of all text files with the filename "client.txt" under any directory at the same time.
So if:
c:\kjhsd\client.txt contained: hello
c:\ekjjs\client.txt contained: helloworld
c:\oiwnk\client.txt contained: helloagain
After running the single command, I would see:
hello
helloworld
helloagain
Thanks in advance :)
from cmd.exe:
#for /r c:\ %a in ("client.txt") do #type "%~a"2>nul
From a Batch-File:
#for /r c:\ %%a in ("client.txt") do #type "%%~a"2>nul
Edit: as per the newline requirement:
#echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
type "%%~a"2>nul
echo(
)
Popd
or if you want to see which file had what text:
#echo off
Pushd "c:\somedir"
for /f %%a in ('dir /b /s /a-d ^| find /i "client.txt"') do (
echo content: "%%~a"
type "%%~a"2>nul
echo(
)
Popd
The easiest approach is to use a for /D loop to find the different directories, and to let find /V "" return the contents of the files, because then a line-break is automatically appended to every file in case there is none in the file itself (opposed to type).
In Command Prompt do this:
#for /D %I in ("D:\some\root\*") do #(< "%~I\client.txt" find /V "") 2> nul
In a batch file do this:
#for /D %%I in ("D:\some\root\*") do #(< "%%~I\client.txt" find /V "") 2> nul
The 2> nul portion suppresses eerror messages if a directory does not contain a file called client.txt.
I am trying to write a Windows batch file where there are two files in concern.
The first file all.err contains logs of failed test cases like below:
`CHECK:FAIL,"It should have been DEST`"
`CHECK:FAIL,"It should have been XYZA`"
`CHECK:FAIL,"It should have been PRTS`"
`CHECK:FAIL,"It should have been ABCD`"
.....................................
We have another file exclude.txt which stores some string per line like:
XYZA
ABCD
I am trying to write a Windows batch script which can list all lines from all.err that do not contain any word in exclude.txt.
I am not able to understand how this can be implemented - any idea?
There is just one Windows command line necessary for this task as written by SomethingDark:
%SystemRoot%\System32\findstr.exe /I /L /V /G:exclude.txt all.err
The help output on running in a Windows command prompt window findstr /? explains the parameters /I (case-insensitive), /L (search strings are literal strings), /V (inverse matching output), /G (get search strings from file).
When using a for loop, you need to go through exclusions file first, get each line, then exclude these meta viariables from your search in the log file:
#echo off
for /f "delims=" %%i in (exclude.txt) do (
for /f "delims=" %%b in ('type all.log ^| findstr /V "%%i"') do echo(%%b
)
The first do code block is not needed, I simply posted it like that for readability, it simply can be:
#echo off
for /f "delims=" %%i in (exclude.txt) do for /f "delims=" %%b in ('type all.log ^| findstr /V "%%i"') do echo(%%b
I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"
I know I can get any file by type in a directory tree with
dir /s /b *.ext > list.txt
or any subdirectory by name with
dir /s/b *directoryName* >list.txt
but how do I combine this to get a list of files by type AND sub-directory name?
dir /s /b *directoryname\*.ext
>>The filename, directory name, or volume label syntax is incorrect.
I may have understood but cant you just do:
dir /s /b "SubfolderName\*.ext"
?
If you want full recursion you can use findstr to whittle away what you don't need from a full dir /b /s.
dir /s /b | findstr /i "directoryname.*\.ext$" >list.txt
... will match .ext files in any directory containing directoryname (including directoryname01 or 01 - directoryname), where the directory may be more than one level deep from the current directory.
In the findstr search regexp, the .* after directoryname signifies 0 or more of any character. In glob syntax it's like *directoryname*.ext. See help findstr for more info on findstr syntax.
dir /s /b /a-d *.ext|findstr /i /r ".*\\.*dirname.*\\.*"
The /a-d suppresses directorynames matching the .ext mask - filenames only.
The /r on the findstr is probably superfluous, as regex is default
The /i forces case-insentitivity
The string-to-match is
[any number of any characters]\[any number of any characters][directoryname][any number of any characters]\[any number of any characters]
since \ escapes \
read HELP FOR and try this in a command line
for /D %a in (dirname*) do #dir /s /b %a*.ext
for /R /D %a in (dirname*) do #dir /s /b %a\*.ext 1>>list.txt 2>nul
In a Windows cmd batch script named my.bat, I want to execute a command for all the files in the current directory except for the batch file my.bat.
I use below command in my.bat currently to run the command only for *.txt *.ppt, but really as new files of different extensions might be added to the folder and hence execution of this command by excluding one type of file extension (in my case *.bat) would be more readable/helpful.
FOR /F "delims=|" %%i IN ('dir /b *.txt *.ppt') DO echo %%i
Question is: How do I exclude that file alone with that particular file extension and make the for command execute on all files of all extensions except the excluded one?
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do if not %%~xi==.bat echo %%i
I have added tokens=* in as well otherwise you won't get full filenames if they have spaces.
To echo without the dot
setlocal enabledelayedexpansion
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do (
set e=%%~xi
set e=!e:.=!
echo !e!
)
This is providing that the file doesn't have any other dots, otherwise it will remove them too. This is a bit more sturdy than just removing the 4th character from the end though, as not all files have a 3 character extension.
You could pass the output of the dir /b command through findstr, like so:
FOR /F "delims=|" %%i IN ('dir /b ^| findstr /v /r "^my.bat$"') DO echo %%i
The /v option to findstr prints anything that doesn't match the parameter. The match is based on a regular expression that matches only lines that contain my.bat and nothing else.