Solved

Searching for Existence of File Using DOS

Posted on 2008-10-08
12
633 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
Comment Utility
if exist YourFile.doc echo hi
0
 
LVL 3

Expert Comment

by:hyperlite
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:KevinNo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Expert Comment

by:t0t0
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you.  This is what I was after.
0
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
Thank you for accepting my solution to your problem.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
The use of stolen credentials is a hot commodity this year allowing threat actors to move laterally within the network in order to avoid breach detection.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now