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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF EXIST delete file

Status
Not open for further replies.

rozzay

Programmer
Jan 3, 2002
142
US
Hi I am trying to do an FTP dos command where I have the login doing okay. I am able to delete the files but if the files are not located in the webserver the .bat file keeps going. I would like the .bat file to stop if the file is not found in the webserver.
Also I am unable to do a wildcard(*) for the files so I have my program to list the files instead which can be long and tedious, does anyone know of a way? Below is what I have so far and it does not seem to work with the 'if exist'.
Thanks in advance!!

ftp -s:%0
GoTo done
open webserver.com
userid
password
cd webedi

if exist 0114891880.html del 0114891880.html
if exist 0114891880.htm del 0114891880.htm
if exist 0114901820.html del 0114901820.html
if exist 0114901820.htm del 011901820.htm

goto end
bye
 
Okay I figured how to delete them the files but how would I do wildcards(*). Instead of listing them all. This is what I have so far. Thanks in advance.

ftp -s:%0
GoTo done
open server.com
userid
password
cd webedi

if exist 0114891880.html goto delete
:delete
del 0114891880.html
goto end
bye
 
I really don't see why you would need to test the file existing. If you just DEL 011489.html it will delete it if found. Even for instance DEL 0*.HTM* will work.
 
If you want this program to run without any user intervention, you will need to turn the prompt function off.

open server.com
user
pass
prompt
delete 0*.HTM*

Hope this helps!
 
[ul]
but if the files are not located in the webserver the .bat file keeps going. I would like the .bat file to stop if the file is not found in the webserver.
[/ul]

Instead of
if exist 0114891880.html goto delete

Use
if not exist 0114891880.html endit

where
ENDIT.BAT is created by ECHO >endit.bat

This will transfer to another BAT file which stops the previous one. Endit will do nothing and stop.
 
The .bat file should not keep going in a loop unless you are calling the .bat file from within itself.

The .bat file will just exit whether or not it has completed its tasks.

Just set this up as a single bat file, and use some sort of a scheduler to run it on a regular basis.


Hope this helps!
 
The line
if not exist 0114891880.html endit

will start endit.bat, but the current bat file will continue.
if you want to pause the current bat file and wait for endit.bat to terminate (and return a value) use "call":
if not exist 0114891880.html call endit.bat

There's a very useful command line tool I've found called LMOD (LMOD 1.0f (c) 1999 Horst Schaeffer, works on stdin/out) which I've used in batch files to create other batch files (which can call other batch files...)
e.g.

open server.com
user
pass
prompt
dir 0*.htm? /b |lmod /l* if not exist [] goto end [+] del [] >delhtml.bat
:: the /L* and the brackets are tokens for lmod
echo :end>>delhtml.bat
call delhtml.bat

This would create an ancillary bat file called delhtml.bat which would look like this:
if not exist 0114891880.html goto end
del 0114891880.html
if not exist 0114891880.htm goto end
del 0114891880.htm
if not exist 0114901820.html goto end
del 0114901820.html
if not exist 0114901820.htm goto end
del 011901820.htm
:end

This way, you don't have to list the files to del manually, you can apply the wildcards to the dir command to list all the files to delete, and as soon as it hits a file that doesn't exist, it would terminate. Except that it won't come to that, since the file existed in the dir to begin with.
 
Chewwie,

"will start endit.bat, but the current bat file will continue.:

Negative. The current level that invokes Endit without a CALL ends as well.

ENDIT.BAT most definitely stops the current level on 95/98 and I recall ME. Perhaps it does not on 2kxpnt.

To keep the main procedure going one has to use CALL explicitly, of course.

Try this

endit
echo test


----

Rozzay, as Marcs41 asked, what are the selection crteria, or why not just
del 0*.html
?





 
Chewwie,

"will start endit.bat, but the current bat file will continue."

Negative.

ENDIT.BAT most definitely stops the current level on 95/98 and I recall ME. Perhaps it does not on 2kxpnt.


Try this:

endit
echo test

No 'test' comes out.
----

Rozzay, as Marcs41 asked, what are the selection crteria, or why not just
del 0*.html
?





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top