?
Solved

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

Posted on 2009-04-06
2
Medium Priority
?
493 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

777 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