Go Back   Tech Support Forum > Microsoft Support > Windows XP Support

User Tag List

check existence of file or folder in batch

This is a discussion on check existence of file or folder in batch within the Windows XP Support forums, part of the Tech Support Forum category. Hello, I am kind of new to this batch scripting and was wondering how one could check the existence of


Closed Thread
 
Thread Tools Search this Thread
Old 05-20-2005, 05:14 AM   #1
 
Join Date: May 2005
Posts: 22
OS:



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 :-)
jerardfjay is offline  
Sponsored Links
Advertisement
 
Old 05-20-2005, 05:29 AM   #2
Superhuman Computer
 
ReeKorl's Avatar
 
Join Date: Mar 2005
Location: 6th Circle, The City of Dis, Hell
Posts: 1,632
OS: WinXP Pro SP2

My System

Hi Jerardfjay and welcome to TSF!

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

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

Code:
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.
__________________
"Sorry, the number you have dialled is imaginary. Please rotate dial by 90 degrees and try again."
ReeKorl is offline  
Old 05-20-2005, 07:19 AM   #3
 
Join Date: May 2005
Posts: 22
OS:


Quote:
Originally Posted by ReeKorl
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.
jerardfjay is offline  
Sponsored Links
Advertisement
 
Old 05-20-2005, 08:08 AM   #4
Superhuman Computer
 
ReeKorl's Avatar
 
Join Date: Mar 2005
Location: 6th Circle, The City of Dis, Hell
Posts: 1,632
OS: WinXP Pro SP2

My System

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:
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
Code:
@ECHO OFF
CALL test2.bat file.txt
ECHO.
ECHO The code here has now run
File2: test2.bat
Code:
@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
__________________
"Sorry, the number you have dialled is imaginary. Please rotate dial by 90 degrees and try again."
ReeKorl is offline  
Old 05-20-2005, 08:19 AM   #5
 
Join Date: May 2005
Posts: 22
OS:


Quote:
Originally Posted by ReeKorl
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
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
jerardfjay is offline  
Old 05-20-2005, 09:08 AM   #6
Superhuman Computer
 
ReeKorl's Avatar
 
Join Date: Mar 2005
Location: 6th Circle, The City of Dis, Hell
Posts: 1,632
OS: WinXP Pro SP2

My System

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!
__________________
"Sorry, the number you have dialled is imaginary. Please rotate dial by 90 degrees and try again."
ReeKorl is offline  
Old 05-20-2005, 10:56 AM   #7
 
Join Date: May 2005
Posts: 22
OS:


Quote:
Originally Posted by ReeKorl

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. Jerardfjay
jerardfjay is offline  
Old 05-21-2005, 02:39 AM   #8
Superhuman Computer
 
ReeKorl's Avatar
 
Join Date: Mar 2005
Location: 6th Circle, The City of Dis, Hell
Posts: 1,632
OS: WinXP Pro SP2

My System

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
__________________
"Sorry, the number you have dialled is imaginary. Please rotate dial by 90 degrees and try again."
ReeKorl is offline  
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is on
Smilies are on
[IMG] code is on
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Post a Question


» Site Navigation
 > FAQ
  > 10.0.0.2
Powered by vBadvanced CMPS v3.2.3


All times are GMT -7. The time now is 08:37 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
vBulletin Security provided by vBSecurity v2.2.2 (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.1.0 (Pro) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
Copyright 2001 - 2018, Tech Support Forum

Windows 10 - Windows 7 - Windows XP - Windows Vista - Trojan Removal - Spyware Removal - Virus Removal - Networking - Security - Top Web Hosts