Periodically listing files in a folder

H

haven104

Hi

I have a folder containing photos which are updated on a daily basi
(added to,replaced and removed.) I'm trying to set up a trackin
system to monitor the folder contents.

Can anyone point me in the right direction to listing the contents of
folder using VBA?

Ideally I'd like to have the code associted with a button which update
the file listing completely within excel.

I currently generate a txt file from a dos prompt, link to this an
update whenever neccesary.

Cheers
To
 
G

Guest

Hi Tom,

The code to achieve this is:
Sub dirList()
Dim MyFile, MyPath, MyName

Range("A1").Select

MyPath = "c:\"
MyName = Dir(MyPath)
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
ActiveCell.Value = MyName
ActiveCell.Offset(1, 0).Activate
End If
MyName = Dir
Loop

end sub

To get a button to launch it in Excel right click on the button bar and
select customise. Then, in the Toolbar tab click Add. Then go to Commands
tab, select Macros from the Categories box, and drag 'Custom Button' to your
new menu.

The last step is to right-click on your new button, select 'Assign Macro'
and point it to the new macro that you have just created.

Michael
 
J

Jack Sons

Tom,

For the Word-macro below (I saw it long ago in a Word news letter) you do
not need to enter the path (very convenient!).

Put this macro in Word and execute it. In the appearing "open" dialog box
just select your directory and highlight (click on) any file in it. Click on
the "open"-button (or OK-button, I do not know its name in the English
version) to open the file. It will not open, but instantly you will get a
Word document with lists the names of all files in that directory, topped by
the full path of your directory.

You can use it for any directory that you select (Excel, Word, C, D etc.)
without knowing or entering its full path. The result is a neat Word
document. Try it, it works like a charm.

Disregard the 'text in Dutch, it says the same as I wrote above.

Jack Sons
The Netherlands

----------------------------------------------------------------------------------------------

Sub Content_Directory()

' deze macro maakt het mogelijk een directory uit te printen doordat
' er een lijst in een Worddocument van wordt gemaakt
' als deze sub wordt uitgevoerd verschijnt de "normale" Open-dialogbox.
' Daarin moet een willekerig bestand worden aangeklikt (wordt dus blauw
' gehighlighted, en als dan op de knop "Openen" wordt geklikt, zal een
' nieuw Worddocument ontstaan - bij voorbeeld "document 1" - dat de
' complete list met bestanden bevat, met bovenaan het volledige pad
' naar de directory.

Dim PathWanted As String
Dim Temp As String
Dim i As Integer

With Dialogs(wdDialogFileOpen)
.Name = "*.*"
If .Display = -1 Then
'Documents.Add

PathWanted = Options.DefaultFilePath(wdDocumentsPath)
Set newdoc = Documents.Add
With newdoc
.Content.Font.Name = "Times New Roman"
.SaveAs FileName:=PathWanted
End With

Selection.TypeText "Files in " & PathWanted & ":" & vbCrLf
With Application.FileSearch
.LookIn = PathWanted
.FileName = "*.*"
If .Execute > 0 Then
For i = 1 To .FoundFiles.Count
Temp = .FoundFiles(i)
While InStr(Temp, "\") > 0
Temp = Mid(Temp, InStr(Temp, "\") + 1)
Wend
Selection.TypeText Temp & vbCrLf
Next
End If
End With
End If
End With
End Sub

----------------------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top