@echo off
set filename=myfile.txt
if exist %filename% (
echo File exists.
) else (
echo File does not exist.
)
Explanation: - @echo off: This command turns off the display of each command in the batch file as it executes.
- set filename=myfile.txt: This sets the variable filename to the name of the file you want to check.
- if exist %filename%: This checks if the file specified by the variable filename exists.
- echo File exists.: If the file exists, this command is executed.
- else: If the file does not exist, the commands following else are executed.
- echo File does not exist.: This command is executed if the file does not exist.
You can replace myfile.txt with the path to the file you want to check. Save the above code in a file with a .bat extension (e.g., checkfile.bat) and run it. It will display whether the file exists or not. Tags: DOS echo off if exist
|