List files with specific extensions

Hello I am doing the following in a Windows XP embedded machine:

dir /b /s *.ext

Instead of getting ONLY the files with extension .ext I also get files with extension .ext_

Is there a way to prevent this?

Thanks in advance
ntzanosAsked:
Who is Participating?
 
Steve KnightIT ConsultancyCommented:
What happens is that the %%a variable gets set to each line of the output of the dir in turn.  The "~" bit strips off any existing " " marks and the %%~xa gets the extension of that dir entry.

I missed a few bits off from mobile earlier sorry.  Please try one of these methods


@echo off
cd /d c:\yourdir

echo Method 1:
for /f "tokens=*" %%a in ('dir /s/b /a-d') do if /i "%%~xa"==".ext" echo "%%~a"

echo Method 2:
for /r %%a in (*.ext) do if /i "%%~xa"==".ext" echo "%%~a"

echo Method 1 to a file:
(for /f "tokens=*" %%a in ('dir /s/b /a-d') do if /i "%%~xa"==".ext" echo "%%~a") > file1.txt

echo Method 2 to a file:
(for /r %%a in (*.ext) do if /i "%%~xa"==".ext" echo "%%~a") > file2.txt

echo Or you can work on this output for a del command, e.g.
for /f "tokens=*" %%a in ('dir /s/b /a-d') do if /i "%%~xa"==".ext" echo del /q "%%~a"

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
good point, perhaps try:

dir /b/s "*.ext"

icant check what works fro mobile at the mo. Sorry!

If we cant do it with dir alone can try with for /r:


for /r %%a in (*.ext) do echo "%%~fa"

we can adjust with f being combination of dpnx (drive, path, name, extension) too if you want different results.


Steve





0
 
PreeceCommented:
Check this page:

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/dir.mspx?mfr=true

I don't think that you can prevent the .ext_ from appearing in the results.  However, you might try sorting by extension, then perhaps the undesirable files will be grouped together and more easily ignored (or more easily deleted if output is going to a text file?).

dir /b /s /oe

The /oe sorts in alphabetic order by extension

Preece
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
ntzanosAuthor Commented:
The output is going to a text file in order for these files to be deleted. but still how would I tell them apart even later on?
0
 
Steve KnightIT ConsultancyCommented:
if previous options suggested do not work try this:

@echo off
cd /d c:\yourdir
for /f %%a in ('dir /s/b') if /i "%%~xa"==".ext" echo "%%~a"

if you want it in a log file then add ( before the word for, ) after the last quote and then redirect to file, e.g.

(for ...... %%a") >logfile.txt

Steve
0
 
ntzanosAuthor Commented:
This last one does not work at all. Is it possible to resend it with the initial variable capitalized?
0
 
Steve KnightIT ConsultancyCommented:
not sure what you mean sorry?
0
 
ntzanosAuthor Commented:
The command does not return a correct output, it seems. But I am also confused, whether the "a" is the variable name or part of the expression
0
 
Bill PrewCommented:
Try this:

dir /b /s *.ext | findstr /r /i /c:"\.ext$"

Open in new window

~bp
0
 
BillDLCommented:
You might also be able to modify your variable to eliminate that last _ character.

Say you have a Variable named "TESTVAR" and you have set its value to "Text String" in a batch file like this:
set TESTVAR=Text String
When used anywhere in the same batch file you can expand that variable to its value using %TESTVAR%, eg.
echo The variable named TESTVAR contains: %TESTVAR%

You can modify a variable using this syntax:
%TESTVAR:~x,y%
where "x" declares the starting point from the start of the string (ie. from the left starting at position Zero), and the "y" is used to tell it how many characters from that point to return, or can be used to declare a position working backwards starting from the end of the string (ie. the Right).

echo %TESTVAR:~0,6%
would start at the First character from the left of "Text String" (the "T") and would include 6 characters, so it would echo back "Text S".

You can see more about this if you open a command window and type   SET /?  then page down a bit.

You can use a minus symbol like this:
echo %TESTVAR~0,-3"
which tells it to start at the First character, and to chop off the last 3 characters ("ing"), resulting in "Text Str".

You can use that theory to do a DIR listing for files with eg. the .EXE file extension only, and then test if the last character is an underscore.  If it IS, then ignore it and just redirect the paths of the files WITHOUT a trailing underscore to your file list.
 
@echo off
SetLocal EnableDelayedExpansion
set CurrDir=%~dp0
set CurrDir=%CurrDir:~0,-1%
set FileList=%CurrDir%\Listing.txt
set UNDERSCORE=_

set BaseDir=C:\_TEST

echo Listing Minus .ex_ files>"%FileList%"

for /f "tokens=* delims=" %%A in ('dir /on /b /s "%BaseDir%\*.exe"') do (
    set FILEPATH=%%A
    set LASTCHAR=!FILEPATH:~-1!
    if !LASTCHAR! neq %UNDERSCORE% echo !FILEPATH!>>"%FileList%"
)

pause

Open in new window


Another method would be to use the FINDSTR command with the /E and /V switches to filter a DIR listing and ONLY redirect file paths where the LAST Character IS NOT an Underscore.
0
 
Steve KnightIT ConsultancyCommented:
I don't know you sare to walk home for a few minutes after a boring journey tapping away on EE on the mobile and everyone turns up ... hi guys! :-)
0
 
Steve KnightIT ConsultancyCommented:
dare even.
0
 
BillDLCommented:
Hi ntzanos

It seems that you are a bit puzzled about the use of the   %%~xA    type of variable.  If you open a command window and type    FOR /?   then page right down to the end, you will see a list of modifiers that can be used to get the Drive, Path, FileName, and Extension singularly.
0
 
Steve KnightIT ConsultancyCommented:
Good idea Bill btw, I suppose if there were no files you'd end up with a "file not found" to capture with a 2>, i.e.

dir /b /s *.ext 2>NUL | findstr /r /i /c:"\.ext$"

or something...
Steve
0
 
BillDLCommented:
Man, this is more congested than the M25 Motorway at 5pm!!

Hey Steve:
"walk home for a few minutes after a boring journey tapping away on EE on the mobile and everyone turns up" ...
Just watch out for missing manhole covers and fountains!
http://www.youtube.com/watch?v=9ClBpYtKJ7c
http://www.youtube.com/watch?v=CeVn6_iwtJQ
I saw a guy out with his dog this morning tapping away on his mobile and he fell flat on his face over a crack in the sidewalk!  I'm sorry, but I had to honk the horn and wave so he knew he had been seen ;-)
0
 
Steve KnightIT ConsultancyCommented:
Nah, was tapping away on the 20 min wait for the 30 min train journey to wait 20 mins for the 50 minute bus journey.... YAWN! not while I was walking!.... did see a "youth" on the other side of the road with his GF I assume roller skating backwards along path while tapping on mobile!!
0
 
Paul TomasiCommented:
Surely all that's required is something like this:

   dir /b /s | findstr /e ".ext"


If that doesn't work due to case-sensitivity the add the '/i' option as in:

   dir /b /s | findstr /i /e ".ext"


Easypeasy!
0
 
Bill PrewCommented:
That is is what I proposed in 36926441 paul...

~bp
0
 
Paul TomasiCommented:
Bill

This is what you posted in http:#36926441

   dir /b /s *.ext | findstr /r /i /c:"\.ext$"


This is what I posted in http:#36927098

   dir /b /s | findstr /e ".ext"


Tiredness can play all sorts of tricks on your eyes!...
0
 
Steve KnightIT ConsultancyCommented:
If we are getting finnicky here what about those directories he might have called something.ext ...
0
 
ntzanosAuthor Commented:
Thanks all for the help. I will try tomorrow at work and provide feedback. One problem in the above solutions is that findstr is not included in this setup of Windows embedded.
0
 
Bill PrewCommented:
@paultomasi

Sorry if I confused you, I didn't mean letter by letter, just the core approach.

~bp
0
 
Paul TomasiCommented:
Thank you for clarifying there is no FINDSTR in XP Embedded. In that case, the following batch file code is all that is required:
@echo off
for /r %%a in (*) do if "%%~xa"==".ext" echo %%a

Open in new window



Can include a starting directory too, like this:
@echo off
for /r "c:\startdir" %%a in (*) do if "%%~xa"==".ext" echo %%a

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
Then for is a built-in command within cmd.exe so pls look at my options in http:#36926474 which should do the job without.

Steve
0
 
Steve KnightIT ConsultancyCommented:
Paul... please stop jumping in at the end of questions posting what we have already done --> see http:#36926474
0
 
Paul TomasiCommented:
dragon-it

I've looked at your comment (http:#36926474) and I don't see where I'm supposed to have posted the 'same' code.


This is my code:
for /r %%a in (*) do if "%%~xa"==".ext" echo %%a

Open in new window


These are your codes:
for /r %%a in (*.ext) do echo "%%~fa"

Open in new window

for /r %%a in (*.ext) do if /i "%%~xa"==".ext" echo "%%~a"

Open in new window


As you can see, both your codes are different to mine.

Furthermore, your first code fails as it returns filenames with '.ext' AND '.ext_' extensions (and I don't think the asker wanted quotes around the output).

Your 2nd code also puts quotes around the output.
0
 
Steve KnightIT ConsultancyCommented:
Paul.  I give up.... And no it doesn't the code does the same as yours.... It checks the extension is .ext using for command and if....
0
 
Steve KnightIT ConsultancyCommented:
oh yeah i KNOW the first code fails... It was a trial, without aid of pc of what MAY work.... Which is why I posted new code with four options.

Being pedantic again then he wanted it in a FILE in order to use it for deleting - both in my example above.

has t0t0 returned?
0
 
Paul TomasiCommented:
No not returned Steve.... But please allow me to point something out to you...

YOU: "please stop ... posting what we have already done"

YOU: "...the code does the same as yours..."


YOU: for /r %%a in (*.ext) do if /i "%%~xa"==".ext" echo "%%~a"

ME:   for /r %%a in (*) do if "%%~xa"==".ext" echo %%a


Not only are the two lines of code plainly different, they DO NOT produce the same results so I'm puzzled as to why you say they do.

Sometimes tiredness can play tricks on your eyes!
0
 
Paul TomasiCommented:
Steve

>> "oh yeah i KNOW the first code fails... It was a trial, without aid of pc..."

Hahaha... Sounds like something a Microsoft employee might say?
0
 
Steve KnightIT ConsultancyCommented:
Correct, they don't.  

Yours wastefully looks at all files when it needs only to look at .ext files.
Yours matches against ext but not .Ext, .EXT etc. as you didn't make it case insensitive (the only real difference).

Oh yeah have you tried typing in lots of symbols on a phone keypad while being bumped up and down on bus... and the one you mentioned gives the same as a dir command ... but couldn't tell that until tried on PC, at which point I put an IF on to check the extension.

Anyway lets just leave this to the asker to try and work through the stupidly long thread for a one line batch!
0
 
BillDLCommented:
Have a nice weekend Steve ;-)
0
 
Paul TomasiCommented:
ntzanos

Please allow me to confirm my solution (which is almost hidden by all the comments above)...
for /r %%a in (*) do if "%%~xa"==".ext" echo %%a

Open in new window


You can also include the '/i' option if case-sensitivity is a problem. Like this:
for /r %%a in (*) do if /i "%%~xa"==".ext" echo %%a

Open in new window


If this is not run inside the folder containing your files then you can include a 'start-folder'. Like this:
for /r "c:\start folder" %%a in (*) do if /i "%%~xa"==".ext" echo %%a

Open in new window



NOTE:

1) Handles all '.EXT' files corectly.
2) Deals with FOLDERS which may also have and '.EXT' extension
3) Behaves well if there are NO matching files
0
 
