Get file name from folder in excel file

atuljadhavnetafim

Active Member
Joined
Apr 7, 2012
Messages
341
Office Version
  1. 365
Platform
  1. Windows
Hi,

i have 1000+ PDF files in one folder, now i want only file name in excel from A2.
any macro for that else i need to copy file name (F2) and paste in excel.

file store at C:\2022-23\Q2\Certificates
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi atuljadhavnetafim,

maybe

VBA Code:
Public Sub MrE_1225548_1616A0B()
' https://www.mrexcel.com/board/threads/get-file-name-from-folder-in-excel-file.1225548/
' Created: 20221228
' By:      HaHoBe

Dim strFile       As String
Dim lngWrite      As Long

Const cstrPath As String = "C:\2022-23\Q2\Certificates"

strFile = Dir(cstrPath & "*.pdf")
ActiveSheet.UsedRange.ClearContents

lngWrite = 1
Do While strFile <> ""
  lngWrite = lngWrite + 1
  ActiveSheet.Cells(lngWrite, "A").Value = strFile
  strFile = Dir
Loop
End Sub

Ciao,
Holger
 
Upvote 0
How about this ?
HTH

VBA Code:
Sub sbFileName()
    Dim myPath
    myPath = "C:\2022-23\Q2\Certificates\"
    
    Dim myFilename
    myFilename = Dir(myPath)
    
    Range("A1") = "FileName" 'If you already have a title, delete the line.
    Do Until myFilename = ""
        Range("A" & Rows.Count).End(xlUp).Offset(1, 0) = myFilename
        myFilename = Dir
    Loop
End Sub
 
Upvote 0
Solution
Hi,

it's not working, after run macro nothing coming in excel file nor show any error.
can you please check again

i need excel copy file name and paste A2 then go to second file and paste A3 so on till last file
 
Upvote 0
Hmmm... In Windows, click Start, type "Command" and select "Command Prompt". You should end up in a terminal window with the prompt C:\Users\USERID> (where USERID is your computer login ID).
type "CD Desktop" and hit [Enter]. The prompt will change to C:\Users\USERID\Desktop>.
Type "DIR C:\2022-23\Q2\Certificates\*.PDF /B >PDFList.txt" and hit [Enter]. If there are subdirectories (folders) under the Certificates directory, add " /S" after /B in that command.
This will create the text file C:\Users\USERID\Desktop\PDFList.txt. It will be a list of all the files in the Certificates folder and sub folders if you used /S with all the file names with the full path such as C:\2022-23\Q2\Certificates\PDFFile.pdf.
You can now open the text file in Excel which will run the Text/CSV Import wizard, or use Power Query where it would be a lot easier to strip the path from the filename.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,824
Members
449,050
Latest member
Bradel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top