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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

19 Experts available now in Live!

Get 1:1 Help Now