VBScript : howto delete a remote folder

Hi all,

I need to delete a folder on a remote computer. First of all, I'm in domain admins group.
I can do all operation manually.
I've got a simple script that should delete those remote folder.
I've made some test and I can delete a folder on the D:\ drive for exemple but I can't delete folder stored in Documents and settings. I've also try the short display "Docume~1" but it also failed.

I've try 2 differents syntax for the foldername :

sFolderPath = """\\" & strComputer & "\d$\Documents and settings\" & strUser & """"
       -> return error  Microsoft VBScript runtime error: Bad file name or number
and
sFolderPath = "\\" & strComputer & "\d$\Documents and Settings\" & strUser
       -> return error Microsoft VBScript runtime error: Permission denied

Thx in advance for help
set FSO=CreateObject("Scripting.FileSystemObject")
sFolderPath = """\\" & strComputer & "\d$\Documents and settings\" & strUser & """"
fso.deleteFolder sFolderPath, true

Open in new window

SybuxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joseph DalyCommented:
Is this a requirement that this be done with VB? If not then you may want to try using the built in DEL command.

http://tmdnet.nothave.com/book/OS/MANUALS/COMMANDS/D/DEL.HTM
0
SybuxAuthor Commented:
It's a requirement. it's just part of a bigger script. I've just copy the part concerned by my problem
0
sameer2010Commented:
Permission denied is coming because D$ would need the local login credentials for the remote machine that you are not supplying.
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

SybuxAuthor Commented:
Wrong because I can remove folder \\computer\d$\test
0
sameer2010Commented:
Is D$ the name of folder?
0
MidnightOneCommented:
Can you check the permissions on the folder it's trying to remove? While you may have domain admin access, if you don't have permissions to the folder the script is trying to access you still won't be able to delete it.
0
RobSampsonCommented:
If you're trying to delete a user's folder, if they are logged in, you won't be able to delete it.....

This is the correct one though:
sFolderPath = "\\" & strComputer & "\d$\Documents and Settings\" & strUser

Regards,

Rob.
0
SybuxAuthor Commented:
Can you check the permissions on the folder it's trying to remove? While you may have domain admin access, if you don't have permissions to the folder the script is trying to access you still won't be able to delete it.

    -> yes I can delete it with the Windows Explorer

If you're trying to delete a user's folder, if they are logged in, you won't be able to delete it.....

This is the correct one though:
sFolderPath = "\\" & strComputer & "\d$\Documents and Settings\" & strUser

      -> of course user is not connected and just to sure, I've rebooted the computer so remove potentialy locked files. I've also tested with your syntax but in this case I got the Permission denied error.
0
RobSampsonCommented:
OK, out of curiosity, what happens if you run this from a DOS prompt:
rd /s "\\computer\c$\documents and settings\username"

Does that work?  Can you use the FileSystemObject in VBScript to delete a local folder?

Rob.
0
SybuxAuthor Commented:
Yes all is working. Except if I run the RD in a wshell object.
0
RobSampsonCommented:
Wow, so this works:
rd /s "\\computer\c$\documents and settings\username"

and this doesn't?

Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c rd /s ""\\computer\c$\documents and settings\username""", 1, True
0
SybuxAuthor Commented:
exact. I'm questioning about under which user account the cscript is running ? does it run under the user connect or another account ?
0
RobSampsonCommented:
When you run a script, it will run under the user context that initiates the script.  If the script is being run as a scheduled task, if may be running under a different user context, or, it may even be running as the SYSTEM account, in which case, it will not have rights to delete remote folders.  The SYSTEM account has limited network access.

When you tried the RD with the objShell object, did you specify cmd /c as I have shown above?

Rob.
0
SybuxAuthor Commented:
Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c rd /s ""\\computer\c$\documents and settings\username""", 1, True

is not working for folder under docuements and settings but for a folder created in the root it's ok
0
RobSampsonCommented:
That's really odd. There must be some other form of access rights....

If you run this from a DOS prompt with admin rights:
rd /s "\\computer\c$\documents and settings\username"

and still get permission denied a reboot from the remote machine, then there must be some other factor involved.

Is there any anti-virus software on the target machine that might be preventing the delete?

Rob.
0
ArmengarCommented:
It isnt working because your cmd is incorrect.  It will be awaiting a "Y/N" due to the /s you need to amend the cmd  (which you cannot see due to the so that it reads:

objShell.Run "cmd /c rd /s /Q ""\\computer\c$\documents and settings\username""", 7, True

This method is not exactly speedy either, to monitor its progress I recommend watching the "properties" of the folder.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobSampsonCommented:
Oh, you're right! LOL! I forgot about the /Q!!!  Sorry Sybux....thanks Armengar!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Digital Forensics

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.