<p>Vous pouvez utiliser IF EXIST to check for a file:</p>
<pre><code class="lang-auto">IF EXIST "filename" (
REM Do one thing
) ELSE (
REM Do another thing
)
</code></pre>
<p>If you do not need an “else”, vous pouvez do something like this:</p>
<pre><code class="lang-auto">set __myVariable=
IF EXIST "C:\folder with space\myfile.txt" set __myVariable=C:\folder with space\myfile.txt
IF EXIST "C:\some other folder with space\myfile.txt" set __myVariable=C:\some other folder with space\myfile.txt
set __myVariable=
</code></pre>
<p>Here’s a working example of searching for a file or a folder:</p>
<pre><code class="lang-auto">REM setup
echo "some text" > filename
mkdir "foldername"
REM finds file
REM "The ELSE clause must occur on the same line
as the command after the IF"
IF EXIST "filename" (
ECHO file filename exists
) ELSE (
ECHO file filename does not exist
)
REM does not find file
IF EXIST "filename2.txt" (
ECHO file filename2.txt exists
) ELSE (
ECHO file filename2.txt does not exist
)
REM folders must have a trailing backslash
REM finds folder
IF EXIST "foldername\" (
ECHO folder foldername exists
) ELSE (
ECHO folder foldername does not exist
)
REM does not find folder
IF EXIST "filename\" (
ECHO folder filename exists
) ELSE (
ECHO folder filename does not exist
)
</code></pre>