Solved

Move file to folder based on name + log it.

Posted on 2000-05-01
9
169 Views
Last Modified: 2010-05-02
Example:

I have 2 files that come in c:\file:
1) a1.txt
2) b3.txt

I have folders c:\a and c:\b.  Within folders a and b I have subfolders 1-9. (ie. c:\a\1, c:\a\2, ...)

I want to move the file based on the filename to the appriate folder. a1.txt should be moved to c:\a\1. b3.txt should be moved to c:\b\3.

Then I want to log the event in c:\move.log
I want filename|datemoved|from|to

Thanks in advance,
Doug
0
Comment
Question by:douglaskarr
  • 4
  • 3
  • 2
9 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2766654
You might use several techniques (ordered by my preference)
1) File System Object (Windows Scripting Host)
2) Dir and FileCopy (VB intrinsic)
3) Dir and Copy/Move called with Shell (VB)

Comment for 1): This technique allows you to get com objects with file collections per folder, with copy/move methods. Additionally, you have good error messages.

Comment for 2): Use this if you don't have the Windows Scripting Host available.

Comment for 3): Don't use this, it's bullshit

Hope this helps
0
 
LVL 1

Accepted Solution

by:
jgravelle earned 150 total points
ID: 2766671
Dim strFilename As String
Dim strDestination As String

strFilename = "b3.txt"
strDestination = "c:\" & Left(strFilename, 1) & "\" & Mid(strFilename, 2, 1) & "\" & strFilename

'********************
'Just use this if the file does not
'already exist at the destination. The
'VB Filecopy will fail if the
'destination file does not already
'exist.
Open strDestination For Append As #1
Close
'********************

'****************************
FileCopy "c:\windows\desktop\" & strFilename, strDestination
'****************************

'*******************
'Adds the entry to the move.log file
Open "c:\move.log" For Append As #1
Print #1, strFilename, Now, strDestination
Close
'*******************

Otherwise you could use the FileSystemObject to achieve the same thing but it would be an additional reference for your project.
0
 

Author Comment

by:douglaskarr
ID: 2766695
Adjusted points from 100 to 150
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:douglaskarr
ID: 2766696
Outstanding!

If you don't mind me asking, how would I do it if the folder names were not the same as the filename:

example:

a=anderson
4=type

In other words.  a4.txt should go to c:\anderson\type

I realize this is a different situation than what I originally specified.  I added 50 points accordingly.
0
 
LVL 1

Expert Comment

by:jgravelle
ID: 2766727
Do you have a textfile that already has this information in it. For example a text file that would state that a4.txt belongs to c:\anderson\type. Would files that begin with "a" always go to anderson or could they go to "amy" for example.
0
 

Author Comment

by:douglaskarr
ID: 2766998
A would always go to the same directory.

4 would always go to the same subdirectory.

I didn't write all the rules since they are quite comprehensive.  The filenames are ~20 characters long.
0
 
LVL 1

Expert Comment

by:jgravelle
ID: 2767019
One more quick question. How many different letter combinations will you be dealing with (i.e. "a", "b"...etc.) and I assume only numbers from 1 to 9.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2767084
26x
  9
---
234
0
 
LVL 1

Expert Comment

by:jgravelle
ID: 2767168
Dim strFilename As String
Dim strDestination As String

Dim tmpArray(26) As String
Dim i As Integer
Dim strSubPart As String
Dim strFrom As String

tmpArray(1) = "anderson"
tmpArray(2) = "bob"
tmpArray(3) = "chris"
'etc....etc.

strFilename = "a4.txt"

'Loop through and look for a match for the first letter
For i = 1 To 26
    If tmpArray(i) = Left(strFilename, 1) Then
        strDestination = "c:\" & tmpArray(i)
        Exit For
    End If
Next

strSubPart = Mid(strFilename, 2, 1)

'This should cover the numeric portion
Select Case strSubPart
    Case 1
        strDestination = strDestination & "\" & "whatever 1 is"
    Case 2
        strDestination = strDestination & "\" & "whatever 2 is"
    Case 3
        strDestination = strDestination & "\" & "whatever 3 is"
    Case 4
        strDestination = strDestination & "\" & "type"
    Case 5
        strDestination = strDestination & "\" & "whatever 5 is"
    Case 6
        strDestination = strDestination & "\" & "whatever 6 is"
    Case 7
        strDestination = strDestination & "\" & "whatever 7 is"
    Case 8
        strDestination = strDestination & "\" & "whatever 8 is"
    Case 9
        strDestination = strDestination & "\" & "whatever 9 is"
End Select


'New destination string
strDestination = strDestination & "\" & strfilenme

'********************
'Just use this if the file does not
'already exist at the destination. The
'VB Filecopy will fail if the
'destination file does not already
'exist.
Open strDestination For Append As #1
Close
'********************

'****************************
FileCopy "c:\file\" & strFilename, strDestination
'****************************
strFrom = "c:\file\" & strFilename

'*******************
'Adds the entry to the move.log file
Open "c:\move.log" For Append As #1
Print #1, strFilename, Now, strFrom, strDestination
Close
'*******************

I haven't really tested this but it looks like it should do the job. This is assuming that all of your directories are already in place(i.e. anderson")
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now