Find name of file in dir

User avatar
sal21
PlatinumLounger
Posts: 4169
Joined: 26 Apr 2010, 17:36

Find name of file in dir

Post by sal21 »

Base myvar="102016", how to check if file in dir exists, and retrieve the complete name of file?

Possible without loop file in dir?

all file have a numeric name, but with variable length
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 75751
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find name of file in dir

Post by HansV »

Code: Select all

    Dim myFolder As String
    Dim myFile As String
    myFolder = "C:\Lavori_VB6\...\...\"
    myFile = Dir(myFolder & myVar & ".*")
    If myFile = "" Then
        MsgBox "File does not exist!"
    Else
        MsgBox "The complete filename is " & myFile
    End If
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4169
Joined: 26 Apr 2010, 17:36

Re: Find name of file in dir

Post by sal21 »

HansV wrote:
28 Nov 2022, 08:14

Code: Select all

    Dim myFolder As String
    Dim myFile As String
    myFolder = "C:\Lavori_VB6\...\...\"
    myFile = Dir(myFolder & myVar & ".*")
    If myFile = "" Then
        MsgBox "File does not exist!"
    Else
        MsgBox "The complete filename is " & myFile
    End If
:clapping:

User avatar
sal21
PlatinumLounger
Posts: 4169
Joined: 26 Apr 2010, 17:36

Re: Find name of file in dir

Post by sal21 »

HansV wrote:
28 Nov 2022, 08:14

Code: Select all

    Dim myFolder As String
    Dim myFile As String
    myFolder = "C:\Lavori_VB6\...\...\"
    myFile = Dir(myFolder & myVar & ".*")
    If myFile = "" Then
        MsgBox "File does not exist!"
    Else
        MsgBox "The complete filename is " & myFile
    End If
sorry bro...
based this code how to rename only all files with namefile.jpeg in namefile.jpg, delete old file , *.jpeg

User avatar
HansV
Administrator
Posts: 75751
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find name of file in dir

Post by HansV »

Code: Select all

    Dim myFolder As String
    Dim myFile As String
    myFolder = "C:\Lavori_VB6\...\...\"
    myFile = Dir(myFolder & "*.jpeg")
    Do While myFile <> ""
        Name myFolder & myFile As myFolder & Replace(myFile, ".jpeg", ".jpg")
        myFile = Dir
    Loop
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4169
Joined: 26 Apr 2010, 17:36

Re: Find name of file in dir

Post by sal21 »

sal21 wrote:
28 Nov 2022, 08:35
HansV wrote:
28 Nov 2022, 08:14

Code: Select all

    Dim myFolder As String
    Dim myFile As String
    myFolder = "C:\Lavori_VB6\...\...\"
    myFile = Dir(myFolder & myVar & ".*")
    If myFile = "" Then
        MsgBox "File does not exist!"
    Else
        MsgBox "The complete filename is " & myFile
    End If
:clapping:
bro...
is this correct to use a multi pattern for files in dir:

...
sPATTERN = "*.gif;*.bmp;*.jpg"

sPath = STRPATHIMG

sfile = Dir(sPath & sPATTERN & ".*")

...

User avatar
HansV
Administrator
Posts: 75751
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find name of file in dir

Post by HansV »

No, you can process only one attachment at a time. So you'll have to use one loop for *.gif, one for *.bmp and one for *.gif.
Regards,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4169
Joined: 26 Apr 2010, 17:36

Re: Find name of file in dir

Post by sal21 »

HansV wrote:
14 Dec 2022, 11:14
No, you can process only one attachment at a time. So you'll have to use one loop for *.gif, one for *.bmp and one for *.gif.
OK.
Peraps, split into array and loop it, for example:

arrpattern="*.gif;*.jpg;*.bmp"

User avatar
HansV
Administrator
Posts: 75751
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find name of file in dir

Post by HansV »

Yes:

Code: Select all

    Dim myFolder As String
    Dim myFile As String
    dim myExtension As Variant
    myFolder = "C:\Lavori_VB6\...\...\"
    For Each myExtension in Array("*.gif", "*.jpg", "*.bmp")
        myFile = Dir(myFolder & myExtention)
        Do While myFile <> ""
            ' Your code here
            ...
            myFile = Dir
        Loop
    Next myExtension
Regards,
Hans