Recursive Directory + file copy w/rename? 
Author Message
 Recursive Directory + file copy w/rename?

Hello all,

What I am trying to do is go thru a CD with TIF files on it.  The CD's dir
structure is under root, is around 20 or so dirs that are all 3 letter names.  
(IE  001, 002, 003, but not necessarily in any order.)  Under each of those
directories is another group of dirs of 2 chars. ( ie 01, 03, 05, but not nec. in
any order again.)  Under each of those dirs is my *.tif files  (Which are all 2
characters.  ie   01.tif, 02.tif, etc.  Not in any order again...)    I want to
go thru all dirs and copy every file to a file on my C Drive.  (Here's the
catch:)

I want to take and rename all of the files to "pick up" the dir names.

Such as:

0010201.tif   This would be (001 is top dir), (02 is dir under 001) and the last
is the file name.

We are using VB 4, and have been working with 2 other people.  All of us are
"newbie" VB programmers.  (And all of us are going crazy trying to figure it
out!)

Can anyone suggest/recommend a method of doing this?

Thanks (for saving us from insanity!!!)

Larry





Fri, 31 Jul 1998 03:00:00 GMT  
 Recursive Directory + file copy w/rename?

Quote:
> [snip]

You simply need to "accumulate" the names of the directories as you
recurse deeper into thd dir structure.  The variable used to accumulate
this "agregate dir name" should be passed BYVAL as a parameter to the
recursive routine.  In this way, VB's stack will un-accumulate the names
whenever you return from a recursive call.  Clear as mud?

Sub GetNames(ByVal CurrDir$, ByVal AccumDir$)
        Dim FileName$

        FileName$ = Dir$(CurrDir$ + "\*.TIF")
        Do While Len(FileName$)
                add (AccumDir$ + FileName$) to your list and copy it
                FileName$ = Dir$
        Loop
'look up the dir$ function, I don't remember if I coded the next line
'correctly
        FileName$ = Dir$(CurrDir$ + "\*.*", 16)
        Do While Len(FileName$)
                Call GetNames(CurrDir$ + "\" + FileName$,
                        AccumDir$ + FileName$)
                FileName$ = Dir$
        Loop
End Sub

I have left out some details, but this is the basic skeleton.

-chris



Sat, 01 Aug 1998 03:00:00 GMT  
 Recursive Directory + file copy w/rename?
No problem ... I don't have VB4 but I'm sure this works exactly the same
way as in VB3. I'm assuming your D: drive is your CD and that the only
directories that exist on D: contain tif files, and those tif files
aren't hidden, and don't already exist in the target directory your
copying to, or that the final name of the file will actuall be a legal
file name. (I have to put all these disclaimers in here .. I was spanked
recently.)  Anyway this is how it could go ...

Dim RootDir$
Dim SubDir$
Dim TifFiles$
Dim TifFilePath$
Dim TargetPath$

TargetPath = "C:\MYDIR"

