Results 1 to 6 of 6

Thread: [RESOLVED] check if exist

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    san francisco ca
    Posts
    120

    Resolved [RESOLVED] check if exist

    hello can anybody tellme how i can check if exist and if not create
    example

    c:\abc.txt

    if dont exist the EXE create with value 0

    thanks all

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: check if exist

    WAY 1: Simple Way

    Code:
    Private Sub Command1_Click()
        If Dir("C:\abc.txt") <> "" Then
            MsgBox "File exists"
        Else
            MsgBox "File does not exist"
            '~~> Code here to create the text file
            Dim i As Long
            Open "C:\abc.txt" For Output As #1
            For i = 0 To 5
                Print #1, i
            Next i
            Close #1
        End If
        
        '~~> If the file has special attributes (eg: is Hidden)
        '~~> you need to specify as below
        'If Dir("C:\abc.txt", vbHidden) <> "" Then
        'MsgBox "File exists"
        'End If
    End Sub
    WAY TWO: The API way

    Code:
    Option Explicit
    
    Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, _
    lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    
    Private Const OF_EXIST As Long = &H4000
    Private Const OFS_MAXPATHNAME As Long = 128
    Private Const HFILE_ERROR As Long = -1
    
    Private Type OFSTRUCT
        cBytes As Byte
        fFixedDisk As Byte
        nErrCode As Integer
        Reserved1 As Integer
        Reserved2 As Integer
        szPathName(OFS_MAXPATHNAME) As Byte
    End Type
    
    Private Sub Command1_Click()
        If FileExists("C:\abc.txt") = False Then
            '~~> Your code to create the text File
            Dim i As Long
            Open "C:\abc.txt" For Output As #1
            For i = 0 To 5
                Print #1, i
            Next i
            Close #1
        End If
    End Sub
    
    Private Function FileExists(ByVal Fname As String) As Boolean
        Dim Ret As Long, Ofstr As OFSTRUCT
        Ret = OpenFile(Fname, Ofstr, OF_EXIST)
        If Ret <> HFILE_ERROR Then
            FileExists = True
        Else
            FileExists = False
        End If
    End Function
    Last edited by Siddharth Rout; Mar 19th, 2009 at 01:00 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    san francisco ca
    Posts
    120

    Re: check if exist

    ok but the question its:
    if dont exist how i can make the program automatic create this file?

  4. #4

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: check if exist

    I have updated the code above which answers both your questions

    Edit: Rhino: You beat me to it
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Location
    san francisco ca
    Posts
    120

    Re: check if exist

    thanks all for the help

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