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
Solved

Searching for Existence of File Using DOS

Posted on 2008-10-08
12
663 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
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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

808 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