Vb6 Open file to add problem. Path not found.

Open  App.Path & "\Folder\" & str(0) For Output

It seems that the path was not found, however, if immediately before this I do

MsgBox App.Path & "\Folder\" & str(0)

It provides the correct directory / file name I want

and if I replace this line with the direct path in quotation marks, it works fine, but it is not very good for other users of my application :( Does anyone know why this does not work?

+3
source share
3 answers

You can open a file that does not exist. I tried:

  Open "c:\temp\test.txt" & Str(0) For Output As #1
  Close #1

When it starts, c: \ temp \ test.txt 0 is created

Note that I added "As # 1" to the Open statement, and taht Str (0) adds the leading space for the optional minus sign (CStr (0) does not add the leading space)

+2
source

: , .

true, . , "path not found".

+2

Here's something easy I made for you:

Function CreateLog(Destination As String, MyMessage As String)
    Dim PathToCreate, FolderPath, FileName As String

    'Check for Unnecessary Spaces
    Destination = Trim(Destination)
    FolderStr = Destination

    'Gather only FolderPath of Destination
    Do
        FolderStr = Mid(FolderStr, 1, Len(FolderStr) - 1)
    Loop Until Right(FolderStr, 1) = "\" Or Len(FolderStr) < 1

    'Gather only FileName
    FileName = Mid(Destination, Len(FolderStr) + 1, Len(Destination) - Len(FolderStr))

    'If the path does not exist than create it
    'Recursive approach
    For Each Folder In Split(FolderStr, "\")
        If InStr(1, Folder, ":") = 0 Then
            PathToCreate = PathToCreate & "\" & Folder
        Else
            PathToCreate = Folder
        End If
        If fso.FolderExists(PathToCreate) = False And PathToCreate <> "" Then
            fso.CreateFolder PathToCreate
        End If
    Next

    'Open file and add the message in it
    Open PathToCreate & "\" & FileName & ".txt" For Append As #1
    Print #1, MyMessage
    Close #1

End Function

Using:

CreateLog "D:\Test\NewTest\NewFolder\AnotherFolder\atlastthefile.abcdefg", "Hello!"

It doesn’t matter which fileExtention the reason is bad to add ".txt" anyway ..

0
source

Source: https://habr.com/ru/post/1699622/


All Articles