Solved problem with Dir function (1 Viewer)

poppajim713

New member
Local time
Today, 17:22
Joined
Dec 1, 2022
Messages
20
I've tried using all text within the Dir function like sFile = "C:\Users\file.txt" which works, but trying
Dim sTxt As String
sTxt = "file.txt"
sFile = "C:\Users\" & sTxt doesn't work. The file is not found.
 

Jon

Access World Site Owner
Staff member
Local time
Today, 22:22
Joined
Sep 28, 1999
Messages
7,398
Welcome to the forums! We are the most active Microsoft Access community on the internet by far, with posts going back over 20 years!

To get started, I highly recommend you read the post below. It contains important information for all new users to this forum.

https://www.access-programmers.co.uk/forums/threads/new-member-read-me-first.223250/

We look forward to having you around here, learning stuff and having fun!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 05:22
Joined
May 7, 2009
Messages
19,245
maybe use:

what if you use:

sFile =Environ("Userprofile") & "\" & sTxt
Debug.Print Dir$(sFile)
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:22
Joined
Sep 21, 2011
Messages
14,310
And what do you get if you debug.print sFile?
 

poppajim713

New member
Local time
Today, 17:22
Joined
Dec 1, 2022
Messages
20
Well that cannot be right?, not if it starts with "C:\Users\" ?
This is exactly what I tested.
Code:
sTxt = "12-1-2022.mdb"
sFile = Environ("Userprofile") & "\Backup\" & sTxt
Debug.Print sFile
nothing happens. Thanks anyway.
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:22
Joined
Sep 21, 2011
Messages
14,310
You were looking at the Immediate window I hope? :(
 

jdraw

Super Moderator
Staff member
Local time
Today, 17:22
Joined
Jan 23, 2006
Messages
15,379
Just tested in immediate window (using A365/64 bit)
sTxt = "12-1-2022.mdb"
sFile = Environ("Userprofile") & "\Backup\" & sTxt
Debug.Print sFile

Result:
C:\Users\JP\Backup\12-1-2022.mdb
 

poppajim713

New member
Local time
Today, 17:22
Joined
Dec 1, 2022
Messages
20
Just tested in immediate window (using A365/64 bit)
sTxt = "12-1-2022.mdb"
sFile = Environ("Userprofile") & "\Backup\" & sTxt
Debug.Print sFile

Result:
C:\Users\JP\Backup\12-1-2022.mdb
I did a bit of research. I think Environ started with Access 2007. I'm still using 2003.
 

poppajim713

New member
Local time
Today, 17:22
Joined
Dec 1, 2022
Messages
20
I tried some code that found a folder, but not a file in that folder
this is the code I tried. the first one the folder exist, but the second one the file doesn't exist.
Code:
   Dim FolderPath As String

    FolderPath = "C:\Users\smltu\Backup"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"

    Else
        MsgBox "Folder doesn't exist"
    End If
    
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\smltu\Backup\12-1-2022.mdb"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If
 

poppajim713

New member
Local time
Today, 17:22
Joined
Dec 1, 2022
Messages
20
this is the code I tried. the first one the folder exist, but the second one the file doesn't exist.
Code:
   Dim FolderPath As String

    FolderPath = "C:\Users\smltu\Backup"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"

    Else
        MsgBox "Folder doesn't exist"
    End If
   
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\smltu\Backup\12-1-2022.mdb"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If
this is the code I tried. the first one the folder exist, but the second one the file doesn't exist.
Code:
   Dim FolderPath As String

    FolderPath = "C:\Users\smltu\Backup"
    If Right(FolderPath, 1) <> "\" Then
        FolderPath = FolderPath & "\"
    End If

    If Dir(FolderPath, vbDirectory) <> vbNullString Then
        MsgBox "Folder exist"

    Else
        MsgBox "Folder doesn't exist"
    End If
   
    Dim FilePath As String
    Dim TestStr As String

    FilePath = "C:\Users\smltu\Backup\12-1-2022.mdb"

    TestStr = ""
    On Error Resume Next
    TestStr = Dir(FilePath)
    On Error GoTo 0
    If TestStr = "" Then
        MsgBox "File doesn't exist"
    Else
        MsgBox "File exist"
    End If
I changed one line to this and it worked.
Code:
FilePath = "C:\Users\smltu\Backup\*.mdb"
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 22:22
Joined
Sep 12, 2006
Messages
15,658
Are you sure there weren't any stray spaces in any of the strings causing the issues?
 

Users who are viewing this thread

Top Bottom