• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

VBS Delete with String

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Bijiont

Member
Joined
Jul 19, 2010
Location
Michigan
Hello Everyone,

I am finally calling uncle and looking for some help on what should be a simple VB script.

What I am trying to do is delete a file from a certain path based on the user profile.

Here is what I have so far, I have removed information which I can't disclose per say. . .

Code:
Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell")
Dim oShell : Set oShell = CreateObject("WScript.Shell")
Dim obj : Set obj = CreateObject("Scripting.FileSystemObject")
Dim strUserName : strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
WshShell = Nothing

'Kills task to allow file delete'
oShell.Run "TASKKILL /IM tasktobekilled.exe", , True
'Sleeps script to allow for task to end'
WScript.sleep 3000
'Deletes the DB file which holds the records'
obj.DeleteFile("C:\Users\" & strUserName & "\remainderpathtodbfile.com.db")
oShell.Run("""C:\Program Files\remainderpathtoapp.exe""")

That last few times I have been getting Permission denied errors however everything which is targeted can be executed at the permission level.

Before that I was getting ExpandEnviromentStrings errors where it wouldn't produce the %USERNAME% variable as it should.

Long story short. . . help :shrug:
 
Works fine for me... I changed it to kill notepad.exe and to remove a testfile that I created, no issues.
 
Very strange. . . now I am starting to wonder if this is in fact permissions based.

I will re-run the code a few times just in the event I am missing something.

Thanks,
 
If whatever file you're trying to delete was installed with administrative rights, it's possible that is causing your issue. To test it out, just create a dummy txt file somewhere as yourself and attempt to delete it in your script instead.

If that is what's causing your issues, I'm sure there's a way to elevate the privileges of the shell you're creating in VBS, which should solve your issues.
 
Thanks pcgamer4life, I did a little testing and found that the wait I placed wasn't enough to allow all the services to stop for that program I killed. I placed in a longer wait and it works perfect now.
 
Makes sense, I hadn't even thought of that. Instead of having an arbitrary wait time that could cause issues if the machine is running slow or the process gets hung up while dying, maybe add a while loop that checks to see if your process is still running instead. Once it finds that the process isn't running, then delete your file.
 
Back