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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3022176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:23:26  来源:igfitidea点击:

How to verify if a file exists in a batch file?

windowsbatch-filecmd

提问by cusspvz

I have to create a .BATfile that does this:

我必须创建一个.BAT执行此操作的文件:

  1. If C:\myprogram\sync\data.handlerexists, exit;
  2. If C:\myprogram\html\data.sqldoes not exist, exit;
  3. In C:\myprogram\sync\delete all files and folders except (test, test3and test2)
  4. Copy C:\myprogram\html\data.sqlto C:\myprogram\sync\
  5. Call other batch file with option sync.bat myprogram.ini.
  1. 如果C:\myprogram\sync\data.handler存在,退出;
  2. 如果C:\myprogram\html\data.sql不存在,则退出;
  3. C:\myprogram\sync\删除除 ( test,test3test2)之外的所有文件和文件夹
  4. 复制C:\myprogram\html\data.sqlC:\myprogram\sync\
  5. 使用选项调用其他批处理文件sync.bat myprogram.ini

If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.

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

回答by stuartd

You can use IF EXIST to check for a file:

您可以使用 IF EXIST 来检查文件:

IF EXIST "filename" (
  REM Do one thing
) ELSE (
  REM Do another thing
)

If you do not need an "else", you can do something like this:

如果您不需要“其他”,您可以执行以下操作:

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=

Here's a working example of searching for a file or a folder:

这是搜索文件或文件夹的工作示例:

REM setup

echo "some text" > filename
mkdir "foldername"

REM finds file    

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
)

回答by Patrick

Type IF /? to get help about if, it clearly explains how to use IF EXIST.

输入 IF /? 要获得有关 if 的帮助,它清楚地说明了如何使用 IF EXIST。

To delete a complete tree except some folders, see the answer of this question: Windows batch script to delete everything in a folder except one

要删除除某些文件夹之外的完整树,请参阅此问题的答案:Windows 批处理脚本以删除文件夹中除一个之外的所有内容

Finally copying just means calling COPY and calling another bat file can be done like this:

最后复制只是意味着调用 COPY 并调用另一个 bat 文件可以这样完成:

MYOTHERBATFILE.BAT sync.bat myprogram.ini

回答by Avrumi Sherman

Here is a good example on how to do a command if a file does or does not exist:

这是一个很好的示例,说明如果文件存在或不存在,如何执行命令:

if exist C:\myprogram\sync\data.handler echo Now Exiting && Exit
if not exist C:\myprogram\html\data.sql Exit

We will take those three files and put it in a temporary place. After deleting the folder, it will restore those three files.

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

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

Use the XCOPYcommand:

使用XCOPY命令:

xcopy "C:\myprogram\html\data.sql"  /c /d /h /e /i /y  "C:\myprogram\sync\"

I will explain what the /c /d /h /e /i /ymeans:

我将解释这/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

Call other batch file with option sync.bat myprogram.ini.

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

I am not sure what you mean by this, but if you just want to open both of these files you just put the path of the file like

我不确定你的意思,但如果你只想打开这两个文件,你只需把文件的路径像

Path/sync.bat
Path/myprogram.ini

If it was in the Bash environment it was easy for me, but I do not know how to test if a file or folder exists and if it is a file or folder.

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

You are using a batch file. You mentioned earlier you have to create a .bat file to use this:

您正在使用批处理文件。您之前提到您必须创建一个 .bat 文件才能使用它:

I have to create a .BAT file that does this:

我必须创建一个执行此操作的 .BAT 文件: