Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Move file to folder based on name + log it.

Posted on 2000-05-01
9
Medium Priority
?
174 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 600 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
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…
Suggested Courses

636 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