Solved

Delete files/folders than restart script

Posted on 2011-03-15
6
405 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 500 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Office 1 64
Assign a value in Excel V-Basic 3 36
how to customize the text in the legend in highcharts 3 73
Access 2003, find all instances of database ODBC 3 41
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

738 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