kingtreelo
kingtreelo

Reputation: 251

Batch file not deleting files when run through application

I have a batch file that removes all files in a certain directory. This works perfectly fine when I run it maually, but when running it from an application, it does not delete the files.

Batch file code:

cd "C:\Users\namehere\Desktop\Test" 
del *.* /q
taskkill /im Presenter.exe
START D:\PresenterInstallFolder\Presenter\Presenter.exe
exit

The batch file does fire off, as it loads in my application. As mentioned, when run manually, it works perfectly fine.

I'm using System.Diagnostics.Process.Start() to start the batch file

Upvotes: 0

Views: 45

Answers (1)

Leptonator
Leptonator

Reputation: 3499

While TASKLIST and TASKKILL work, you should be able to do a bit cleaner:

del /q "C:\Users\namehere\Desktop\Test\*.*" 
WMIC Path win32_process WHERE "CommandLine Like '%Presenter%'" CALL Terminate
START D:\PresenterInstallFolder\Presenter\Presenter.exe
exit

What about the following?

del /q "%userprofile%\Desktop\Test\*.*" 
WMIC Path win32_process WHERE "CommandLine Like '%Presenter%'" CALL Terminate
START D:\PresenterInstallFolder\Presenter\Presenter.exe
exit

It would also seem to me, you would want to terminate the application first, then delete the files:

WMIC Path win32_process WHERE "CommandLine Like '%Presenter%'" CALL Terminate
del /q "%userprofile%\Desktop\Test\*.*" 
START D:\PresenterInstallFolder\Presenter\Presenter.exe
exit

You may do well to add a Sleep 1 or 2 seconds to give the process time to stop or start as needed.

Other ways to start a program, you could do the following:

WMIC process  CALL Create "Notepad.exe"

Or, in your example:

wmic process call create "cmd /c start /min D:\PresenterInstallFolder\Presenter\Presenter.exe"

if you don't want to start a second instance of CMD:

wmic process call create "D:\PresenterInstallFolder\Presenter\Presenter.exe"

Then, you should be able to do something like:

CALL C:\Path\YourBatchfile.bat

Hope this helps!

Upvotes: 1

Related Questions