473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

add filenames

347 100+
does anyone know if its possible to add all the filenames in a folder to an access database i.e.

a folder contains

file1.mp3
file2.mp3
file3.mp3

i want a code that will search a folder on our local server and add all these filenames to a particular field in the DB
Mar 2 '09 #1
8 5117
ADezii
8,834 Expert 8TB
@colinod
  1. How to you want these File Names to appear in the Field?
    Expand|Select|Wrap|Line Numbers
    1. file1.mp3, file2.mp3, file2.mp3
    2. file1.mp3 file2.mp3 file2.mp3
    3. file1.mp3:file2.mp3:file2.mp3
  2. What are you basing the Filespec on?
  3. Where does the actual Filespec exist? (Text Box, Combo Box, etc.)
  4. What is the Field Name?
  5. What is the Name of the Table in which the Field exists?
  6. Are there other Fields in this Table, and if so, what are their Names and Data Types?
  7. etc...
Mar 2 '09 #2
OldBirdman
675 512MB
If the question is "How do I read a Directory", the answer is simple:
Expand|Select|Wrap|Line Numbers
  1.     strFileName = Dir(strCompleteDrivePath)
  2. Do Until strFileName = ""
  3.     '<< Code to handle file name strFile >>
  4.     strFileName = Dir()  'Get next name - May not be in alphabetic order
  5. Loop
Mar 2 '09 #3
colinod
347 100+
ADezii

i want one file name per field, they are in a table called mp3 in a field called File, its a hyperlink type of field, the other field in this tabel are as follows
mp3id - autonumber

File - hyperlink - comlete link to the file e.g.\\Pc04\artist voice clips\GILBREATH\GILBREATH - MASTER CD\AlexandraGilbreath.mp3

location - hyperlink - comlete link to the folder e.g.\\Pc04\artist voice clips\GILBREATH\GILBREATH - MASTER CD

Artist - text - just the name of the artist Artist
Alexandra Gilbreath

Information - memo - just info text if needed

filename - text just the filename - AlexandraGilbreath.mp3

do you need any more info, i can attach the db if you want
Mar 2 '09 #4
ADezii
8,834 Expert 8TB
@colinod
The following code will search for all *.mp3 Files in the C:\Misc Files\mp3s Folder. For every File it finds, it will write the Absolute Path of the File to the [File] Field (Type HYPERLINK) in the Table mp3. It will also write the Folder Name to the [Location] Field (Type HYPERLINK) in the mp3 Table. Both of these Fields will be functional Hyperlinks as long as they are defined as such. The Name of the retrieved Files only will also be written to the [Filename] Field as Text. The code has been thoroughly tested and is fully operational. Any questions, please feel free to ask.
Expand|Select|Wrap|Line Numbers
  1. Dim strFileName As String
  2. Dim MyDB As DAO.Database
  3. Dim rstMP3 As DAO.Recordset
  4. Dim strMsg As String
  5.  
  6. Const conFOLDER_PATH As String = "C:\Misc Files\mp3s\"
  7. Const conFILE_SPEC As String = "\*.mp3"
  8.  
  9. strMsg = "No Files were found in " & conFOLDER_PATH & " with a File Specification " & _
  10.          "of (" & Right$(conFILE_SPEC, 5) & ")"
  11.  
  12. If Dir(conFOLDER_PATH & conFILE_SPEC) = "" Then
  13.   MsgBox strMsg, vbExclamation, "No Files Found"
  14.     Exit Sub
  15. End If
  16.  
  17. Set MyDB = CurrentDb()
  18. Set rstMP3 = MyDB.OpenRecordset("mp3", dbOpenDynaset, dbAppendOnly)
  19.  
  20. strFileName = Dir(conFOLDER_PATH & conFILE_SPEC)
  21.  
  22. Do While strFileName <> ""
  23.   With rstMP3
  24.     .AddNew
  25.       ![File] = conFOLDER_PATH & strFileName & "#" & _
  26.                         conFOLDER_PATH & strFileName
  27.       ![Location] = Left$(conFOLDER_PATH, Len(conFOLDER_PATH) - 1) & _
  28.                         "#" & Left$(conFOLDER_PATH, Len(conFOLDER_PATH) - 1)
  29.       ![Filename] = strFileName
  30.     .Update
  31.   End With
  32.     strFileName = Dir()
  33. Loop
  34.  
  35. rstMP3.Close
  36. Set rstMP3 = Nothing
