Solved

Move file to folder based on name + log it.

Posted on 2000-05-01
9
167 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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