IF File Exist

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
supersoup
Posts: 5
Joined: 10 Jul 2009 07:35

IF File Exist

#1 Post by supersoup » 10 Jul 2009 07:44

I am brand new to batch files and dos commands and i would like to make a file that tells me if a file exists or not. This is what i have:

Code: Select all

echo off
if not exist "C:\Folder A\File Q.txt" goto ELSE
echo The file C:\Folder A\File Q.txt DOES exist. >> "IF Test Results.txt"
goto END
ELSE
echo The file C:\Folder A\File Q.txt DOES NOT exist. >> "IF Test Results.txt"
:END


This code works correctly when i test it on files that do exist, but does nothing when i test it on files that do not exits.

Please help

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

Re: IF File Exist

#2 Post by RElliott63 » 10 Jul 2009 09:50

supersoup wrote:

Code: Select all

echo off
if not exist "C:\Folder A\File Q.txt" goto ELSE
echo The file C:\Folder A\File Q.txt DOES exist. >> "IF Test Results.txt"
goto END
ELSE
echo The file C:\Folder A\File Q.txt DOES NOT exist. >> "IF Test Results.txt"
:END



Try something like this ...

Code: Select all

echo off
if not exist "C:\Folder A\File Q.txt" (
   echo File does NOT exist!
   echo The file "C:\Folder A\File Q.txt" DOES NOT exist.   >> "IF Test Results.txt"
) Else (
   echo File exists!
   echo The file "C:\Folder A\File Q.txt" DOES exist. >> "IF Test Results.txt"
)
:END

supersoup
Posts: 5
Joined: 10 Jul 2009 07:35

#3 Post by supersoup » 10 Jul 2009 10:17

That worked like a charm! Thanks

I have written another batch file that lists the contents in a particular folder:

Code: Select all

echo Contents of Folder A >> "Folder A Contents.txt"
dir "C:\Folder A" /s /b >> "Folder A Contents.txt"


How do i get the the previous code to see if files in "Folder A Contents.txt" exist in another folder?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 10 Jul 2009 21:09

Not sure I understand completely but . . .

(untested)

Code: Select all

for /f "skip=1 usebackq" %%a in ("Folder A Contents.txt") do (
   if exist "c:\other directory\%%~nxa" (
      echo Put your code here for what to do if the file exists
      ) else (
         echo Put your code here for what to do if the file doesn't exist
   )
)

Post Reply