• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
ZEUS__

help to IF NOT EXIST batch command

34 posts in this topic

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
0

Share this post


Link to post
Share on other sites

What is the value of %WinDir%? Does it have spaces?

0

Share this post


Link to post
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.
0

Share this post


Link to post
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

0

Share this post


Link to post
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

0

Share this post


Link to post
Share on other sites

It seems all of the methods work under certain conditions. Windows 98 native COMMAND.COM didn't understand (GOTO :NEXT)

but understood GOTO NEXT. WIN95CMD.EXE file understands both. So it seems certain flags/pipes/variables etc..,.. are recognized by both, but the WIN95CMD.EXE seems to recognize most if not all. Thanks guys, its working smoothly.

0

Share this post


Link to post
Share on other sites

Why could you not simply correct the first word after the equals symbol in each of the four cases I provided!

I think that it may be you who is misunderstanding OR!

You now seem to only want FILE.THREE to be deleted if both of the other two files do not exist.

IF NOT EXIST "FILE.ONE" (SET _=BO)
IF NOT EXIST "FILE.TWO" (SET _=%_%TH)
IF %_%==BOTH DEL "FILE.THREE"

So in summary:

  • IF BOTH FILE.ONE AND FILE.TWO DO NOT EXIST
    use the above example
  • IF AT LEAST ONE OF FILE.ONE OR FILE.TWO DO NOT EXIST
    use either of the two examples I provided in my original post

0

Share this post


Link to post
Share on other sites

Yzöwl did you look at post 14 it was the VBS way of doing what PROBLEMCHYLD wanted done.

If you read it and it really simple if the 2 files are not there then delete file 3, I am Dyslexic and could

understand what PROBLEMCHYLD wanted.

Yzöwl cool code :thumbup

0

Share this post


Link to post
Share on other sites

What is the value of %WinDir%? Does it have spaces?

The Windows folder can't have spaces so you don't need quotes (unless you set the variables manually to something else but it's not the case here). Microsoft themselves usually don't use quotes when pointing to %windir% / %systemroot% in their patches.

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.

Newer Windows can also use WINNT, ex. when you upgraded your system from Windows 2000 to XP, later upgraded XP to Vista, and Vista to 7, etc.

Edited by tomasz86
0

Share this post


Link to post
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.