Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBScript : howto delete a remote folder

Posted on 2010-01-11
17
Medium Priority
?
3,226 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
  • 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
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is an article on how to answer questions, earn points and become an expert.
Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
Suggested Courses

580 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