?
Solved

Searching for Existence of File Using DOS

Posted on 2008-10-08
12
Medium Priority
?
675 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

800 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