Home All Groups Group Topic Archive Search About

Using VBscript to perform a file pattern search



Author
26 Nov 2007 6:04 PM
DeJae
Hello All,

I am new at using vbscript. I have been using .bat files to try to
administer our file server, but for what I am trying to do right now, they
are not robust enough.

My goal is to delete files that are .tmp files  and files that have no
extension which are created by Excel. These files with no extensions (created
by Excel) are my biggest concern. These files are created when users who only
have r/w perms go in to modify a spreadsheet on the server and click save.
The temp files cannot be automatically deleted as designed, if the users like
the permissions to delete. So without granting users perms they are not
authorized to have, I am looking into creating a script that will go in and
search for these particular files through all subdirectories that would look
like this: C7F9F830

These temp files are always 8 characters long and have no extension. Is
there a way to perform a search through all subdirectories for this file type
that has no extension?

Author
26 Nov 2007 7:50 PM
pooradmin
On Nov 26, 1:04 pm, DeJae <De***@discussions.microsoft.com> wrote:
Show quote
> Hello All,
>
> I am new at using vbscript. I have been using .bat files to try to
> administer our file server, but for what I am trying to do right now, they
> are not robust enough.
>
> My goal is to delete files that are .tmp files  and files that have no
> extension which are created by Excel. These files with no extensions (created
> by Excel) are my biggest concern. These files are created when users who only
> have r/w perms go in to modify a spreadsheet on the server and click save.
> The temp files cannot be automatically deleted as designed, if the users like
> the permissions to delete. So without granting users perms they are not
> authorized to have, I am looking into creating a script that will go in and
> search for these particular files through all subdirectories that would look
> like this: C7F9F830
>
> These temp files are always 8 characters long and have no extension. Is
> there a way to perform a search through all subdirectories for this file type
> that has no extension?

This would be a nice one-liner with powershell.  can remove the whatif
when comfortable with it.

dir f:\temp\ -R | where { $_.name.length -eq 8 } | % { del $_.fullname
-whatif }

-J
www.pooradmin.com
Author
26 Nov 2007 8:11 PM
Pegasus (MVP)
Show quote
"DeJae" <De***@discussions.microsoft.com> wrote in message
news:AF2F3280-8294-4552-94FB-364AF85270DE@microsoft.com...
> Hello All,
>
> I am new at using vbscript. I have been using .bat files to try to
> administer our file server, but for what I am trying to do right now, they
> are not robust enough.
>
> My goal is to delete files that are .tmp files  and files that have no
> extension which are created by Excel. These files with no extensions
> (created
> by Excel) are my biggest concern. These files are created when users who
> only
> have r/w perms go in to modify a spreadsheet on the server and click save.
> The temp files cannot be automatically deleted as designed, if the users
> like
> the permissions to delete. So without granting users perms they are not
> authorized to have, I am looking into creating a script that will go in
> and
> search for these particular files through all subdirectories that would
> look
> like this: C7F9F830
>
> These temp files are always 8 characters long and have no extension. Is
> there a way to perform a search through all subdirectories for this file
> type
> that has no extension?

And here is a VB Script solution. Chatty, as usual, but it should
work. Remove the single quote at the start of Line 32 to activate it.

Dim ObjWshShell, objArgs, objFSO
Dim Folder, FileCount

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjWshShell = WScript.CreateObject("WScript.Shell")
Set objArgs = WScript.Arguments

FileCount = 0
Folder = ObjWshShell.CurrentDirectory
If objArgs.Count  > 0 Then Folder = objArgs(0)
ProcessFolder Folder

WScript.Echo Chr(10) & FileCount & " file(s) deleted"
Set objFSO = Nothing

'------------------------------
'Process one folder recursively
'------------------------------
Sub ProcessFolder(Folder)
' Dim objFSO, objFile, objFldr, objSFldr, path
Dim objFile, objFldr, objSFldr, path

WScript.echo "Processing " & Folder
WScript.Sleep(1000)
' Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFldr = objFSO.GetFolder(Folder)

For Each objFile In objFldr.Files
  If InStr(objFile.Name, ".") = 0 And Len(objFile.Name) = 8 Then
   FileCount = FileCount + 1
   WScript.Echo objFile.Name
'   objfile.Delete
  End If
Next

' Process subdirectories
For Each objSFldr In objFldr.SubFolders
  If Right(Folder,1) = "\" Then
   path = Folder & objSFldr.name
  Else
   path = Folder & "\" & objSFldr.Name
  End If
  ProcessFolder path
Next
End Sub

AddThis Social Bookmark Button