Results 1 to 4 of 4

Thread: [RESOLVED]write/check file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Resolved [RESOLVED]write/check file

    Hello,

    How can I make an .vbs file that does this:
    Check if there is a hello.txt file with "hello" in it, if yes, give messagebox, if no, give messagebox and create/write the file.

    A sort of cookie system in vbscript, but it wil not be used on the web, just on my computer.

    Thanks in advance,
    Thomas
    Last edited by Mindstorms; Nov 17th, 2007 at 08:07 AM.

  2. #2
    Hyperactive Member kayos's Avatar
    Join Date
    Apr 2004
    Location
    Largo, Florida
    Posts
    306

    Re: write/check file

    VB Code:
    1. 'Code file for "hello.vbs"
    2.  
    3. Dim fso, flspec, fl, flcontents
    4.  
    5. 'First create fso (FileSystemObject)
    6. Set fso = CreateObject("Scripting.FileSystemObject")
    7.  
    8. 'Now look for file C:\hello.txt
    9. If fso.FileExists("C:\hello.txt") Then
    10.      'Get file to check size
    11.      Set flspec = fso.GetFile("C:\hello.txt")
    12.      If flspec.Size > 0 Then
    13.           'File size > 0 so file contains something
    14.       Set fl = fso.OpenTextFile("C:\hello.txt", 1, False)
    15.           flcontents = fl.ReadAll()
    16.           fl.Close
    17.      Else
    18.       'File size is 0 so nothing in file
    19.           flcontents = ""
    20.      End If
    21.      'Check file contents for "hello"
    22.      If flcontents = "hello" Then
    23.           '"hello" found
    24.           MsgBox("The file contatins hello!")
    25.      Else
    26.       '"hello" not found, but file exists
    27.           CreateHelloFile()
    28.           MsgBox("The file did not have hello in it.")
    29.      End If
    30. Else
    31.      '"C:\hello.txt did not exist
    32.      CreateHelloFile()
    33.      MsgBox("The file did not exist.")
    34. End If
    35.  
    36. 'Kill all objects
    37. Set fl = Nothing
    38. Set flspec = Nothing
    39. Set fso = Nothing
    40.  
    41. Function CreateHelloFile()
    42.      'Function that creates txt file C:\hello.txt and writes "hello" in it
    43.      Set fl = fso.OpenTextFile("C:\hello.txt", 2, True)
    44.      fl.Write("hello")
    45.      fl.Close
    46. End Function


    If this post helps, please RATE MY POST!

    Using Visual Studio 2005 SE

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    178

    Re: write/check file

    Thank you!

  4. #4
    Hyperactive Member kayos's Avatar
    Join Date
    Apr 2004
    Location
    Largo, Florida
    Posts
    306

    Re: write/check file

    no problem, don't forget to mark the thread resolved.


    If this post helps, please RATE MY POST!

    Using Visual Studio 2005 SE

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width