如何在批处理文件中验证文件是否存在?

218

我需要创建一个 .BAT 文件,执行以下操作:

  1. 如果 C:\myprogram\sync\data.handler 存在,则退出;
  2. 如果 C:\myprogram\html\data.sql 不存在,则退出;
  3. C:\myprogram\sync\ 中删除除(testtest3test2)之外的所有文件和文件夹;
  4. C:\myprogram\html\data.sql 复制到 C:\myprogram\sync\
  5. 使用选项 sync.bat myprogram.ini 调用其他批处理文件。

如果它是在 Bash 环境中,对我来说很容易,但我不知道如何测试文件或文件夹是否存在以及它们是文件还是文件夹。

3个回答

341
你可以使用IF EXIST来检查文件是否存在。
IF EXIST "filename" (
  REM Do one thing
) ELSE (
  REM Do another thing
)

如果你不需要一个“else”,你可以这样做:
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=

这是一个搜索文件或文件夹的工作示例:
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
)

1
如何检查包含文件名的完整路径?如果路径中包含空格,则额外加分。像楼上所说,在BASH中很简单。 - Nick
3
@Nick:在“cmd”中也很简单 - 请提出一个不同的问题 - 它们并不需要太多成本。在超过3年的旧问题下添加进一步的问题评论可能不会得到太多回复(但首先检查SO以获取对这个精确问题的答案,否则你的新问题将被标记为重复...)。 - Magoo
12
从“IF /?”帮助文件中需要注意的一点是:“ELSE子句必须出现在IF命令之后的同一行。” 这曾经让我吃过亏,希望对你有所帮助。 - funkymushroom
2
提醒:IF、EXIST、ELSE、REM、DEL等命令也可以使用小写字母! - Terra Ashley
2
要检查文件是否不存在,请使用 If Not Exist "%FilePath% ( command )。请注意,bat 使用圆括号 ( 而不是花括号 { - transang
显示剩余8条评论

15
以下是如何根据文件是否存在来执行命令的好例子:
if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit

我们将把这三个文件放在临时位置。在删除文件夹后,它将恢复这三个文件。

xcopy "test" "C:\temp"
xcopy "test2" "C:\temp"
del C:\myprogram\sync\
xcopy "C:\temp" "test"
xcopy "C:\temp" "test2"
del "c:\temp"

使用 XCOPY 命令:
xcopy "C:\myprogram\html\data.sql"  /c /d /h /e /i /y  "C:\myprogram\sync\"

我会解释一下 /c /d /h /e /i /y 的含义:

  /C           Continues copying even if errors occur.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /H           Copies hidden and system files also.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.

`To see all the commands type`xcopy /? in cmd

使用选项sync.bat myprogram.ini调用其他批处理文件。

我不确定你的意思,但如果你只是想打开这两个文件,只需将文件路径放入其中即可,例如:

Path/sync.bat
Path/myprogram.ini

如果在Bash环境中,这对我来说很容易,但是我不知道如何测试文件或文件夹是否存在以及它们是文件还是文件夹。
您正在使用批处理文件。您之前提到过必须创建一个.bat文件来使用此功能:

13

输入IF /? 以获取关于 IF 的帮助,它清楚地解释了如何使用 IF EXIST。

要删除完整的树形目录,除了一些文件夹,请参见此问题的答案:Windows batch script to delete everything in a folder except one

最后,仅复制意味着调用COPY,而调用另一个批处理文件可以像这样完成:

MYOTHERBATFILE.BAT sync.bat myprogram.ini

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接