Solved

Searching for Existence of File Using DOS

Posted on 2008-10-08
12
657 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
multiple application databases same MSSQL instance 5 53
Windows NLB cluster 3 28
Problem to App source 6 35
Windows DNS Server Caching 3 28
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
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…
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…
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…

785 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