vbscript not able to delete file

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
briandr
Posts: 9
Last visit: Tue May 22, 2012 10:55 am

vbscript not able to delete file

Post by briandr »

Hi,This script is not deleting the file it is supposed to. Any ideas?const Hidden = 0
Dim filesys
const WaitOnReturn = true
set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set filesys = CreateObject("Scripting.FileSystemObject")

If filesys.FileExists("C:WindowsSystem32dmMailServices.dll") Then
filesys.DeleteFile "C:WindowsSystem32dmMailServices.dll"
End If

objNetwork.MapNetworkDrive "B:", "cmhwaltpssoftware"
objShell.CurrentDirectory = "B:Onbase"
filesys.CopyFile "B:OnbasedmMailServices.dll", "C:Windowssystem32dmMailServices.dll"
objShell.CurrentDirectory = "C:"
objNetwork.RemoveNetworkDrive "B:" If I change the file to be deleted to test.txt then it deletes it. I don't believe the dll is registered, but my thought was can I incorporate something into the script that it would allow the file to be deleted using 'Run As'? If that is possible I just need some help setting this up.Thanks.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

vbscript not able to delete file

Post by Alexander Riedel »

Moved to correct forum by moderator.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

vbscript not able to delete file

Post by jvierra »

You are trying to delete a system file. System files can be protected by System File Protection. These files cannot be deleted by any normal mechanism. To replace one of these files you must use a special installer.

If the file is in use it cannot be deleted.
If the file is hung in memory due to an exception it cannot be deleted.

Some malware can make a file undeleteable when it becomes infected.

Most of your code is unnecessary. If teh file can be repalced you only need two lines to do it.

Code: Select all

source = "cmhwaltpssoftwaredmMailServices.dll"
destination = "C:Windowssystem32"
Set fso= CreateObject("Scripting.FileSystemObject") 
fso.CopyFile sourceFile, target, True

The bit in red will force an overwrite if the file is set as read-only. You do not need to delete teh file first. You do not need to map a drive or use the shell to flip diretories around. Just perform the task.

The same can be done at a command prompt

COPY cmhwaltpssoftwaredmMailServices.dll C:Windowssystem32
User avatar
briandr
Posts: 9
Last visit: Tue May 22, 2012 10:55 am

vbscript not able to delete file

Post by briandr »

Hi,Not sure what function the file performs. I was given the file to copy down to computers that did not have or had an older version of it. Had I been given an MSI that would have done this for me then I would not have landed here. Either way sorry for posting in the wrong forum and thanks for the help. I don't know VB too well as you saw with the extra lines of code. Are you saying all I need is the four lines above? If yes, then I am getting a 'Invalid procedure call or argument'. I would think I would need some more. I will try to piece this together, but any help you can offer is much appreciated. Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

vbscript not able to delete file

Post by jvierra »

This is what I am trying with now. I tried to clean it up the best I could. There may be some lines of code that are not necessary. const Hidden = 0const WaitOnReturn = trueSet fso=CreateObject("Scripting.FileSystemObject")Set objNetwork = WScript.CreateObject("WScript.Network")set objShell = CreateObject("WScript.Shell")source = "cmhwaltpssoftwaredmMailServices.dll"fso.CopyFile sourceFile, target, TrueI need to make sure the script can map the drive. Can I incorporate a Run As line so I could connect to the share and copy the file down. I hope that makes sense.....Sorry if bad explanation was given.



You do not need to map a drive. Just copy the file.

If you cannot copy the file from the command line without error than you cannot copy the file. You can map a dozen drives and the file will not be able to be copied.

Files that are protected or are in use cannot be replaced by vbscript or an MSI unless the MSI knows how to shutdown the process that is using the file or is designed to replace a file under Windows System File Protection. You cannot get around this with VBS or any other script.

Try replacing the file from the command line with copy. If it does not work then you cannot replace the file with VBScript.

At an MSDOS prompt type:

COPY cmhwaltpssoftwaredmMailServices.dll C:Windowssystem32



jvierra2012-05-22 16:44:16
User avatar
briandr
Posts: 9
Last visit: Tue May 22, 2012 10:55 am

vbscript not able to delete file

Post by briandr »

This line:source = "cmhwaltpssoftwaredmMailServices.dll"Should be:source = "cmhwaltpssoftwareonbasedmMailServices.dll"If you step through my original code you see that I changed to the directorythat the files resides in.When I tried this:COPY cmhwaltpssoftwareonbasedmMailServices.dll C:Windowssystem32I get:Prompted with (Yes/No/All:)I take this as a good sign that the file can be copied down.Going back to the vbscript I try again. Still errors out on me. I notice 'source' and 'sourcefile'. I assume it is supposed to be this way. Just to play with this in an effort to fix it, I change'sourceFile' to 'source'. I then re-run and get permission denied.I tried one more test before calling it a night.
If this line:
target = "c:windowssystem32"
Becomes this line:
target = "c:"
Then the file is able to be copied. Keep in mind two things. One, the
application that uses this file is not running. I don't even know if its
a desktop or web based application. Second, the file itself is not read
only. I just think the script is having issues copying to
c:windowssystem32 when run through a vbscript. It can work outsideof vbscript. Not sure if a batch file will do this for me, but it should not be difficult to copy to c:windowssystem32.

briandr2012-05-22 17:45:35
User avatar
briandr
Posts: 9
Last visit: Tue May 22, 2012 10:55 am

vbscript not able to delete file

Post by briandr »

Copy from command prompt:C:>COPY cmhwaltpssoftwareonbasedmMailServices.dll C:Windowssystem32Overwrite C:Windowssystem32dmMailServices.dll? (Yes/No/All): A 1 file(s) copied.C:>Line 5, Character 1, Permission denied error, 800A0046If we assume for a second the file is not in use and I believe it not be, at least on my computer. On other computers it may or may not be which presents other problems. Can't we de-register the file with Regsrv32 and then try to copy it?This is looking more like getting a manual process unless other ideas could work. At a minimum I need to at least delete the old version of the file on computers that already have it. The ones that need it now and never had it should not be as a big of an issue I hope.

briandr2012-05-22 18:08:52
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

vbscript not able to delete file

Post by jvierra »

De-registering a file will only potentially crash a system if that file is actually in use.

What makes you think it is a registered file?

So you are saying that this exact code fails with access denied to the exact same file and local system? YOuare running it from teh exact same prompt that ran the copy command? YOU ran teh copy then immeditely ran the vbs file and the VBS failed with that exact error.

Code: Select all

Set fso=CreateObject("Scripting.FileSystemObject")
source="cmhwaltpssoftwareonbasedmMailServices.dll"
target="c:windowssystem32"
fso.CopyFile source, target, True

Be sure that you use the above code because it matches the copy command.

Remember that it is your responsibility to be sure the file and path names are exactly correct. I cannot see your system so I cannot know if you are making mistakes.




You are running this by typing the name of the VBS at a command prompt on the target system?
This topic is 11 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked