?
Solved

Delete files/folders than restart script

Posted on 2011-03-15
6
Medium Priority
?
407 Views
Last Modified: 2012-06-21
BACKGROUND: I am deploying an application that after the install it is not cleaning up the files and folders. I need some help creating a simple script that will delete two folders on hard drive and after that perform a restart.

I tried the following script to delete the folders but I was getting an error about permissions and I was running it as administrator. I am assuming it has to do with the files in the folders be read-only or something like that. So I need a code that will delete the folders regardless of the files that are in them. The code I was using is:

Dim FSO, Folder
set FSO=CreateObject("Scripting.FileSystemObject")
Folder="\Test"
FSO.DeleteFolder(Folder)

I also need a restart code to put at end of delete code.

Thanks

0
Comment
Question by:cmartineau
[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
  • 5
6 Comments
 
LVL 13

Accepted Solution

by:
connectex earned 2000 total points
ID: 35144774
You delete code looks good. But you probably should have the full path in there as the root will be the drive script was ran from. Also use the On Error Resume Next and On Error Goto 0 around code that may fail like deletes. This way the script will continue if the delete fails. I've also included some code to do the reboot.
Dim FSO, Folder
set FSO=CreateObject("Scripting.FileSystemObject")
Folder="\Test"
On Error Resume Next
FSO.DeleteFolder(Folder)
On Error GoTo 0

' Reboot system
Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//.").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each OpSys in OpSysSet
  OpSys.Reboot()
Next

Open in new window

0
 

Author Comment

by:cmartineau
ID: 35147021
I ran into an error running that code...

Line: 14
Char: 3
Error: Privilege not held.
Code: 80041062
Source: SWbemObjectEx

It is also not deleting my windows.old folder, any ideas?

Dim FSO, Folder1, Folder2
set FSO=CreateObject("Scripting.FileSystemObject")
Folder1="C:\minint"
Folder2="C:\windows.old"

On Error Resume Next
FSO.DeleteFolder(Folder1)
FSO.DeleteFolder(Folder2)
On Error GoTo 0

' Reboot system
Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//.").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each OpSys in OpSysSet
  OpSys.Reboot()
Next 

Open in new window

0
 

Assisted Solution

by:cmartineau
cmartineau earned 0 total points
ID: 35147195
I found this code to delete my windows.old folder saving as a .bat file and it worked... Is there an easier way to do this and can my script run the bat file? As for the reboot error I am running Windows 7 64-bit operating systems if that makes a difference. I saw win32 in the code.

Let me know Thanks!

@echo off

setlocal

Title Windows.Old Deleter

set BAT_HOME=%~dp0
set BAT_NAME=%~s0
set BATFILE=%~nx0
set DRVLET=%~d0


if "%1"=="" goto interactive

REM Flash related lines are meant for deleting Flash specific .OCX files which cannot be easily deleted

if exist %1\Windows\System32\Macromed\Flash CD /D %1\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F

cd /d %DRVLET%

takeown /f %1 /R /d y
icacls %1 /grant administrators:F /t
rd /s /q %1

goto end


:interactive

cls

cd /d %DRVLET%\

IF EXIST %DRVLET%\Windows.old ECHO. && ECHO Found %DRVLET%\Windows.old && GOTO Auto

IF NOT EXIST %DRVLET%\Windows.old ECHO. && ECHO NOT FOUND %DRVLET%\Windows.old && ECHO.


echo Enter folder name [with complete path] which needs to be deleted:
echo.
echo To exit type "x" and press Enter key

echo.
echo.

set /p user=Folder Name[with path]: 

if "%user%"=="x" goto end


if exist %user%\Windows\System32\Macromed\Flash CD /D %user%\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F

cd /d %DRVLET%\

takeown /f %user% /R /d y
icacls %user% /grant administrators:F /t
rd /s /q %user%


if not "%user%"=="x" goto interactive

goto end


:Auto
cls
ECHO.

cd /d %DRVLET%\

set user=Windows.old

if exist %DRVLET%\%user%\Windows\System32\Macromed\Flash CD /D %DRVLET%\%user%\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F

cd /d %DRVLET%\

takeown /f %user% /R /d y
icacls %user% /grant administrators:F /t
rd /s /q %user%


goto end

:end
ECHO.
ECHO Exiting...
ECHO.
pause

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cmartineau
ID: 35147259
Here is my new code... It calls on my win.bat file from above and works fine. I just need help on the reboot command.

Dim FSO, Folder
set FSO=CreateObject("Scripting.FileSystemObject")
Folder="C:\minint"

On Error Resume Next
FSO.DeleteFolder(Folder)
On Error GoTo 0

Set WSHShell = CreateObject("Wscript.Shell")
WSHShell.Run ("c:\winold.bat") 

' Reboot system
Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//.").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each OpSys in OpSysSet
  OpSys.Reboot()
Next 

Open in new window

0
 

Assisted Solution

by:cmartineau
cmartineau earned 0 total points
ID: 35147332
I figured it out... Thank you for helping out. My file code is below for anyone else that may need it.
Dim FSO, Folder
set FSO=CreateObject("Scripting.FileSystemObject")
Folder="C:\minint"

On Error Resume Next
FSO.DeleteFolder(Folder)
On Error GoTo 0

Set WSHShell = CreateObject("Wscript.Shell")
WSHShell.Run ("c:\winold.bat") 


Set WSHShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"

Open in new window

0
 

Author Closing Comment

by:cmartineau
ID: 35178747
Figured it out
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

752 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