Jump to content

help to IF NOT EXIST batch command


Recommended Posts

It does not delete, I just posted to show how it would be done in VBS and left the delete out.


Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")

If Not Fso.FileExists("Text1.txt") And _
Not Fso.FileExists("Text2.txt") Then
'-> Code Here To Delete Or Do Something
MsgBox "Delete Text3.txt"
Else
MsgBox "Text1 or Text2 Exists"
End If

Example VBS Delete to be added to above code


Fso.DeleteFile("FILE_PATH\FILE_NAME"),True

Link to comment
Share on other sites


PROBLEMCHYLD, are you suggesting that you do not wish to delete FILE.THREE if neither file exits, only if just one of them doesn't?

Yes one or the other. If neither file exist then delete file3. If one or the other file exist, leave file3 alone. Im half sleep. Sorry bout that.
Link to comment
Share on other sites

I'm still not sure what you want!

Correct this:

FILE.ONE exists + FILE.TWO exists = Keep FILE.THREE

FILE.ONE exists + FILE.TWO absent = Delete FILE.THREE

FILE.ONE absent + FILE.TWO exists = Delete FILE.THREE

FILE.ONE absent + FILE.TWO absent = Keep FILE.THREE

Link to comment
Share on other sites

I'm still not sure what you want!

Correct this:

FILE.ONE exists + FILE.TWO exists = Keep FILE.THREE

FILE.ONE exists + FILE.TWO absent = Delete FILE.THREE

FILE.ONE absent + FILE.TWO exists = Delete FILE.THREE

FILE.ONE absent + FILE.TWO absent = Keep FILE.THREE

IF NOT EXIST FILE.ONE OR FILE.TWO DEL FILE.THREE

IF EXIST FILE.ONE OR FILE.TWO DON'T DELETE FILE.THREE

Remeber the (OR) because either file can be present. It doesn't need both files, just one or the other.

Link to comment
Share on other sites

So like this?


IF EXIST FILE.ONE (GOTO :NEXT)
IF EXIST FILE.TWO (GOTO :NEXT)
DEL FILE.THREE
:NEXT

or


IF NOT EXIST FILE.ONE (IF NOT EXIST FILE.TWO (DEL FILE.THREE))

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

Hmmm I don't see how. Could you please post the code you are using?

Cheers and Regards

@ECHO OFF
IF NOT EXIST %WINDIR%\SYSTEM\NSCIRDA.SYS DEL %WINDIR%\SYSTEM\IRFTP.EXE
IF NOT EXIST %WINDIR%\SYSTEM\SMCIRDA.SYS DEL %WINDIR%\SYSTEM\IRFTP.EXE
CLS

This way is wrong because it deletes IRFTP.EXE before it checks for the next file.

Edited by PROBLEMCHYLD
Link to comment
Share on other sites

So do either of these work?

@ECHO OFF
IF EXIST %WINDIR%\SYSTEM\NSCIRDA.SYS (GOTO :NEXT)
IF EXIST %WINDIR%\SYSTEM\SMCIRDA.SYS (GOTO :NEXT)
DEL %WINDIR%\SYSTEM\IRFTP.EXE
:NEXT
CLS

@ECHO OFF
IF NOT EXIST %WINDIR%\SYSTEM\NSCIRDA.SYS (IF NOT EXIST %WINDIR%\SYSTEM\SMCIRDA.SYS DEL %WINDIR%\SYSTEM\IRFTP.EXE)
CLS

Cheers and Regards

Edited by bphlpt
Link to comment
Share on other sites

So do either of these work?

@ECHO OFF
IF EXIST %WINDIR%\SYSTEM\NSCIRDA.SYS (GOTO :NEXT)
IF EXIST %WINDIR%\SYSTEM\SMCIRDA.SYS (GOTO :NEXT)
DEL %WINDIR%\SYSTEM\IRFTP.EXE
:NEXT
CLS

@ECHO OFF
IF NOT EXIST %WINDIR%\SYSTEM\NSCIRDA.SYS (IF NOT EXIST %WINDIR%\SYSTEM\SMCIRDA.SYS DEL %WINDIR%\SYSTEM\IRFTP.EXE)
CLS

Cheers and Regards

No, they don't work. they keep deleting FILE.THREE/IRFTP.EXE

I'm on using Win98 BTW.

Edited by PROBLEMCHYLD
Link to comment
Share on other sites

So do either of these work?

@ECHO OFF
IF EXIST %WINDIR%\SYSTEM\NSCIRDA.SYS (GOTO :NEXT)
IF EXIST %WINDIR%\SYSTEM\SMCIRDA.SYS (GOTO :NEXT)
DEL %WINDIR%\SYSTEM\IRFTP.EXE
:NEXT
CLS

Yes, this works but not with the original Win98 command.com. It works when I run it in cmd.exe, which is win95cmd.exe renamed. Thanks and sorry for wasting your time.
Link to comment
Share on other sites

If spaces are involved anywhere in the path, then these should work with cmd.exe:

@ECHO OFF
IF EXIST "%WINDIR%\SYSTEM\NSCIRDA.SYS" (GOTO :NEXT)
IF EXIST "%WINDIR%\SYSTEM\SMCIRDA.SYS" (GOTO :NEXT)
DEL "%WINDIR%\SYSTEM\IRFTP.EXE"
:NEXT
CLS

@ECHO OFF
IF NOT EXIST "%WINDIR%\SYSTEM\NSCIRDA.SYS" (IF NOT EXIST "%WINDIR%\SYSTEM\SMCIRDA.SYS" DEL "%WINDIR%\SYSTEM\IRFTP.EXE")
CLS

If you really needed to use command.com, then the commands above should still work if you used the 8.3 version of the paths (without the quotes).

As to command.com vs cmd.exe, a couple of the main differences in support of using cmd.exe in most cases are:

command.com is 16-bit while cmd.exe is 32-bit

command.com can not handle long file names, spaces in the names, or quotes around the names

cmd.exe is available in 64-bit versions of XP+

Cheers and Regards

Link to comment
Share on other sites

First, as previously mentioned, always use the quotes!

Second, using global variables are nice and all, but sometimes are overkill. They make sense when designing a batch file to be ported among several systems, something that most people don't do.

The problem is that variables are easily changed by people, other batch files or by malware. Infamously it was often done by using '=' ( assignment ) rather than '==' ( test if equal ). The re-assignment would then cause all manner of havoc. I usually suggest hardcoding paths to save headaches, especially for core files in C:\Windows\System ( Win9x ) or C:\Windows\System32 ( WinXP+ ). Earlier NT and 2K might have been in \WinNT but unless running on an older one is a real possibility, avoid it. Additionally, IMHO I would always avoid RD or Del or similar destructive commands using easily changed variables.

In your batch file do a sanity check ...

echo.

echo WinDir is set to %WinDir%

echo.

This will tell you what path is being fed to you and if it contains a trailing backslash already.

An overall safer way might be using a local variable in the batch file perhaps called Target. To delete it ...

set Target="C:\Example File Path\Filename.exe"

if not exist %Target% echo File does not exist: %Target%

if exist %Target% del %Target%

Also, someone above used this example...

IF EXIST "%WINDIR%\SYSTEM\NSCIRDA.SYS" (GOTO :NEXT)

... which should be ...

IF EXIST "%WINDIR%\SYSTEM\NSCIRDA.SYS" GOTO NEXT

... because the colon does not belong there. Or better ...

set Target="C:\Windows\System\Nscirda.sys"

if exist %Target% goto NEXT

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...