Page 1 of 1
IF File Exist
Posted: 10 Jul 2009 07:44
by supersoup
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
Re: IF File Exist
Posted: 10 Jul 2009 09:50
by RElliott63
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
Posted: 10 Jul 2009 10:17
by supersoup
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?
Posted: 10 Jul 2009 21:09
by avery_larry
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
)
)