If Exist Command

cebu1014 used Ask the Experts™
I want to move a file or files from, for example, c:\program\data to c:\program\bu
if the fiiles  sat1xx.zip, or sat2xx.zip, or sat0xx.zip are residing in c:\program\data folder.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


   move c:\program\data\sat???.zip c:\program\bu

will work, but that will generate an error code and message if there are no files. So...

  if exist c:\program\data\sat???.zip move c:\program\data\sat???.zip c:\program\bu

However, if you want to restrict it to sat1xx.zip, sat2xx.zip and sat0xx.zip:

  for %c in (0 1 2) do if exist c:\program\data\sat%c??.zip move c:\program\data\sat%c??.zip c:\program\bu

In addition, if you put that last command in a batch script (rather than the command line), you need to double up the percent signs:

  for %%c in (0 1 2) do if exist c:\program\data\sat%%c??.zip move c:\program\data\sat%%c??.zip c:\program\bu

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

I assume the files you want to check for are not the ones to move, or not the only ones. Since you need a "any files exists" criterium, a simple approach is this (in batch file):

@echo off
pushd c:\program\data
dir sat0??.zip sat1??.zip sat2??.zip /b >nul 2>nul && (
   move file1 ..\bu
   move file2 ..\bu


What does the c mean in sat%%c??  ?


Is the for loop variable name. When the command is executed, the numbers 0, 1 and 2 are substituted.

To see what it does, you could try (on the command line, not a batch script):

for %c in (1 2 10 20) do echo %c

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial