Solved

Delete files/folders than restart script

Posted on 2011-03-15
6
397 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now