Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - please help with folder search and copy

Reply
 
Old 12-03-2008   #1 (permalink)
Thomas Niemann


 
 

please help with folder search and copy

Hi VBS-Grouü,

i have to find a sollution for the following, but i am afraid that this task
is too hard for me as a vbs newbie...

There is a folder which contains files like "12345678_BlaBla.doc".
Or Folder\Subfolder\12345678_BlaBla.doc

There is an additional destination folder-structure like this:

FolderName1\12345678\serverdocuments.doc
FolderName1\45642354\serverdocuments.doc
FolderName2\12345678\serverdocuments.doc
FolderName3\43233242\serverdocuments.doc

What i have to do is to go throu a source path (Structure), take the first 8
letters/numers of the filenames, search for folders with the same name as
the 8letters/numbers part of the filename at the destination, and copy the
file in each folder that matches.


Thank you all in advance

Kind Regards

Thomas



My System SpecsSystem Spec
Old 12-03-2008   #2 (permalink)
Harvey Colwell


 
 

Re: please help with folder search and copy

Give this a try.



[--- Begin: Migrate Files.vbs ---]

001. Option Explicit
002.
003. Dim oFS, oFile, sFile
004.
005. Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
006.
007. Const sSourceFdr = "C:\Folder1\"
008. Const sDestFdr = "D:\Folder2\"
009.
010. For Each oFile In oFS.GetFolder(sSourceFdr).Files
011. If oFS.FolderExists(sDest & Left(oFile.Name, 8)) Then
012. On Error Resume Next
013. oFS.MoveFile oFile.Path, sDest & Left(oFile.Name, 8) & "\"
014. On Error GoTo 0
015. End If
016. Next
017.
018. Set oFS = Nothing
019. WScript.Quit
020.

[--- End: Migrate Files.vbs ---]




"Thomas Niemann" <niemann.thomas@xxxxxx> wrote in message
news:49366ae1$0$31343$9b4e6d93@xxxxxx-online.net...
Quote:

> Hi VBS-Grouü,
>
> i have to find a sollution for the following, but i am afraid that this
> task is too hard for me as a vbs newbie...
>
> There is a folder which contains files like "12345678_BlaBla.doc".
> Or Folder\Subfolder\12345678_BlaBla.doc
>
> There is an additional destination folder-structure like this:
>
> FolderName1\12345678\serverdocuments.doc
> FolderName1\45642354\serverdocuments.doc
> FolderName2\12345678\serverdocuments.doc
> FolderName3\43233242\serverdocuments.doc
>
> What i have to do is to go throu a source path (Structure), take the first
> 8 letters/numers of the filenames, search for folders with the same name
> as the 8letters/numbers part of the filename at the destination, and copy
> the file in each folder that matches.
>
>
> Thank you all in advance
>
> Kind Regards
>
> Thomas
>
>
My System SpecsSystem Spec
Old 12-04-2008   #3 (permalink)
Thomas Niemann


 
 

Re: please help with folder search and copy

I have modified the script like the following, and it seems to work if the
searched folder is directly under the sdestfdr folder.
It does not work for folders in subfolders of the sdestfdr like
c:\temp\folder\searchedsubfolder.
Are you able to extend the script below, please, please, please,
please(...)?

Option Explicit
Dim oFS, oFile, sFile
Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
Const sSourceFdr = "C:\"
Const sDestFdr = "c:\temp\"
For Each oFile In oFS.GetFolder(sSourceFdr).Files
WScript.Echo Left(oFile.Name, 8)
If oFS.FolderExists(sDestFdr & Left(oFile.Name, 8)) Then
WScript.Echo (sDestFdr & Left(oFile.Name, 8))
'On Error Resume Next
oFS.MoveFile oFile.Path, sDestFdr & Left(oFile.Name, 8) & "\"
'On Error GoTo 0
End If
Next
Set oFS = Nothing
WScript.Quit


My System SpecsSystem Spec
Old 12-04-2008   #4 (permalink)
Pegasus \(MVP\)


 
 

Re: please help with folder search and copy


