kr094
kr094

Reputation: 139

VBScript Shell - Differences between Windows CMD

I'm using a Wscript.Shell object in VBScript to control a hidden shell.

I'd like to delete some test files before the app starts, so I looked into an MSDOS command to conditionally delete files.

if exist name del name

This works fine in CMD, and does not give a warning about the file not existing. In VBScript shell however, this will generate the file not exists warning as if if exist wasnt part of the command.

This is especially annoying as in VBScript shell errors are displayed via MsgBox and block the app from running.

Upvotes: 0

Views: 705

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

del is an intrinsic/internal command, it's provided by the shell (cmd.exe), not by an executable named del.exe/com/.... So it 'works' from a shell/dos box/command line window.

The .Run (as the .Exec) method of the WScript.Shell object starts a process (an executable), not the shell. So del (and dir, and other intrinsics, and I/O redirection, and 'batch language') aren't available, unless you start the %comspec% executable and ask that to do what you want.

So: If something like

if exist deleteme.tmp del deleteme.tmp

works from a command line window,

%comspec% /c if exist deleteme.tmp del deleteme.tmp

will work as first parameter to .Run or .Exec.

Upvotes: 2

Bond
Bond

Reputation: 16311

With CreateObject("Scripting.FileSystemObject")
    If .FileExists(name) Then .DeleteFile name
End With

Upvotes: 1

Related Questions