P.S. - Change the value of the conFOLDER_PATH and conFILE_SPEC Constants to suit your own specific needs.
Mar 3 '09 #5
colinod
347 100+
hi thanks for that, will this search in subfolders, so if i have various folders inside the mp3 folder will it find them also??

Its also throwing back an error at line 2
Mar 26 '09 #6
ADezii
8,834 Expert 8TB
@colinod
does anyone know if its possible to add all the filenames in a folder to an access database
No mention was ever made concerning Sub-Folders, so the code only returns Files matching a Filespec in a specific Folder. It can, however, be adapted to search Sub-Folders as well.
Its also throwing back an error at line 2
You are probably missing a Reference to the Microsoft DAO X.X Object Library
Mar 26 '09 #7
Anyone know how the code above would be adapted in order to include files within subfolders and/or the code I have listed below? I'm using similar code that I had found posted on this website a few years ago to list files in a specific file folder, and I am now trying to adapt it so I can list files within the subfolders. The code I've been attempting to adapt is below:

Sub GetFiles(strPath As String)
Dim rs As Recordset
Dim strFile As String, strDate As Date

'clear out existing data
CurrentDb.Execute "Delete * From tblDirectory", dbFailOnError

'open a recordset
Set rs = CurrentDb.OpenRecordset("tblDirectory", dbOpenDynaset)

'get the first filename
strFile = Dir(strPath, vbNormal)
'Loop through the balance of files
Do
'check to see if you have a filename
If strFile = "" Then
GoTo ExitHere
End If
strDate = FileDateTime(strPath & strFile)
rs.AddNew
'to save the full path using strPath & strFile
'save only the filename
rs!FileName = strFile
rs!FileDate = strDate
rs.Update

'try for next filename
strFile = Dir()
Loop

