Solved

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

Posted on 2009-04-06
2
490 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 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
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

707 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