216

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.

Synetech
  • 9,643
  • 9
  • 64
  • 96
cusspvz
  • 5,143
  • 7
  • 30
  • 45

3 Answers3

337

You can use IF EXIST to check for a file:

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
)
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 1
    How does one check full path with filename? Bonus points if the path contains spaces. Like OP said, simple in BASH. – Nick Dec 24 '13 at 23:10
  • 3
    @Nick: Simple in `cmd` too - please ask as a different question - they don't cost much. Adding a further question comment to one that is more than 3 years old isn't likely to garner many responses (but check SO first for answers to this precise question, else you'll get your new question marked as a duplicate...) – Magoo Jan 12 '14 at 23:30
  • 12
    Just something to note from the `IF /?` help file: `The ELSE clause must occur on the same line as the command after the IF.` This burned me. hope it helps you. – funkymushroom Feb 27 '14 at 17:58
  • From an edit - _"Also note that EXIST does return true on words reserved for devices, like NUL, CON, AUX, PRN for example, even though one wouldn't technically consider them filenames."_ – stuartd Jun 10 '16 at 14:38
  • 2
    Reminder: IF, EXIST, ELSE, REM, DEL, etc. all work in lowercase as well! – Terra Ashley Jul 19 '16 at 23:57
  • @Nick path containing spaces needs to be put in variables, then use them like: `SET PathHavingSpace="file path" SET SolutionDir=%PathHavingSpace%` – cyber_raj Sep 02 '17 at 15:30
  • This doesn't work for me. I follow strictly as answer suggests. – TiredOfProgramming Jul 17 '18 at 15:18
  • @TiredOfProgramming sorry to hear that. Perhaps you should ask a new question, showing what you have tried? – stuartd Jul 17 '18 at 21:55
  • @stuartd for me if I create a simple batch file test.bat and put the code that was provided in the answer, the console simply outputs the entire code back without actual verification if the file exists or not..... – TiredOfProgramming Jul 18 '18 at 13:14
  • @TiredOfProgramming I expanded the answer with a working example. – stuartd Jul 18 '18 at 14:12
  • 2
    to check if file not exist, use `If Not Exist "%FilePath% ( command )`. Note that, bat uses braces `(` instead of curly braces `{` – Sang May 05 '20 at 04:01
  • 1
    @funkymushroom You are still the real MVP in 2023. Thanks for that helpful insight and reading the manual. I was banging my head. – alexGIS Jun 21 '23 at 19:00
15

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:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Avrumi Sherman
  • 341
  • 2
  • 12
13

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
Community
  • 1
  • 1
Patrick
  • 23,217
  • 12
  • 67
  • 130