+ Reply to Thread
Results 1 to 3 of 3

Delete multiple files from a folder using an Access Database

  1. #1

    Delete multiple files from a folder using an Access Database

    Good Morning All,

    I am not sure if I am posting this in the right place, but please bear
    with me.

    I am trying to run a db query to quickly populate a database with
    filenames from a folder on my hard drive. Then in turn I would like to
    be able to delete files from the folder, based on my deletes in the
    database.

    I hope that this makes sense. I am currently using the following code
    in the Visual Basic editor to populate my table with the necessary
    data:

    Sub GetGameFiles()
    Dim strFile As String
    Dim strFolder As String
    Dim strSQL As String

    strFolder = "C:\path\to\thefile\iwantadded\tothisdb\"
    strFile = Dir(strFolder & "*.*")
    Do While Len(strFile) > 0
    strSQL = "INSERT INTO gamenames (gamefilename) " & _
    "VALUES (" & Chr$(34) & strFile & Chr$(34) & ")"
    CurrentDb.Execute strSQL, dbFailOnError
    strFile = Dir()
    Loop

    End Sub

    I am currently running Access 2003. I also thought about trying this
    with Excel 2003

    If anyone can offer assistance it would be greatly appreciated.

    Thank you.

    Devon L.


  2. #2
    Michael H
    Guest

    RE: Delete multiple files from a folder using an Access Database

    Hi.

    Is there any other reason you are placing the filenames into a table first?
    Why not just delete the files within the Do While loop?

    Just replace this:
    strSQL = "INSERT INTO gamenames (gamefilename) " & _
    "VALUES (" & Chr$(34) & strFile & Chr$(34) & ")"
    CurrentDb.Execute strSQL, dbFailOnError

    With this:
    Kill strFolder & strFile

    Or, if you need the filenames in a table for some additional reason, just
    insert my line of code right before this one:
    strFile = Dir()

    -Michael



    "[email protected]" wrote:

    > Good Morning All,
    >
    > I am not sure if I am posting this in the right place, but please bear
    > with me.
    >
    > I am trying to run a db query to quickly populate a database with
    > filenames from a folder on my hard drive. Then in turn I would like to
    > be able to delete files from the folder, based on my deletes in the
    > database.
    >
    > I hope that this makes sense. I am currently using the following code
    > in the Visual Basic editor to populate my table with the necessary
    > data:
    >
    > Sub GetGameFiles()
    > Dim strFile As String
    > Dim strFolder As String
    > Dim strSQL As String
    >
    > strFolder = "C:\path\to\thefile\iwantadded\tothisdb\"
    > strFile = Dir(strFolder & "*.*")
    > Do While Len(strFile) > 0
    > strSQL = "INSERT INTO gamenames (gamefilename) " & _
    > "VALUES (" & Chr$(34) & strFile & Chr$(34) & ")"
    > CurrentDb.Execute strSQL, dbFailOnError
    > strFile = Dir()
    > Loop
    >
    > End Sub
    >
    > I am currently running Access 2003. I also thought about trying this
    > with Excel 2003
    >
    > If anyone can offer assistance it would be greatly appreciated.
    >
    > Thank you.
    >
    > Devon L.
    >
    >


  3. #3

    Re: Delete multiple files from a folder using an Access Database

    what's wrong with storing them in a database?

    why try to change the guys requirements?

    sure there is a risk of having 'out of date' information

    using a tool like SQL Server instead of a crutch like MDB would make
    this a lot easier.

    you could even populate the list of files / folders through a simpe
    recursive DOS command.

    SQL beats VB any day of the week (for complexity, performance,
    stability, etc)





    Michael H wrote:
    > Hi.
    >
    > Is there any other reason you are placing the filenames into a table first?
    > Why not just delete the files within the Do While loop?
    >
    > Just replace this:
    > strSQL = "INSERT INTO gamenames (gamefilename) " & _
    > "VALUES (" & Chr$(34) & strFile & Chr$(34) & ")"
    > CurrentDb.Execute strSQL, dbFailOnError
    >
    > With this:
    > Kill strFolder & strFile
    >
    > Or, if you need the filenames in a table for some additional reason, just
    > insert my line of code right before this one:
    > strFile = Dir()
    >
    > -Michael
    >
    >
    >
    > "[email protected]" wrote:
    >
    > > Good Morning All,
    > >
    > > I am not sure if I am posting this in the right place, but please bear
    > > with me.
    > >
    > > I am trying to run a db query to quickly populate a database with
    > > filenames from a folder on my hard drive. Then in turn I would like to
    > > be able to delete files from the folder, based on my deletes in the
    > > database.
    > >
    > > I hope that this makes sense. I am currently using the following code
    > > in the Visual Basic editor to populate my table with the necessary
    > > data:
    > >
    > > Sub GetGameFiles()
    > > Dim strFile As String
    > > Dim strFolder As String
    > > Dim strSQL As String
    > >
    > > strFolder = "C:\path\to\thefile\iwantadded\tothisdb\"
    > > strFile = Dir(strFolder & "*.*")
    > > Do While Len(strFile) > 0
    > > strSQL = "INSERT INTO gamenames (gamefilename) " & _
    > > "VALUES (" & Chr$(34) & strFile & Chr$(34) & ")"
    > > CurrentDb.Execute strSQL, dbFailOnError
    > > strFile = Dir()
    > > Loop
    > >
    > > End Sub
    > >
    > > I am currently running Access 2003. I also thought about trying this
    > > with Excel 2003
    > >
    > > If anyone can offer assistance it would be greatly appreciated.
    > >
    > > Thank you.
    > >
    > > Devon L.
    > >
    > >



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1