# check existence of file or folder in batch



## jerardfjay (May 19, 2005)

Hello,

I am kind of new to this batch scripting and was wondering how one could check the existence of a file or folder in a batch program. I am using XP. 
Any help or suggesstions is greatly appreciated. Thanks.

Jerardfjay


----------



## ReeKorl (Mar 25, 2005)

Hi Jerardfjay and welcome to TSF!

If you're making a dos style batch file, the info you're looking for should be this:


```
IF [NOT] EXIST filename command ELSE command
```
EG:


```
IF EXIST hello.txt (
DEL hello.txt
) ELSE (
ECHO hello.txt is missing
)
```
the 'NOT' is optional, as is the 'ELSE' section

More information can be found by typing *HELP IF* at the command prompt.


----------



## jerardfjay (May 19, 2005)

ReeKorl said:


> More information can be found by typing *HELP IF* at the command prompt.


Thank you ReeKorl. 

I tried a simple script and it does work. Extending this capability, If I may ask. what if I wanted to check either the directory or file whose value is stored in a variable called src_dir or src_file, how would I accomplish that?

Please advise. Thanks.


----------



## ReeKorl (Mar 25, 2005)

OK, this is slightly more difficult. Variables are passed into a batch file as command line parameters. An example of this would be the following:


```
Code for making a 'new' copy batch file - Filename: c:\copier.bat

@ECHO OFF
COPY %1 %2
```
This would be used as follows:

*copier testfile.txt c:\folder*

This would, in the same way as copy does, copy the file from c:\ to c:\folder\ by calling copy to do it. Not very helpful yet, but you can use this a bit better with a nested batch file.

File 1: test1.bat

```
@ECHO OFF
CALL test2.bat file.txt
ECHO.
ECHO The code here has now run
```
File2: test2.bat

```
@ECHO OFF
IF EXIST %1 (
DEL %1
) ELSE (
ECHO.
ECHO File does not exist
ECHO.
)
```
This would check to see if the file stored in batch file 1 exists, then if it does, deletes it, otherwise lets you know. After that, it returns to the first batch file where it left off. If you left out the CALL command it would go and run the second file then immidiately terminate without returning. 

If you know BASIC, it equates to the CALL command is being a GOSUB with the end of the next file being a RETURN, whereas just the filename is a GOTO.

Hope this answers the question. If not, ask again and I'll try to solve it a bit better.

-EDIT

aah, it's been a while since I coded up a batch file. I must do this more often :grin:


----------



## jerardfjay (May 19, 2005)

ReeKorl said:


> Hope this answers the question. If not, ask again and I'll try to solve it a bit better.
> 
> -EDIT
> 
> aah, it's been a while since I coded up a batch file. I must do this more often :grin:


Thanks ReeKorl. This is infact a quite resourceful in trying to check the existence of a file. Here is my requirement withouth trying to make this more difficult than it already is.

I have a batch script which prompts user by a series of "set /p" command in XP as follows

*set /p src_dir="Enter the absolute path for Source Directory where Files are present:"*
and

*set /p src_file="Enter the name of the compressed file:"*

After these commands are executed I need to check to see if %src_dir% and %src_file% are existing in the reply values provided back from the user. 
Based on the conditions evaluation I need to branch out and either prompt for the user again or continue on with other work. Any ideas. Thanks again for keeping this alive. Jerardfjay :4-dontkno


----------



## ReeKorl (Mar 25, 2005)

OK, here goes:

@ECHO OFF
:start_of_file
SET /P src_dir="Enter the absolute path for Source Directory where Files are present *(including the final \ )*:"
SET /P src_file="Enter the name of the compressed file:"
ECHO.
ECHO.
ECHO The file you are looking for is %src_dir%%src_file%
IF EXIST %src_dir%%src_file% (
GOTO file_found
) ELSE (
GOTO file_not_found)
:file_found
*REM 
REM Do what you want in this section if file is found
REM Perhaps a CALL to another batch?
REM ie. CALL batch_file.bat %src_dir% %src_file%
REM then in the other file, it takes the two inputs as %1 and %2
REM*
ECHO File HAS been found
GOTO end_of_file
:file_not_found
*REM
REM Do what you want in this section if file is not found
REM Same as previous section
REM*
ECHO File HAS NOT been found
GOTO end_of_file
:end_of_file

This will allow what you are attempting. I suggest you just put a CALL to another batch file in each of the sections where you do stuff as it will allow easier bug testing and make things much less messy.

ALSO, take note of the bolded section in the first input - you need the final \ in the path or the syntax for the file will be something like this:

C:\folderfile.txt

instead of 

C:\folder*\*file.txt

Hope this helps!


----------



## jerardfjay (May 19, 2005)

ReeKorl said:


> Hope this helps!


Thank you much ReeKorl. It works fine for me. Hope you dont mind another question. I am from the NIXES world. I have a zip file created by PKZIP command, which is transferred over to windows system. 
Can the compact command with a /U option uncompress the zip file created in UNIX? If not what native command / windows utility (not a 3rd party software) can I use to uncompress the zip file created by PKZIP. Any suggesstions. Thanks. :smile: Jerardfjay


----------



## ReeKorl (Mar 25, 2005)

Erm, I have very limited knowledge of Unix/Linux (haven't got around to trying it yet... soon though!) but I _think_ it can be opened. I'm pretty sure a tarball can't be opened using pkzip, but if it's been created as a regular zip file (possibly evan as a gz) then it should open it.

Best option is to try it i guess!

Glad to have helped :grin:


----------

