?
Solved

VBScript : howto delete a remote folder

Posted on 2010-01-11
17
Medium Priority
?
3,171 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:Sybux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +3
17 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 26284333
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
 

Author Comment

by:Sybux
ID: 26284421
It's a requirement. it's just part of a bigger script. I've just copy the part concerned by my problem
0
 
LVL 13

Expert Comment

by:sameer2010
ID: 26284764
Permission denied is coming because D$ would need the local login credentials for the remote machine that you are not supplying.
0
Supports up to 4K resolution!

The VS192 2-Port 4K DisplayPort Splitter is perfect for anyone who needs to send one source of DisplayPort high definition video to two or four DisplayPort displays. The VS192 can split and also expand DisplayPort audio/video signal on two or four DisplayPort monitors.

 

Author Comment

by:Sybux
ID: 26284809
Wrong because I can remove folder \\computer\d$\test
0
 
LVL 13

Expert Comment

by:sameer2010
ID: 26284861
Is D$ the name of folder?
0
 
LVL 26

Expert Comment

by:MidnightOne
ID: 26285322
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 26289428
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
 

Author Comment

by:Sybux
ID: 26290841
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 26291021
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
 

Author Comment

by:Sybux
ID: 26291035
Yes all is working. Except if I run the RD in a wshell object.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 26291049
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
 

Author Comment

by:Sybux
ID: 26291161
exact. I'm questioning about under which user account the cscript is running ? does it run under the user connect or another account ?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 26291226
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
 

Author Comment

by:Sybux
ID: 26415277
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 26423827
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
 
LVL 4

Accepted Solution

by:
Armengar earned 1000 total points
ID: 26544035
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
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 1000 total points
ID: 26549516
Oh, you're right! LOL! I forgot about the /Q!!!  Sorry Sybux....thanks Armengar!
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question