Solved

Searching for Existence of File Using DOS

Posted on 2008-10-08
12
672 Views
Last Modified: 2012-05-05
How would I search a directory for the existence of a file using a batch file?
0
Comment
Question by:justpg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22673352
if exist YourFile.doc echo hi
0
 
LVL 3

Expert Comment

by:hyperlite
ID: 22673360
really depends on what you are trying to do.  You could do a if exist then do something like below:

@echo off
 
echo ********Start Batch Script********
 
echo Check for File
 
IF EXIST "c:\file.txt" goto :FILEDONE
echo File not found. Copying...
xcopy /s /c /e /h /i /r /y "\\server\file.txt" "c:\file.txt"
:FILEDONE
echo ******Finish Installer Script******

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22673607
could also do:

dir \myfile.txt /s |find /i "Directory of"

but it depends on what you want to do with it once it's found...to get it into a variable...

for /f "tokens=3*" %a in ('dir \myfile.txt /s^|find /i "Directory of"') do echo Found the file at %a
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 2

Expert Comment

by:KevinNo
ID: 22676563
You could also open a cmd.exe. Change to the root of the drive you want to search and use a dir /s command. Example: Finding a file called desktop.in in every folder on C:
Start > Run
cmd
c:
cd\
dir /s desktop.ini

You can output to a txt file by using dir /s desktop.ini > export.txt
0
 
LVL 16

Expert Comment

by:t0t0
ID: 22723394
HIDDEN FILES

In a DOS box...

1) Create a file named FILE.TMP anywhere on your drive, eg...

   COPY CON file.tmp

The press [CTRL]-Z to write the empty file to the drive

2) Hide the file

    ATTRIB +H file.tmp

3) Navigate to the root directory of the drive

    CD \

4) Now find the file

    a) Method 1:    DIR /S /A:H file.tmp

    b) Method 2:    ATTRIB file.tmp /S

Neat eh?


(DIR has /A:H (hidden attribute) option to display ONLY files that are hidden)
0
 
LVL 16

Expert Comment

by:t0t0
ID: 22723476
Oh, I forgot your batch file....

Here goes... don't blink thought because it's a short one...

----------------------------------
@ATTRIB %1 /S
----------------------------------

There you go.... Just one line

Name the batch file LOCATE.BAT

And find your file.... Let's say your're looking for a file named FILE.TMP. Just enter

    LOCATE file.tmp

On the DOS command line and, well, magic....Try it and see.....
0
 
LVL 16

Expert Comment

by:t0t0
ID: 22723529
HOW TO CREATE YOUR BATCH FILE IN A DOS BOX

    COPY CON locate.bat <CR>

    @ATTRIB %1 /S<CR>

    <CTRL>Z<CR>



NOTES:

    <CR> is the Carriage Return key (or Enter key}

    <CTRL>Z is CONTROL key and Z pressed together

    COPY CON just means copy the console - here, the keyboard
0
 

Author Comment

by:justpg
ID: 22761179
More specifics:

Want to find a file in a a specific directory having a name containing a date...e.g. yyyymmprocess.
Want to automate process.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 22772732
Easy....

suppose you're looking for a file named: 200810prog.exe

and supposing the file is in: C:\temp

You could find the file using a variety of ways:

1. By date (the date is added automatically)

    DIR c:\temp\%date:~-4%%date:~-7,2%prog.exe

2. By program name:

    DIR c:\temp\??????prog.exe

3. Explicit search where you enter specify the name and the date:

    DIR c:\temp\200810prog.exe

4. Recursive search from by name  (will search the whole drive):

    DIR /s c:\%date:~-4%%date:~-7,2%prog.exe

5. Recursive search by date (will search the whole drive):

    DIR /s c:\%date:~-4%%date:~-7,2%????.???

6. Recursive search of ALL files prefixed by date (will search whole drive):

    DIR /s c:\%date:~-4%%date:~-7,2%*.*

7. Find any file created 'today' (very powerful - searches whole drive):

    DIR /s /a:-d c:\ | FIND "%date:~-10%"

8. Find ALL files of a specific date ('today') and add them to a list in a text.file named files.txt (extremely powerful):

    DIR /s /a:-d c:\ | FIND "%date:~-10%" > c:\files.txt

The file files.txt can be found in the root folder of the hard drive ie, C:\. Open it in Notepad to view the list.

9. Here's a one-line batch file which will find ALL files created on a certain day

    @dir /s /a:-d c:\ | find "%1"

Name the batch files LOCATE and to find all files created say on 5th june 2007 enter the following:

    LOCATE 05/06/2007

10. Find ALL files created on a specific date and write list to a specific file (single line batch file)

    @if "%2"=="" (dir /s /a:-d c:\ | find "%1") else (dir /s /a:-d c:\ | find "%1" > %2)

Name the above batch file LOCATE.BAT then you can enter either one of the two commands:

    LOCATE 05/06/2007

    or

    LOCATE 05/06/2007 ListOfFiles.txt

So, you either get an output to the screen OR you get the list of files written to a file, in this case ListOfFiles.txt

Neat eh?

0
 
LVL 16

Accepted Solution

by:
t0t0 earned 500 total points
ID: 22772857
To find the existence of a file in a particular folder.

Suppose the file is named FILE.TXT and the folder you want to search is C:\TEMP. This following will confirm it's existence:

@echo off
if exist c:\temp\file.txt (
   echo File found
) else (
   echo File not found
)

Can't be much simpler than that, eh?

Finally, you can do something like this - enter the following command and tell the command what to look for and where:

    LOCATE myfile c:\temp

Here's the batch file to make that work

@echo off
if exist %2\%1 (
   echo File found
) else (
   echo File not found
)
0
 

Author Closing Comment

by:justpg
ID: 31504444
Thank you.  This is what I was after.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 22780819
Thank you for accepting my solution to your problem.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

717 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