How to verify if a file exists in a DOS (Windows Command Prompt) .BAT file?

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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

  1. If C:\myprogram\sync\data.handler exists, exit;
  2. If C:\myprogram\html\data.sql does not exist, exit;
  3. In C:\myprogram\sync\ delete all files and folders except (test, test3 and test2)
  4. Copy C:\myprogram\html\data.sql to C:\myprogram\sync\
  5. Call other batch file with option 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.

回答1:

You can use IF EXIST to check for a file:

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

If you want to look for a drive or directory, see http://support.microsoft.com/kb/65994 for examples



回答2:

Type IF /? to get help about if, it clearly explains how to use 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

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

MYOTHERBATFILE.BAT sync.bat myprogram.ini 


回答3:

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 XCOPY command:

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 /y means:

  /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.

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.

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!