?
Solved

List files with specific extensions

Posted on 2011-10-06
43
Medium Priority
?
486 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:ntzanos
  • 19
  • 9
  • 5
  • +3
43 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36925913
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
 
LVL 12

Expert Comment

by:Preece
ID: 36925921
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
 

Author Comment

by:ntzanos
ID: 36925936
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36926041
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
 

Author Comment

by:ntzanos
ID: 36926103
This last one does not work at all. Is it possible to resend it with the initial variable capitalized?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36926164
not sure what you mean sorry?
0
 

Author Comment

by:ntzanos
ID: 36926232
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36926441
Try this:

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

Open in new window

~bp
0
 
LVL 39

Expert Comment

by:BillDL
ID: 36926465
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
 
LVL 43

Accepted Solution

by:
Steve Knight earned 1200 total points
ID: 36926474
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36926483
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36926486
dare even.
0
 
LVL 39

Expert Comment

by:BillDL
ID: 36926498
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36926536
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
 
LVL 39

Expert Comment

by:BillDL
ID: 36926559
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36926605
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36927098
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36927344
That is is what I proposed in 36926441 paul...

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36927454
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36927790
If we are getting finnicky here what about those directories he might have called something.ext ...
0
 

Author Comment

by:ntzanos
ID: 36927945
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36928013
@paultomasi

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

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36928033
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36928038
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36928041
Paul... please stop jumping in at the end of questions posting what we have already done --> see http:#36926474
0
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 800 total points
ID: 36928216
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36928905
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36928912
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36929060
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 36929072
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36929090
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
 
LVL 39

Expert Comment

by:BillDL
ID: 36929109
Have a nice weekend Steve ;-)
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36929111
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36929176
FFS Paul.  NOW IT IS THE SAME AS MIN ALREEADY POSTED AGES AGO
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36929723
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36929766
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36929772
C:\1. DATA>fc file1.txt file2.txt
Comparing files file1.txt and FILE2.TXT
FC: no differences encountered
0
 

Author Closing Comment

by:ntzanos
ID: 36930416
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36930450
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
 
LVL 39

Expert Comment

by:BillDL
ID: 36930618
Thanks Steve.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36931203
I notice my name was 4th in that list....
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36931468
No order intended!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36932424
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

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question