Jump to content

detecting files using bat


pacerier

Recommended Posts


Try this:

setlocal
dir /s /b c:\*.abc >c:\dir.txt
set detected=no.no
for /f "delims=;" %%i in (c:\dir.txt) do (set detected=%%i)
if "%detected%"=="no.no" goto end
echo file detected: %detected%
:end
endlocal

Link to comment
Share on other sites

isn't this easier?

Dir /B /S %1*%2 | FIND "%2">nul
ECHO Errorlevel is %ERRORLEVEL%

@echo off
::Check if a file exists in a given directory
::(and subs) by extension - FFBE.CMD
::by Jacopo Lazzari
::thanks to Rob van Der Woude for the examples, tutorials and
::info on his page http://www.robvanderwoude.com/
::and to Simon Sheppard http://www.ss64.com/
::-----------------------------------------------------------
::Usage: FFBE.CMD <path> <.ext>
:: path must end with \
::-----------------------------------------------------------
::Example FFBE.CMD

Dir /B /S %1*%2 | FIND "%2">nul
If %ERRORLEVEL%==0 Echo At least one file with .ext %2 was found

(no need to write to a temp file) ;)

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

hi thanks, but can we omit >nul?

Yes, sure, the only difference will be that the output of the command will be shown on screen, i.e. a listing of all found files matching *.ext.

the >nul redirects the standard output of the command to a "NULL DEVICE" instead of default "CONSOLE".

jaclaz

Link to comment
Share on other sites

Of course, Windows made that a bit easier with the "search" function.

Just do a search for *.ABC for instance. All files that fill the bill will be displayed on your screen for you to see.

That sure makes it easy to find files of a type or name.

Using a batch file, if you don't save the result to a txt file, is kind of a waste of effort.

If you're going to the effort of writing a batch file, at least use it to its fullest possibility.

dir C:\*.ABC /s >result.txt

will not only find the filenames for you but list them in a text file that you can read or print at your leisure.

For instance, when I ran: dir C:\*.wab /s >wabfiles.txt on my own PC,

I got the following listing saved to a file called 'wabfiles.txt' which I could easily read with Wordpad.

Volume in drive C is SATAMAINMAX

Volume Serial Number is 3D3B-1CDA

Directory of c:\Documents and Settings\Randy\My Documents

07/21/2006 03:39 PM 333,334 myadbook.WAB

1 File(s) 333,334 bytes

Directory of c:\Documents and Settings\Randy\Application Data\Microsoft\Address Book

09/10/2006 12:28 AM 453,305 Randy.wab

09/10/2006 12:28 AM 265,281 Randy.wab~

2 File(s) 718,586 bytes

Total Files Listed:

3 File(s) 1,051,920 bytes

0 Dir(s) 33,878,114,304 bytes free

Good Luck,

Andromeda43 B)

PS: My name's NOT Randy. :rolleyes::whistle:;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...