ExitHere:
Set rs = Nothing
MsgBox ("Directory list is complete.")
End Sub
Apr 7 '09 #8
ADezii
8,834 Expert 8TB
Here is some code that uses 1 Public Function, 2 Private Functions, the Dir() Function, a Collection, and Recursion to Print to the Immediate Window all *.mp3 Files that were found in a specified Folder and all its Sub-Folders. You can easily adapt it to suit your needs. Follow these Steps closely.
  1. Copy and Paste the following Function to a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public Function ListFiles(strPath As String, Optional strFileSpec As String, _
    2.                 Optional bIncludeSubfolders As Boolean)
    3. On Error GoTo Err_Handler
    4.  
    5. Dim colDirList As New Collection
    6. Dim varItem As Variant
    7.  
    8. 'colDirList - Files are rturned in this Collection
    9. Call FillDir(colDirList, strPath, strFileSpec, True)
    10.  
    11. For Each varItem In colDirList
    12.   Debug.Print varItem
    13. Next
    14.  
    15. Exit_Handler:
    16.   Exit Function
    17.  
    18. Err_Handler:
    19.   MsgBox "Error " & Err.Number & ": " & Err.Description
    20.   Resume Exit_Handler
    21. End Function
  2. Copy and Paste the following 2 Functions to the 'same' Standard Code Module used in Step 1.
    Expand|Select|Wrap|Line Numbers
    1. Private Function FillDir(colDirList As Collection, ByVal strFolder As String, strFileSpec As String, _
    2.                          bIncludeSubfolders As Boolean)
    3. 'Build up a list of files, and then add add to this list, any additional folders
    4. Dim strTemp As String
    5. Dim colFolders As New Collection        'holds Directory Names
    6. Dim vFolderName As Variant
    7.  
    8. 'Add the files to the folder.
    9. strFolder = TrailingSlash(strFolder)    'ensures Folder Names end with a Slash "\"
    10. strTemp = Dir(strFolder & strFileSpec)  'adds FileSpec to the Folder Name
    11.  
    12. Do While strTemp <> vbNullString
    13.   colDirList.Add strFolder & strTemp    'Folder\ & FileSpec
    14.   strTemp = Dir                         'recursively calls the Dir() Function
    15. Loop
    16.  
    17. If bIncludeSubfolders Then      'Include Sub-Directories?
    18.   'Build collection of additional subfolders.
    19.   strTemp = Dir(strFolder, vbDirectory)     'passed Folder Argument
    20.  
    21.   Do While strTemp <> vbNullString
    22.     If (strTemp <> ".") And (strTemp <> "..") Then      'Ignore . and ..
    23.       If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then      'Is it a Directory?
    24.         colFolders.Add strTemp      'Adds Directory/Folder to colFolders Collection
    25.       End If
    26.     End If
    27.       strTemp = Dir                     'recursively calls the Dir() Function
    28.   Loop
    29.  
    30.   'Call function recursively for each subfolder.
    31.   For Each vFolderName In colFolders
    32.     Call FillDir(colDirList, strFolder & TrailingSlash(vFolderName), strFileSpec, True)
    33.   Next vFolderName
    34. End If
    35. End Function
    Expand|Select|Wrap|Line Numbers
    1. Private Function TrailingSlash(varIn As Variant) As String
    2. If Len(varIn) > 0& Then
    3.   If Right(varIn, 1&) = "\" Then
    4.     TrailingSlash = varIn
    5.   Else
    6.     TrailingSlash = varIn & "\"
    7.   End If
    8. End If
    9. End Function
  3. Call the Entry Level Function and pass to it the Path, File-spec, and either True or False to include Sub-Directories. The following code will list all *.mp3 Files in the Windows Folder as well as any *.mp3 Files that may exist in Sub-Folders under the Windows Folder:
    Expand|Select|Wrap|Line Numbers
    1. Call ListFiles("C:\Windows", "*.mp3", True)
  4. Any questions, please feel free to ask.
Apr 7 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ralph Freshour | last post by:
I have a PHP web app using MySQL - when I save a .jpg file named test's.jpg I see that the filename on the unix server is: test\'s.jpg - the filename I end up saving in my SQL table is named...
9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
8
by: Tim | last post by:
Does anyone know a way to read the filenames from a given directory in C in a Solaris environment? I did this, but it seems goofy: sprintf(t, "ls *.csv > filenames.txt"); system(t); fptr =...
1
by: Pieter | last post by:
I want to be able to split regular MAX_PATH length filenames and 32K UNICODE filenames using the \\?\ naming convention. Are there any equivalents to _tsplitpath() for long names, or am I left to...
2
by: rbutch | last post by:
guys, i need a little help with this. this is working (well sort of) i get the info, but it's not moving to a new line as it iterates thru the array and all of the fields are like ONE HUGE LONG...
0
by: n33470 | last post by:
We have a web site that is being converted from the 1.1 format into 2.0. I've noticed that after the web project has been converted, the first time that the solution is opened in VS, all of the...
22
by: rtilley | last post by:
# Spaces are present before and after the XXX filename = ' XXX ' new_filename = filename.strip() if new_filename != filename: print filename Macs allow these spaces in file and folder...
2
by: Taras_96 | last post by:
Hi everyone, Firstly, I would like to know if you can open chinese filenames under win2000 using PHP 5.0? I have a file named 中国.php, and try to open it using fopen(‘中国.php','r');....
0
by: chongming | last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser. My...
3
by: Torsten Bronger | last post by:
Hallöchen! I'd like to map general unicode strings to safe filename. I tried punycode but it is case-sensitive, which Windows is not. Thus, "Hallo" and "hallo" are mapped to "Hallo-" and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.