[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Delete files/folders than restart script

Posted on 2011-03-15
6
Medium Priority
?
409 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
Technology Partners: 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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

649 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