Find name of file in dir
-
- PlatinumLounger
- Posts: 4534
- Joined: 26 Apr 2010, 17:36
Find name of file in dir
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
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.
-
- Administrator
- Posts: 79676
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find name of file in dir
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
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4534
- Joined: 26 Apr 2010, 17:36
Re: Find name of file in dir
HansV wrote: ↑28 Nov 2022, 08:14Code: 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
-
- PlatinumLounger
- Posts: 4534
- Joined: 26 Apr 2010, 17:36
Re: Find name of file in dir
sorry bro...HansV wrote: ↑28 Nov 2022, 08:14Code: 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
based this code how to rename only all files with namefile.jpeg in namefile.jpg, delete old file , *.jpeg
-
- Administrator
- Posts: 79676
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find name of file in dir
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
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4534
- Joined: 26 Apr 2010, 17:36
Re: Find name of file in dir
bro...sal21 wrote: ↑28 Nov 2022, 08:35HansV wrote: ↑28 Nov 2022, 08:14Code: 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
is this correct to use a multi pattern for files in dir:
...
sPATTERN = "*.gif;*.bmp;*.jpg"
sPath = STRPATHIMG
sfile = Dir(sPath & sPATTERN & ".*")
...
-
- Administrator
- Posts: 79676
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find name of file in dir
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.
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4534
- Joined: 26 Apr 2010, 17:36
-
- Administrator
- Posts: 79676
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find name of file in dir
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
Best wishes,
Hans
Hans
-
- 2StarLounger
- Posts: 116
- Joined: 10 Oct 2022, 02:52
Re: Find name of file in dir
Dear All,
I get file names to an excel sheet from a folder, using the below macro. I tried to sort them in ascending order. but not sorted them in the correct order. please can anyone solve this issue?
Correct oder : 1-1.pdf, 1-2.pdf, 1-3.pdf ............... 1-10.pdf, 1-11.pdf......1-20.pdf..
After getting to the excel sheet: 1-1.pdf, 1-10.pdf, 1-11.pdf, 1-20.pdf........
My VBA Code :
Sub Get_Old_File_Name()
Dim myPath As String
Dim myFile As String
Dim r As Long
myPath = "C:\Users\User\Desktop\Maling Payadvice\NVQ\PaySlips"
myFile = Dir(myPath & "*.*")
If myFile = "" Then
MsgBox " No Files in the Folder"
Else
r = 4
Do While myFile <> ""
Cells(r, 5).Value = myFile
r = r + 1
myFile = Dir
Loop
MsgBox "Completed"
End If
End Sub
I get file names to an excel sheet from a folder, using the below macro. I tried to sort them in ascending order. but not sorted them in the correct order. please can anyone solve this issue?
Correct oder : 1-1.pdf, 1-2.pdf, 1-3.pdf ............... 1-10.pdf, 1-11.pdf......1-20.pdf..
After getting to the excel sheet: 1-1.pdf, 1-10.pdf, 1-11.pdf, 1-20.pdf........
My VBA Code :
Sub Get_Old_File_Name()
Dim myPath As String
Dim myFile As String
Dim r As Long
myPath = "C:\Users\User\Desktop\Maling Payadvice\NVQ\PaySlips"
myFile = Dir(myPath & "*.*")
If myFile = "" Then
MsgBox " No Files in the Folder"
Else
r = 4
Do While myFile <> ""
Cells(r, 5).Value = myFile
r = r + 1
myFile = Dir
Loop
MsgBox "Completed"
End If
End Sub
-
- Administrator
- Posts: 79676
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find name of file in dir
What is the largest number before the "-", and what is the largest number after the "-"?
Best wishes,
Hans
Hans
-
- 2StarLounger
- Posts: 116
- Joined: 10 Oct 2022, 02:52
Re: Find name of file in dir
Dear Hans,
As my Example, my original file's name is "1.pdf" (I can use any name I like as the file name, - number or letters). After splitting to many files they were displayed as 1.1.pdf, 1.2.pdf, 1.3.pdf......... 1.25.pdf, etc. the largest number before the "-" is "1" & largest number after the "-" is 54 (First file name: 1.1.pdf & Last file name 1.54.pdf)
Thanks,
Regarding
Priyantha
As my Example, my original file's name is "1.pdf" (I can use any name I like as the file name, - number or letters). After splitting to many files they were displayed as 1.1.pdf, 1.2.pdf, 1.3.pdf......... 1.25.pdf, etc. the largest number before the "-" is "1" & largest number after the "-" is 54 (First file name: 1.1.pdf & Last file name 1.54.pdf)
Thanks,
Regarding
Priyantha
-
- Administrator
- Posts: 79676
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Find name of file in dir
Try this:
Code: Select all
Sub Get_Old_File_Name()
Dim myPath As String
Dim myFile As String
Dim r As Long
myPath = "C:\Users\User\Desktop\Maling Payadvice\NVQ\PaySlips"
myFile = Dir(myPath & "*.*")
If myFile = "" Then
MsgBox " No Files in the Folder"
Else
Application.ScreenUpdating = False
r = 3
Do While myFile <> ""
r = r + 1
Cells(r, 5).Value = myFile
myFile = Dir
Loop
Call Sort_Names(Range("E4:E" & r))
Application.ScreenUpdating = True
MsgBox "Completed"
End If
End Sub
Sub Sort_Names(rng As Range)
rng.Offset(ColumnOffset:=1).EntireColumn.Insert
rng.Offset(ColumnOffset:=1).FormulaR1C1 = "=-MID(RC[-1],FIND(""-"",RC[-1]),FIND(""."",RC[-1])-FIND(""-"",RC[-1]))"
rng.Resize(ColumnSize:=2).Sort Key1:=rng(1, 2), Header:=False
rng.Offset(ColumnOffset:=1).EntireColumn.Delete
End Sub
Best wishes,
Hans
Hans