[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Delete files/folders than restart script

Posted on 2011-03-15
6
Medium Priority
?
412 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 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
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.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 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