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

Posted on 2009-04-06
Last Modified: 2012-05-06

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

Question by:nathanlukewong
LVL 38

Accepted Solution

Shift-3 earned 500 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


Set colSubFolders = objRoot.SubFolders

For Each objFolder in colSubfolders

    If strFound = "" Then

        GetSubFolders objFolder.Path

    End If


If strFound = "" Then

    strFound = strDefault & "\" & strFileName

    Set objTextFile = objFSO.CreateTextFile(strFound)


End If

Set objTarget = objFSO.GetFile(strFound)

intAttrib = objTarget.Attributes

If Not objTarget.Attributes And 1 Then

    objTarget.Attributes = intAttrib + 1


    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



    Set colSubfolders2 = objSub.SubFolders

    For Each objFolder2 in colSubfolders2

        If strFound = "" Then

            GetSubFolders objFolder2.Path

        End If


End Sub

Open in new window


Author Closing Comment

ID: 31566999
fan bloody tastic

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
This is an addendum to the following article: Acitve Directory based Outlook Signature ( The script is fine, and works in normal client-server domains…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

20 Experts available now in Live!

Get 1:1 Help Now