[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

search hard drive for file, if not found create..

Posted on 2009-04-06
2
Medium Priority
?
499 Views
Last Modified: 2012-05-06
HI

I need some code that will allow me to search a local drive for a file, and if it does not exist to create it using FileSystemObject.  I then need to use FileSystemObject or Getfile to toggle the attribute of the file (meaning that if it IS set to 'read only', set it anything BUT 'read only, and if it is NOT 'read-only, it must be set TO 'read-only'.
it is important as my boss requires this to be in vbs script and using the filesystemobject

Thanks
0
Comment
Question by:nathanlukewong
[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
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 24077365
Paste the script below into a text file with a .vbs extension.  Customize the value of the strRoot variable on line 1 with the letter of the drive to search.  Customize the value of the strFileName variable on line 2 with the name of the file to find.  Customize the value of the strDefault variable on line 3 with the location in which to create the file if it is not found.

Running the script will search the drive for the file.  If it is not found, it will create an empty text file with that name in the specified folder.  It will then toggle the Read-Only attribute of the file.


strRoot = "c:"
strFileName = "filename.txt"
strDefault = "c:\files"
 
On Error Resume Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objRoot = objFSO.GetFolder(strRoot)
Set colFiles = objRoot.Files
 
For Each objFile in colFiles
    If LCase(objFile.Name) = LCase(strFileName) Then
        strFound = objFile.Path
    End If
Next
 
Set colSubFolders = objRoot.SubFolders
 
For Each objFolder in colSubfolders
    If strFound = "" Then
        GetSubFolders objFolder.Path
    End If
Next
 
If strFound = "" Then
    strFound = strDefault & "\" & strFileName
    Set objTextFile = objFSO.CreateTextFile(strFound)
    objTextFile.Close
End If
 
Set objTarget = objFSO.GetFile(strFound)
intAttrib = objTarget.Attributes
 
If Not objTarget.Attributes And 1 Then
    objTarget.Attributes = intAttrib + 1
Else
    objTarget.Attributes = intAttrib - 1
End If
 
Sub GetSubFolders(strFolderPath)
    Set objSub = objFSO.GetFolder(strFolderPath)
    
    Set colFiles2 = objSub.Files
 
    For Each objFile2 in colFiles2
        If LCase(objFile2.Name) = LCase(strFileName) Then
            strFound = objFile2.Path
        End If
    Next
        
    Set colSubfolders2 = objSub.SubFolders
 
    For Each objFolder2 in colSubfolders2
        If strFound = "" Then
            GetSubFolders objFolder2.Path
        End If
    Next
End Sub

Open in new window

0
 

Author Closing Comment

by:nathanlukewong
ID: 31566999
fan bloody tastic
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

649 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