"Thomas Niemann" <niemann.thomas@xxxxxx> wrote in message
news:49366ae1$0$31343$9b4e6d93@xxxxxx-online.net...
Quote:

> Hi VBS-Grouü,
>
> i have to find a sollution for the following, but i am afraid that this
> task is too hard for me as a vbs newbie...
>
> There is a folder which contains files like "12345678_BlaBla.doc".
> Or Folder\Subfolder\12345678_BlaBla.doc
>
> There is an additional destination folder-structure like this:
>
> FolderName1\12345678\serverdocuments.doc
> FolderName1\45642354\serverdocuments.doc
> FolderName2\12345678\serverdocuments.doc
> FolderName3\43233242\serverdocuments.doc
>
> What i have to do is to go throu a source path (Structure), take the first
> 8 letters/numers of the filenames, search for folders with the same name
> as the 8letters/numbers part of the filename at the destination, and copy
> the file in each folder that matches.
>
>
> Thank you all in advance
>
> Kind Regards
>
> Thomas
>
You could try this script. It is fully recursive. I recommend that you try
to understand it in order to adjust it to your specific requirements. Note
that it copies files (as per your initial post) instead of moving them as
per your subsequent note.

01. Option Explicit
02. Const InFolder = "D:\SourceDir\"
03. Const sTarget = "D:\TargetDir\"
04. Dim oFSO, aFolders
05. Set oFSO = CreateObject("Scripting.FileSystemObject")
06.
07. GetTargetFolders sTarget, aFolders
08. GetFiles InFolder, aFolders
09.
10. '------------------------
11. 'Process one folder level
12. '------------------------
13. Sub GetFiles(sFolder, aFldrs)
14. Dim oFolder, oFldr, oFile, sName, i
15.
16. WScript.echo
17. WScript.Echo "Processing """ & sFolder & """"
18. Set oFolder = oFSO.GetFolder(sFolder)
19. For Each oFile In oFolder.Files
20. sName = LCase(Left(oFile.Name, 8))
21. For i = 0 To UBound(aFldrs)
22. If sName = aFldrs(i, 1) Then
23. WScript.Echo "Copy " & sFolder & oFile.Name & " " _
24. & aFldrs(i,0) & aFldrs(i,1) & "\" & oFile.Name
25. ' oFSO.CopyFile sFolder & oFile.Name, _
26. ' aFldrs(i,0) & aFldrs(i,1) & "\" & oFile.Name, True
27. End If
28. Next
29. Next
30.
31. 'Recurse into subfolders
32. For Each oFldr In oFolder.SubFolders
33. WScript.Echo oFldr.Path
34. GetFiles oFldr.Path & "\", aFldrs
35. Next
36. End Sub
37. '--------------------------------------------------------------
38. 'Compile a list of all existing target
39. 'folders \Target\DirName\*.*
40. 'Put the prefix \Target\DirName into array element aFldrs(x, 0)
41. 'Put the name (the rightmost 8 chars) into array element aFldrs(x, 1)
42. '--------------------------------------------------------------
43. Sub GetTargetFolders(sFolder, aFldrs)
44. Dim oFolder, oFldr, oSubFldr, line, aAux, i
45.
46. line = ""
47. Set oFolder = oFSO.GetFolder(sFolder)
48. For Each oFldr In oFolder.SubFolders
49. For Each oSubFldr In oFldr.SubFolders
50. line = line & oFolder.Path & "\" & oFldr.Name _
51. & "\" & oSubFldr.Name & "/"
52. Next
53. Next
54.
55. aAux = Split(line, "/")
56. ReDim aFldrs(UBound(aAux)-1, 2)
57. For i = 0 To UBound(aAux)-1
58. aFldrs(i, 0) = Left(aAux(i), Len(aAux(i)) - 8)
59. aFldrs(i, 1) = LCase(Right(aAux(i), 8))
60. Next
61. End Sub


My System SpecsSystem Spec
Old 12-09-2008   #5 (permalink)
Thomas Niemann


 
 

Re: please help with folder search and copy

> You could try this script. It is fully recursive. (...)
Thank you very much! Your script works great.

I´ve tried to understand it, but some parts are a riddle for me...
Could you explain the following:
Quote:

> 55. aAux = Split(line, "/")
> 56. ReDim aFldrs(UBound(aAux)-1, 2)
> 57. For i = 0 To UBound(aAux)-1
> 58. aFldrs(i, 0) = Left(aAux(i), Len(aAux(i)) - 8)
> 59. aFldrs(i, 1) = LCase(Right(aAux(i), 8))
> 60. Next
> 61. End Sub

Another Task:
í have seen that there are some/too much files where the search-string is
within the filename, not at the beginning. Sample:
RA-12074_11001092_abc.idw
The interesting part is the Number between the two underlines.
or
Technik 11004052 18.05.04
The interesting Part is the number between the two spaces
How could the be implemented?

Can you help me again, please?

My System SpecsSystem Spec
Old 12-10-2008   #6 (permalink)
Pegasus \(MVP\)


 
 

Re: please help with folder search and copy


"Thomas Niemann" <Niemann.Thomas@xxxxxx> wrote in message
news:493e9534$0$31339$9b4e6d93@xxxxxx-online.net...
Quote:
Quote:

>> You could try this script. It is fully recursive. (...)
> Thank you very much! Your script works great.
>
> I´ve tried to understand it, but some parts are a riddle for me...
> Could you explain the following:
>
Quote:

>> 55. aAux = Split(line, "/")
>> 56. ReDim aFldrs(UBound(aAux)-1, 2)
>> 57. For i = 0 To UBound(aAux)-1
>> 58. aFldrs(i, 0) = Left(aAux(i), Len(aAux(i)) - 8)
>> 59. aFldrs(i, 1) = LCase(Right(aAux(i), 8))
>> 60. Next
>> 61. End Sub
You must look at these lines in the context of the preceding lines. Here is
the lot again:
47. Set oFolder = oFSO.GetFolder(sFolder)
48. For Each oFldr In oFolder.SubFolders
49. For Each oSubFldr In oFldr.SubFolders
50. line = line & oFolder.Path & "\" & oFldr.Name _
51. & "\" & oSubFldr.Name & "/"
52. Next
53. Next
53a. msgbox "Line=" & line
54.
55. aAux = Split(line, "/")
56. ReDim aFldrs(UBound(aAux)-1, 2)
57. For i = 0 To UBound(aAux)-1
58. aFldrs(i, 0) = Left(aAux(i), Len(aAux(i)) - 8)
59. aFldrs(i, 1) = LCase(Right(aAux(i), 8))
60. Next
61. End Sub

I introduced code line 53a into the existing code. It shows you the value of
the variable "Line". As you will see, it contains the names of all
subdirectories, with a forward slash between each element.

Code line 55 will create the array aAux, containing the various subdirectory
names found in the variable "Line".

Code line 56 will create a two-dimensional array. Its second dimensioin is
2. The first dimension is the number of subdirectories included in the
variable "Line". As a gentle exercise you might try to find out why the code
is
ReDim aFldrs(UBound(aAux)-1, 2) instead of
ReDim aFldrs(UBound(aAux), 2)
Hint: Have a look at the very last element of aAux.

The loop in code lines 57 to 60 will populate the two-dimensional array. To
see what's inside the array, you should add the following lines of code:
62. for i = 0 to UBound(aAux) - 1
63. wscript.echo "Element0=" & aFldrs(i,0) & ", Element1=" & aFldrs(i,1)
64. msgbox "Click OK to continue"


My System SpecsSystem Spec
Old 12-11-2008   #7 (permalink)
Thomas Niemann


 
 

Re: please help with folder search and copy

Thx again for the explenation!

Another Task:
í have seen that there are some/too much files where the search-string is
within the filename, not at the beginning. Sample:
RA-12074_11001092_abc.idw
The interesting part is the Number between the two underlines.
or
Technik 11004052 18.05.04.doc
The interesting Part is the number between the two spaces
How could the be implemented?

every filename starts with 1100 or 1200 but a filename like 11001100 or
11001200 could be possible...

Can you help me again, please?

I will add you to my personal hall of heroes!

My System SpecsSystem Spec
Old 12-11-2008   #8 (permalink)
Pegasus \(MVP\)


 
 

Re: please help with folder search and copy


"Thomas Niemann" <Niemann.Thomas@xxxxxx> wrote in message
news:494102f2$0$31865$9b4e6d93@xxxxxx-online.net...
Quote:

> Thx again for the explenation!
>
> Another Task:
> í have seen that there are some/too much files where the search-string is
> within the filename, not at the beginning. Sample:
> RA-12074_11001092_abc.idw
> The interesting part is the Number between the two underlines.
> or
> Technik 11004052 18.05.04.doc
> The interesting Part is the number between the two spaces
> How could the be implemented?
>
> every filename starts with 1100 or 1200 but a filename like 11001100 or
> 11001200 could be possible...
>
> Can you help me again, please?
It would be useful to summarise your initial requirement.
1. You have a source folder with some subfolders that contains
some files.
2. The file names start with an 8-character string such as 12345678.
3. You have a target folder that contains some subfolders and
subsubfolders. The names of some of them also start with the
above 8-character string.
4. You wish to copy all files from the source folder to one or
several target folders, but only when you achive a match
between file name and folder name (first 8 chars).

I consider the idea a fairly far-fetched scheme and I admit that I don't see
the purpose of it. Your new request makes the whole thing a lot more
complex: You no longer focus on the first 8 characters of your file names
when looking for a match but on a string of 8 characters that could occur
anywhere in the file name. This probably means that you have to turn the
whole code on its head: Take each target folder name in turn, then scan all
source file names to see if you can find a match *anywhere* in the name.

Perhaps one of the other respondents in this newsgroup will offer to spend
the time required to write/debug/test the necessary code.


My System SpecsSystem Spec
Old 12-12-2008   #9 (permalink)
Thomas Niemann


 
 

Re: please help with folder search and copy

Quote:

> It would be useful to summarise your initial requirement.
> 1. You have a source folder with some subfolders that contains
> some files.
right
Quote:

> 2. The file names start with an 8-character string such as 12345678.
at first i thought this. But later i´ve seen that there are much files with
the importand numberpart somewhere within the name.
Quote:

> 3. You have a target folder that contains some subfolders and
> subsubfolders. The names of some of them also start with the
> above 8-character string.
the structue is like
customer\artikel\number\one of 4 subfolders
The one of the four subfolders can be coded fixed in the script. The
importend thing was to search for the right "artikel" folder (the 8 Char.
String)
This part is solved yet
Quote:

> 4. You wish to copy all files from the source folder to one or
> several target folders, but only when you achive a match
> between file name and folder name (first 8 chars).
The desired folder name is the 8 chars(number) part of the filename
This part is solved yet

Quote:

> You no longer focus on the first 8 characters of your file names when
> looking for a match but on a string of 8 characters that could occur
> anywhere in the file name.
right....

i think it wold be possible for me to edit all files by a bulk rename
utility bevore running the script.
Would it be easier to implement if i do make all files look like
randomblabla_11001234randomblabla.ext ?
So the searched numberstring would begin with "_1100".
The 1100 is always the half part of the searched number.
Quote:

> Perhaps one of the other respondents in this newsgroup will offer to spend
> the time required to write/debug/test the necessary code.
I am afraid not (because of no response for some time :-( )
And i think you are the best (MVP!!! - Wow..)

If this is too much work for free (thank you for helping me so much),
please email me (niemann.thomas@xxxxxx) so we can talk about the price...

My problem is that i do not have much time to learn vbs this deep...
Quote:

>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Context Menu - Add COPY TO FOLDER and MOVE TO FOLDER Tutorials
Copy search results with full path Vista General
Please help with folder search and copy script VB Script
Re: Copying CD files to folder, then copy folder to another hard drive, explorer hangs Vista file management
Search and Smart search from a folder Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46