|
-
Jun 15th, 2006, 11:23 AM
#1
Thread Starter
New Member
deleating .bak files from subfolders
Whats up everyone,
Im having a bit of trouble getting this script to delete the .bak files in all subfolders. Im new to .vbs so please be gental. Here is what I have so far, it does delete the .bak files in that directory, but ive tried all kinds of things to make it delete in the subfolders too.
On Error Resume Next
const Archive = "\\Bubba\Archive\Archive Holdings"
Const DeleteReadOnly = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(Archive)
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Do Until i = 1
strFiles = strPath & "\*.bak"
objFSO.DeleteFile(strFiles), DeleteReadOnly
strFiles = strPath & "\...\*.bak" <------line im having trouble with*****
objFSO.DeleteFile(strFiles), DeleteReadOnly
Wscript.Sleep 6000000
Loop
-
Jun 15th, 2006, 01:12 PM
#2
PowerPoster
Re: deleating .bak files from subfolders
You have to loop through each folder .. here is an example .. you will need to enter the path yourself somewhere .. this example shows how to let the user input the Path in a Popup .. you can comment that out and use a constant, or just set fileName to your path in the "CheckFolderEntry" sub, and get rid of the input box.
Code:
' Note, save this script as a .vbs file.
'----------------------------
Option Explicit
'----------------------------
'// PROGRAM SETTINGS
Const ProgTitle = "Delete Files & Folders"
Const FileExt = ".bak"
'// Const folderName = "C:\Temp"
'----------------------------
'// DECLARATIONS
Dim FileCnt
Dim FolderCnt
Dim folderName
FileCnt = 0
FolderCnt = 0
'********* LOGIN **********
'**************************
'----------------------------
'// GET FOLDER & PERFORM TASKS
Sub ParseFolder()
Call DeleteFiles()
Call EndMessage()
End Sub
'----------------------------
'// CHECK IF FOLDER EXISTS
Function DoesFolderExist()
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FolderExists(folderName)) Then
DoesFolderExist = True
end if
Set objFSO = nothing
End function
'----------------------------
'// DELETE FILES IN SUB FOLDERS
Sub DeleteInFolder(byRef objFolder)
Dim objFile
Dim objSubFolder
'// Loop through the files and delet each one with .bak
For Each objFile in objFolder.Files
If Right(objFile.Name,Len(FileExt))=FileExt Then
objFile.delete(True)
FileCnt = FileCnt + 1
End If
next
'// Loop through the sub folders and call self on each folder.
For Each objSubFolder in objFolder.SubFolders
Call DeleteInFolder(objSubFolder)
FolderCnt = FolderCnt + 1
Next
End Sub
'----------------------------
'// DELETE FILES
Sub DeleteFiles()
Dim objFSO
Dim objFolder
'// Setup the FSO Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'// Get the root folder
Set objFolder = objFSO.GetFolder(folderName)
'// Do the Delete on the root folder.
Call DeleteInFolder(objFolder)
End Sub
'----------------------------
'// FOLDER NOT EXIST
Sub badFolder()
msgBox("Folder: " & folderName &", does not exist! Operation Cancelled")
End Sub
'----------------------------
'// END MESSAGE
Sub EndMessage()
Call MsgBox("Deleted " & FileCnt & _
" Files from " & FolderCnt & _
" Folders", vbOkOnly, ProgTitle)
End Sub
'----------------------------
'// CANCEL BY USER
Sub operationCancelled()
Call MsgBox("User Cancelled", vbOkOnly, ProgTitle)
End Sub
'----------------------------
'// START PROGRAM
Sub startRoutines()
if FolderName = "" Then
FolderName = InputBox("Enter Folder Path (eg: c:\test)", ProgTitle)
End If
if FolderName <> "" then
if DoesFolderExist() = True then
call ParseFolder()
else
call badFolder()
end if
else
call operationCancelled()
end if
End Sub
'----------------------------
call startRoutines()
Last edited by rory; Jun 6th, 2015 at 03:45 PM.
-
Jun 15th, 2006, 02:36 PM
#3
Thread Starter
New Member
Re: deleating .bak files from subfolders
thanks Rory,
I tried that script however it gives me an error,
Line 62
Char 4
Error Permission denied
Code 800A0046
Source Microflop VBscript runtime error
its the line:
objFile.delete
So Im not sure why its not deleting.
maybe a readonly issue.
Last edited by adambeazley; Jun 15th, 2006 at 02:39 PM.
-
Jun 15th, 2006, 02:55 PM
#4
PowerPoster
Re: deleating .bak files from subfolders
Yep that is a permissions error .. is this on your hard drive and are you logged in as Admin ..??
-
Jun 15th, 2006, 03:34 PM
#5
Thread Starter
New Member
Re: deleating .bak files from subfolders
well at first i was running the script on the network server //Bubba from my computer and got that error. Then I went to the machine and ran the script and got the same error.
Just went back to the machine--logged in as the network administrator and ran it and yet again I got the same error.
Now when I logged on as Administrator its the Domain admin account and the computer logs into our domain name instead of the local computer.
Now do I need to actually logon as the local administrator to that particular machine?? I hope not because its an old machine that was setup before I even got here and I dont have the local administrator password to it.
Is there a way that this can run without admin rights?
Im testing this script on that old machine because I dont like tesing these things on our main file server. Our server is running out of space because of all of the autocad.bak files so thats why Im trying to get this script working.
-
Jun 15th, 2006, 03:36 PM
#6
PowerPoster
Re: deleating .bak files from subfolders
Either way you will need to be logged in as a user that has write permissions .. or set up those folders to be read and write for everyone .. but you need to be logged in as the local admin as far as i know ..
If you knew the local admin user and pass you could auto login in the script before deleting ..
Rory
-
Jun 15th, 2006, 03:38 PM
#7
Thread Starter
New Member
Re: deleating .bak files from subfolders
I just put the script on my machine and tried it and I got exactly the same error.
-
Jun 15th, 2006, 03:41 PM
#8
PowerPoster
Re: deleating .bak files from subfolders
Are you logged on with admin rights ..?
Also, what OS are you using ..?
Works for sure on XPSP2 as admin.
-
Jun 15th, 2006, 03:41 PM
#9
Thread Starter
New Member
Re: deleating .bak files from subfolders
How would I auto login the script?
I would prefer that, so that I dont have to take the server off the network to excecute it.
-
Jun 15th, 2006, 03:52 PM
#10
PowerPoster
Re: deleating .bak files from subfolders
change this sub to reflect these changes . add in your admin user name and pass ..
Code:
Sub ParseFolder()
Dim WshShell
Set WshShell = Createobject("Wscript.Shell")
WshShell.Run "runas /user:ADMINNAME", 0, True
WshShell.Sendkeys "ADMINPASS"
Call DeleteFiles()
Call EndMessage()
Set WshShell = Nothing
End Sub
-
Jun 15th, 2006, 04:11 PM
#11
PowerPoster
Re: deleating .bak files from subfolders
Sorry, didnt realise you were also dealing with Read Only files .. update this line .. Forcing Deletion with the TRUE.
objFile.delete(True)
-
Jun 15th, 2006, 04:22 PM
#12
Thread Starter
New Member
Re: deleating .bak files from subfolders
It works great now, and I dont even have to use the auto login, just by adding the (True) it does the job.
thank you so much
Adam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|