[AccessD] Running a Dir command from within Access vba
    Doug Steele 
    dbdoug at gmail.com
       
    Sun Mar 12 14:07:49 CDT 2017
    
    
  
Here is some butchered code I pulled out of an old database.  It does a
recursive search to get all file names down through all subfolders of the
starting folder.  The original code did a bunch of processing on each file
name found; I've cut this code out and hopefully left a skeleton that you
can understand. You need to set a reference to the Microsoft Scripting
Runtime to get access to properties like .files, .subfolders etc.
Public Sub GetFiles(strStartingPath, strPath)
    Dim File
    Dim Subfols
    Dim SubFol
    Dim strSubFolderPath As String
    Dim CurrentPath As String
    Dim CurrentFile As String
    Dim FullPathFile As String
    Dim files
    Set files = strStartingPath.files
    For Each File In files
        'remove any trailing backslash in MyPath name
        If Right(strPath, 1) = "\" Then strPath = Left(strPath,
Len(strPath) - 1)
                CurrentFile = File.Name
                'add processing for this File.Name, like writing it to a
table
                CurrentPath = strPath
                FullPathFile = CurrentPath & "\" & CurrentFile
    Next
    'recursive search on subfolders
    Set Subfols = strStartingPath.subfolders
    For Each SubFol In Subfols
        If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
        strSubFolderPath = strPath & SubFol.Name
        GetFiles SubFol, strSubFolderPath
    Next
End Sub
On Sun, Mar 12, 2017 at 9:51 AM, jack drawbridge <jackandpat.d at gmail.com>
wrote:
> Hi again,
>
> Does anyone have sample vba to run a Dir command?
> I'm trying to get a listing of all *.jpg files from a directory and its
> subdirectories and put the list in a text file in a specific directory.
>
> I can get this to work going to command prompt and entering the string
> directly, but I want to do this from inside Access.
>
> cd  \users\mellon\documents\testImages Dir \s\b *.jpg >MyJpgs.txt
>
> Thanks,
> jack
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>
    
    
More information about the AccessD
mailing list