Solved

Move file to folder based on name + log it.

Posted on 2000-05-01
9
170 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 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

839 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