RootDir = Dir$("D:\",16) '16 reads directories
If RootDir <> "" then    'directories do exist
   Do Until RootDir = ""
      SubDir = Dir$("D:\" & RootDir, 16)
      If SubDir <> "" then
         Do Until SubDir = ""
            TifFilePath = "D:\" & RootDir & "\" & SubDir"  
            TifFiles = Dir$(TifFilePath & "\*.tif")
            If TifFiles <> "" then
               Do Until TifFiles = ""
                  FileCopy (TifFilePath & "\" & TifFiles), TargetPath
                  Name (TargetPath & "\" & TifFiles) AS (TargetPath & "\"
& RootDir & SubDir & TifFiles)
                  TifFiles = Dir$
               Loop  
            End If
            SubDir = Dir$
         Loop
      End If
      RootDir = Dir$
   Loop
End if      

Know I admit I just threw this together, it's way past my bed time, and
I'm not thinking totally straight right now ... but you should get the
general idea here if it doesn't work just as it's written.  For help do
just that ... look up help on DIR - DIR$.

Hope this helps ... and that'll be fifty bucks .. or at least a job
interview  ;)



Sat, 01 Aug 1998 03:00:00 GMT  
 Recursive Directory + file copy w/rename?

infinite reservoirs of wisdom:

Quote:
>RootDir = Dir$("D:\",16) '16 reads directories

and normal files, and you don't check if it's a dir later on.
*spank*

Quote:
>If RootDir <> "" then    'directories do exist
>   Do Until RootDir = ""
>      SubDir = Dir$("D:\" & RootDir, 16)

uh oh. the dirs "." and ".." are returned here. *spank*

Quote:
>      If SubDir <> "" then
>         Do Until SubDir = ""
>            TifFilePath = "D:\" & RootDir & "\" & SubDir"  

one " to many. *spank*


--
Newbiehood is not a period of time, it's a way of life.

http://www.sn.no/~balchen
ftp://ftp.sn.no/user/balchen/



Sat, 01 Aug 1998 03:00:00 GMT  
 Recursive Directory + file copy w/rename?

Quote:




> >[snip]
> You'll run into trouble using the 'Call' statement in the second loop.
> Don't forget, if you make a call to the 'Dir$' function, it will wipe out
> the results of your previous 'Dir$'.  So, when you come back from the
> Call, the 'FileName$ = Dir$' won't work.

Very true, if only VB would provide access to interrupts, one could
implement a re-entrant DIR$.  Then again, with access to interrupts, VB
wouldn't need the DIR$ function.  

-chris



Mon, 03 Aug 1998 03:00:00 GMT  
 Recursive Directory + file copy w/rename?

How about something like:

a. getcwd [eg., cwd$ = "c:\001\01"]
b. create basename, eg:
   basename$ = mid$(cwd$, 4,3) & right$(cwd$, 2)]
c. recurse through files in cwd, ie., c:\001\01
   while (not done)
     file$ = getfilename
     copy file$ to "destpath\" & basename$ & file$
   wend
d. do above for sibling subdirs under \001
e  do above for dirs under \

That should be one way to tackle the task.  HTH

George


Quote:
>Hello all,

>What I am trying to do is go thru a CD with TIF files on it.  The CD's dir
>structure is under root, is around 20 or so dirs that are all 3 letter names.  
>(IE  001, 002, 003, but not necessarily in any order.)  Under each of those
>directories is another group of dirs of 2 chars. ( ie 01, 03, 05, but not nec. in
>any order again.)  Under each of those dirs is my *.tif files  (Which are all 2
>characters.  ie   01.tif, 02.tif, etc.  Not in any order again...)    I want to
>go thru all dirs and copy every file to a file on my C Drive.  (Here's the
>catch:)

>I want to take and rename all of the files to "pick up" the dir names.

>Such as:

>0010201.tif   This would be (001 is top dir), (02 is dir under 001) and the last
>is the file name.

>We are using VB 4, and have been working with 2 other people.  All of us are
>"newbie" VB programmers.  (And all of us are going crazy trying to figure it
>out!)

>Can anyone suggest/recommend a method of doing this?

>Thanks (for saving us from insanity!!!)

>Larry




===============================================================================
 George R. Torralba              xxxxxxxxxxxx                  Batang Cebu!!!
 Seattle, Washington             206.227.0821              MIME mail accepted
===============================================================================


Mon, 03 Aug 1998 03:00:00 GMT  
 Recursive Directory + file copy w/rename?
I admit ... I AM A MORON AT 4 in the morning.  As a matter of pride I
wrote a small routine given the example in the original question to
rectifiy this gentlemans problem. I tested it and then sent him a BAS
file <virus checked of course> admitting my stupidity and bowed my head.
PLEASE forgive me ... well maybe not forgive but let it slide this one
time.  Anyway ...I don't want to attach all the code here but here's how
it works psuedo style ... in a very basic form <I'm kissing feet now>

Array RootDirs$()
RootDirsPos%
Array SubDirs$()
SubDirsPos%
TargetDir$
FileNames$

TargetDir = C:\MyDir

Loop counting RootDirs
Redim RootDirs(according to count)

Do Until Ubound(RootDirs)
   Erase SubDirs
   SubDirsPos = 0
   loop counting SubDirs
   Redim SubDirs(according to count + 2 <for "." & "..">)
   Do Until Ubound(SubDirs)
      FileNames = Dir$(RootDirs(RootDirsPos) & "\" & SubDirs(SubDirsPos)
& "\" & *.tif)
      Do Until FileNames = ""
         FileCopy OriginalFile, TargetDir & "\" & RootDirs(RootDirsPos) &
SubDirs(SubDirsPos) & FileNames
         FileNames = Dir$
      Loop
      SubDirsPos = SubDirsPos + 1
   Loop
   RootDirsPos = RootDirsPos + 1
Loop

Now I know the psuedo code isn't perfect but the code I sent was ....
I must appologize to all ... and I promise not to do something that
stupid again!  I am my own worst critic.  I've been programming in Basic
for 17 years for cryin' out loud ... I just don't know what to say  ~<:(



Tue, 04 Aug 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. recursive copy and deletion of directories

2. Recursive renaming files

3. Recursive directories and files search

4. copy file, rename file API desired

5. Directory/File Recursive Scan - Could use some advice

6. Recursive file copy in VBScript

7. Directory File Rename Program

8. Rename multiple files within multiple directories

9. Renaming files in a directory

10. making directories and renaming files ! (newbie)

11. Using VB6 Listview to rename files in a directory

12. File and Directory Renaming

 

 
Powered by phpBB® Forum Software