Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

does "if exist" exist? 3

Status
Not open for further replies.

realtree

Technical User
Aug 5, 2003
53
CA
i'm trying to write a batch file to install some programs.
i want to use
Code:
if not exist "C:\Program Files" goto changedir
it works fine, but it needs to be compatible with all windows machines. (I already tried using %ProgramFiles%, but I found that win98 doesn't have this as a default) I tried
Code:
if exist "C:\Program Files" echo yea
and
Code:
if exist C:\Progra~1 echo yea
but it still don't echo back! (and I DO know that it IS a valid directory). I do know a little bit of pascal, but I don't have it at work, and I truly only know a little bit right now.

also: True or False? For the most part,(as far as batch scripting goes) if it works on win98 and NT4, it'll work on them all.
 
Have you tried

if exist C:\Progr* echo yea
 
Correct me if I'm wrong, but "exist" works for files, not for folders in 98. In XP, it does behave differently.

I created the following batch file:

Code:
echo off
if exist c:\progra~1 echo folder ~1 is there
if exist c:\progra~1\desktop.ini echo file exists in ~1
if exist "c:\program files" echo long name works for folder
if exist "c:\program files\desktop.ini" echo long name works for files
if exist "c:\progra~1\." echo dot is there
if exist "c:\progra~1\.." echo dot dot is there

Here's the result in 98SE:
file exists in ~1
long name works for files

Here's the result in XP Home
folder ~1 is there
long name works for folder
dot is there
dot dot is there

Bottom line is, the operating system you're using does not allow "exist" to operate on folders, it only works with filename.

Hope this helps..



Cheers,

Realm174
 
Hi,

You can use the following trick to check for the existence of a folder in DOS/9x:

Code:
if exist "c:\foldername\nul" goto continue
md c:\foldername
:continue
rem copy files here

The nul device is an old trick basically checking for the equivalent of a bottomless pit under the folder.

John
 
Hey John! I forgot about that old trick... but you're right, it works!! Ahhh the good ol' DOS days : )

Cheers,



Cheers,

Realm174
 
Don't forget that your batch file is running is a DOS environment and will only recognize file/folder names of 8 characters or less.

There's always a better way. The fun is trying to find it!
 
I see... very nice! * can be used (duh) instead of nul. except if it's empty, it won't return. good way to test the emptyness of a folder tho.

So then "C:\Program Files\nul" should work in all windows versions.... wait I just tried it on an XP Pro box, doesn't work. Why does M$ have to mess around so much!?! I could use progra~1, but it's not fool proof. would be nice to use full names.

the final line as of now reads:
Code:
if not exist C:\Progra~1\nul goto changedir

I don't have access to all of the OS though. will this work in 95(sadly, some ppl still use it), 98, NT, ME, 2K, and XP?

btw, I was looking at pascal and trying to relate my DOS routines to the dos and standard units there. Too bad I don't get it. Oh well, 9 days til my C++ and VB courses start!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top