Solved

Move file to folder based on name + log it.

Posted on 2000-05-01
9
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 143

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

729 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