Steve KnightIT ConsultancyCommented:
FFS Paul.  NOW IT IS THE SAME AS MIN ALREEADY POSTED AGES AGO
0
 
Paul TomasiCommented:
When two solutions converge into one, the odds are we've reached perfection....      

YOU:   for /r %%a in (*.ext) do if /i "%%~xa"==".ext" echo "%%~a"
ME:     for /r %%a in (*      ) do if /i "%%~xa"==".ext" echo  %%a

As you can see, you're almost there....

:)
0
 
Steve KnightIT ConsultancyCommented:
Ok I can't let this lie... with and without filter on the same data set:

10:14:24.87 Start *
10:14:48.67 End *
10:14:48.67 Start .jpg
10:14:53.43 End .jpg

@echo off
echo %time% Start *
  (for /r %%a in (*) do if /i "%%~xa"==".jpg" echo %%a ) > file1.txt
echo %time% End *

echo %time% Start .jpg
  (for /r %%a in (*.jpg) do if /i "%%~xa"==".jpg" echo "%%~a" ) > file2.txt
echo %time% End .jpg

0
 
Steve KnightIT ConsultancyCommented:
C:\1. DATA>fc file1.txt file2.txt
Comparing files file1.txt and FILE2.TXT
FC: no differences encountered
0
 
ntzanosAuthor Commented:
Hello there,

You may not believe it but that a constructive debate you too had, as your arguments really played the role of an extended help command for me (like the /i in the if is for case insensitive etc)

The code needed a very slight modification, but it seems to have worked like a charm.
Being very negative against windows in general, having people really expert in the subject is really nice and helpful. Thanks all
0
 
Steve KnightIT ConsultancyCommented:
Yes there are lot of good people here for scripting requirements - BillDl, billprew, Qlemo, paultomasi etc. though we do tend to go off on lengthy discussion about technically making things better, prettier, or faster to the nth degree a LOT, especially when paul appears :-)
0
 
BillDLCommented:
Thanks Steve.
0
 
Paul TomasiCommented:
I notice my name was 4th in that list....
0
 
Steve KnightIT ConsultancyCommented:
No order intended!
0
 
Bill PrewCommented:
More importantly, you left your name off the list, quite gracious Steve, but I'll say it if you won't, you're on the list, and always a valued contributor in these zones.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.