Sub ListAllFiles()
Dim fs As FileSearch, ws As Worksheet, i As Long
Set fs = Application.FileSearch
With fs
.SearchSubFolders = False ' set to true if you want sub-folders included
.FileType = msoFileTypeAllFiles 'can modify to just Excel files eg with msoFileTypeExcelWorkbooks
.LookIn = "C:\" 'modify this to where you want to serach
If .Execute > 0 Then
Set ws = Worksheets.Add
For i = 1 To .FoundFiles.Count
ws.Cells(i, 1) = Mid$(.FoundFiles(i), InStrRev(.FoundFiles(i), "\") + 1)
Next
Else
MsgBox "No files found"
End If
End With
End Sub
Option Explicit
Public Sub ListAndHyperlink()
Const cstrPath As String = "E:\Knowledge Base\Books" 'Change this to suit
Dim varList As Variant
Dim i As Long
varList = Split(CreateObject("WScript.Shell").exec("cmd /c dir " & Chr(34) & cstrPath & Chr(34) & " /b").stdout.readall, vbCrLf)
For i = LBound(varList) To UBound(varList)
If Len(varList(i)) > 0 Then
Cells(i + 1, 1).Value = varList(i)
Cells(i + 1, 2).Formula = "=HYPERLINK(" & Chr(34) & cstrPath & Application.PathSeparator & varList(i) & Chr(34) & ",""Click Here"")"
End If
Next i
End Sub
Option Explicit
Public Sub ListAndHyperlink2()
Const cstrPath As String = "E:\Knowledge Base\Books" 'Change this to suit
Dim i As Long
Dim strDir As String
strDir = Dir(cstrPath & Application.PathSeparator & "*.pdf")
i = 1
Do While strDir <> ""
Cells(i, 1).Value = strDir
Cells(i, 2).Formula = "=HYPERLINK(" & Chr(34) & cstrPath & Application.PathSeparator & strDir & Chr(34) & ",""Click Here"")"
i = i + 1
strDir = Dir
Loop
End Sub
Option Explicit
Public Sub ListAndHyperlink3()
Const cstrPath As String = "E:\Knowledge Base\Books" 'Change this to suit
Dim wsCurr As Worksheet
Dim varList As Variant
Dim i As Long
varList = Split(CreateObject("WScript.Shell").exec("cmd /c dir " & Chr(34) & cstrPath & Chr(34) & " /b").stdout.readall, vbCrLf)
Set wsCurr = ActiveSheet
For i = LBound(varList) To UBound(varList)
If Len(varList(i)) > 0 Then
Cells(i + 1, 1).Value = varList(i)
wsCurr.Hyperlinks.Add Cells(i + 1, 1), cstrPath & Application.PathSeparator & varList(i), _
, "Click on the Hyperlink to see document!", varList(i)
End If
Next i
End Sub
Option Explicit
Public Sub ListAndHyperlink4()
Const cstrPath As String = "E:\Knowledge Base\Books" 'Change this to suit
Dim wsCurr As Worksheet
Dim varList As Variant
Dim i As Long
'\\ Use DOS to generate list of pdf files and build an array of names
varList = Split(CreateObject("WScript.Shell").exec("cmd /c dir " & Chr(34) & cstrPath & Chr(34) & " /b").stdout.readall, vbCrLf)
Set wsCurr = ActiveSheet
With wsCurr
'\\ Clear old list and assign suitable heading to the list
.Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp)).Delete xlUp
.Cells(1, 1).Value = "File List"
'\\ Loop through the array and create hyperlinks
For i = LBound(varList) To UBound(varList)
If Len(varList(i)) > 0 Then
wsCurr.Hyperlinks.Add .Cells(i + 2, 1), cstrPath & Application.PathSeparator & varList(i), _
, "Click on the Hyperlink to see document!", varList(i)
End If
Next i
End With
End Sub
Hi ,shrivallabha sir how can i include subfolders and can find a file with a input box. and i also need Location of file in C column
Sir please help me