Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to copy multiple folders based on criteria? 1

Status
Not open for further replies.

JGKWORK

IS-IT--Management
Apr 1, 2003
342
0
0
GB
Hi - been asked to post this here instead of the XP/DOS section, hope I have more luck here? I don't know how to run VBScript so any pointers would be helpful!

We are facing a huge task at work to COPY thousands of folders in to a new folder structure.

The folders which need transfered are named:

\schemes\"001 Scheme name"
\schemes\"002 Scheme name"
\schemes\"003 Scheme name"

etc etc

Each of these folders need to be transfered in to their respective "Asbestos" folders shown in the path below:

\Properties\001 Random name\Asbestos
\Properties\002 Random name\Asbestos
\Properties\003 Random name\Asbestos

So as you can see (hopefully) I need to be able to transfer the, for example "001 Scheme name" folder in to an "Asbestos" folder that sits within a folder that contains the same scheme number e.g. 001, 002 etc.

So the final folder path for each scheme would look like this:

\Properties\001 Random name\Asbestos\001 Scheme name
\Properties\002 Random name\Asbestos\002 Scheme name
\Properties\003 Random name\Asbestos\003 Scheme name

So there needs to be some code which contains a wildcard to check for the scheme number e.g. move scheme folder in to "Asbestos" folder which is within folder like 001*

I think the logic would be (in a loop):

1.Return the folder name from the source directory and add it to a variable.

2.Extract the number from the folder name (only ever going to be one set of numbers in the file name).

3.Use this number to match against the folders in the destination folders and where it matches add that folder/file destination to a variable.

4. Run a copy command using the two variables from above.

Many thanks!
 
Is the number before "scheme name" always 3 digits?
 
Here's a quick script to get you started. Try it out on some test data.

Code:
Option Explicit
dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

	const schemeFolder = "C:\TEMP\schemes"
	const targetFolder = "C:\TEMP\Properties"
	dim objStartFolder
	dim objTargetFolder
	dim colSubFolders
	dim objSubFolder
	dim schemeNumber
	dim newpath
	
'make sure scheme folder exists
If not objFSO.FolderExists(schemeFolder) Then
	wscript.echo("invalid folder")
	wscript.quit
else
	Set objStartFolder = objFSO.GetFolder(schemeFolder)
end if

'make sure target folder exists and/or create it
If objFSO.FolderExists(targetFolder) then
	Set objTargetFolder = objFSO.GetFolder(targetFolder)
else
	wscript.echo(targetFolder & " does not exist")
	wscript.quit
	'or add code here to create it
end if
	
	Set colSubFolders = objStartFolder.SubFolders
	for each objSubFolder in colSubFolders
		schemeNumber = Left(objSubFolder.Name, 3)
		if IsNumeric(schemeNumber) then
			newpath = objFSO.BuildPath(targetFolder, schemeNumber & " Random name")
			newpath = objFSO.BuildPath(newpath, "Asbestos")
			'wscript.echo("schemeNumber = " & schemeNumber)
			'wscript.echo("objSubFolder = " & objSubFolder.Name)
			'wscript.echo("newpath = " & newpath)
			MakeDir(newpath)
			objFSO.MoveFolder objSubFolder, newpath & "\"
		end if
	next
	
wscript.echo("Done")

'**************************************************************************************************
'[URL unfurl="true"]http://www.windowsitpro.com/article/tips/jsi-tip-10441-how-can-vbscript-create-multiple-folders-in-a-path-like-the-mkdir-command-.aspx[/URL]
Function MakeDir (strPath)
	Dim strParentPath, objFSO
  Set objFSO = CreateObject("Scripting.FileSystemObject")
	On Error Resume Next
	strParentPath = objFSO.GetParentFolderName(strPath)

  If Not objFSO.FolderExists(strParentPath) Then MakeDir strParentPath
	If Not objFSO.FolderExists(strPath) Then objFSO.CreateFolder strPath
	On Error Goto 0 
  MakeDir = objFSO.FolderExists(strPath)
End Function
 
Many thanks for the reply - sorry, yes it will always be 3 digits.

Will give this a run through, thanks.
 
Sorry for my ignorance but how do I run this - seem to be getting Active X errors Scripting .FileSystemObject?

Sorry never worked with this before.
 
Scratch that there was a space after the word "Scripting"!

Ok very excited because this is very nearly doing what I need it to!

Can I ask if anyone is able to put the finishing touches as I am a VBScript virgin?

Basically the script is moving (I need it to COPY) the folders (e.g. "001 Scheme name") in to a new folder structure and then moving that structure in to the Properties folder (at that level).

What I really need is for it to copy the "001 Scheme name" folder directly in to the asbestos folder that sits within the associated e.g. "001 random name" folder by comparing the first three digits.

Please help, very nearly there and running our of time!

Many thanks to everyone.
 
If it is the copy instead of move that is needed, how about changing this line?
>objFSO.MoveFolder objSubFolder, newpath & "\"
[tt]objFSO.CopyFolder objSubFolder.path, newpath & "\", true[/tt]
 
Hint of sarcasm?! hehe, yeh no worries I already changed that line while waiting for a response on the meaty stuff.

What I really need to be able to do now is to compare the original folder with the possible destination folders, I'm guessing but have got this far:

Set colSubFolders = objStartFolder.SubFolders
for each objSubFolder in colSubFolders
schemeNumber = Left(objSubFolder.Name, 3)

'My bit so far:
Set destSubFolders = objTargetFolder.SubFolders
for each objSubFolder in destSubFolders
if

here I need to be able to take the schemeNumber, and check if it is used in one of the destSubFolders - if it is I then need to return the full name of that folder so I can use it to construct the final output path.

I only need to transfer ONE scheme folder at a time to its correct path - so maybe a DO WHILE LOOP or something?

Any help really appreciated, thanks.
 
bit closer - maybe something like this? It's hard when you don't know the language!


Set destSubFolders = objTargetFolder.SubFolders
for each objSubFolder in destSubFolders
if InStr(destSubFolders,schemeNumber) <> 0 then
wscript.echo(destSubFolders)
end if
next
 
Ok I have the correct folder name being returned and have added it to the variable "destFolderName" how do I add this to the newpath (line of code below) as every time I try to add the "destFolderName" variable to this line it gives the following error: Object required String[name of the folder]

Any ideas?

Ta.

newpath = objFSO.BuildPath(targetFolder, "Asbestos")

___________________________________________________________


Set colSubFolders = objStartFolder.SubFolders
Set destSubFolders = objTargetFolder.SubFolders

for each objSubFolder in colSubFolders
schemeNumber = Left(objSubFolder.Name, 3)

for each objSubFolderDest in destSubFolders
if InStr(objSubFolderDest.Name, schemeNumber) <> 0 then
'wscript.echo(objSubFolderDest.Name)
set destFolderName = objSubFolderDest.Name

end if

next

if IsNumeric(schemeNumber) then
newpath = objFSO.BuildPath(targetFolder, "Asbestos")
'wscript.echo("schemeNumber = " & schemeNumber)
'wscript.echo("objSubFolder = " & objSubFolder.Name)
wscript.echo("newpath = " & newpath)
MakeDir(newpath)
objFSO.CopyFolder objSubFolder, newpath & "\"
end if
next
 
>set destFolderName = objSubFolderDest.Name
This cannot be right. Try this if it fits to the rest.
[tt]destFolderName = objSubFolderDest.Name[/tt]
 
Amazing what you can do when you put your mind to it eh?! it actually all works!!

Can't thank you enough guys,

Final script below for those who are interested (although still to do final testing):

Option Explicit
dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

const schemeFolder = "C:\TEMP\schemes"
const targetFolder = "C:\TEMP\Properties"
dim objStartFolder
dim objTargetFolder
dim colSubFolders
dim destSubFolders
dim objSubFolder
dim objSubFolderDest
dim schemeNumber
dim newpath
dim destFolderName

'make sure scheme folder exists
If not objFSO.FolderExists(schemeFolder) Then
wscript.echo("invalid folder")
wscript.quit
else
Set objStartFolder = objFSO.GetFolder(schemeFolder)
end if

'make sure target folder exists and/or create it
If objFSO.FolderExists(targetFolder) then
Set objTargetFolder = objFSO.GetFolder(targetFolder)
else
wscript.echo(targetFolder & " does not exist")
wscript.quit
'or add code here to create it
end if

Set colSubFolders = objStartFolder.SubFolders
Set destSubFolders = objTargetFolder.SubFolders

for each objSubFolder in colSubFolders

schemeNumber = Left(objSubFolder.Name, 3)


for each objSubFolderDest in destSubFolders


if InStr(objSubFolderDest.Name, schemeNumber) <> 0 then
'wscript.echo(objSubFolderDest.Name)
destFolderName = objSubFolderDest.Name



end if

next



if IsNumeric(schemeNumber) then
newpath = objFSO.BuildPath(targetFolder & "\" & destFolderName, "Asbestos")
'wscript.echo("schemeNumber = " & schemeNumber)
'wscript.echo("objSubFolder = " & objSubFolder.Name)
'wscript.echo("newpath = " & newpath)
MakeDir(newpath)
objFSO.CopyFolder objSubFolder, newpath & "\"
end if
next


wscript.echo("Done")

'**************************************************************************************************
'Function MakeDir (strPath)
Dim strParentPath, objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
strParentPath = objFSO.GetParentFolderName(strPath)

If Not objFSO.FolderExists(strParentPath) Then MakeDir strParentPath
If Not objFSO.FolderExists(strPath) Then objFSO.CreateFolder strPath
On Error Goto 0
MakeDir = objFSO.FolderExists(strPath)
End Function
 
P.S. jges - many many thanks for your script - really appreciated.
 
Sorry one last thing - how do I script it so that if it can't find a folder with the three numbers in it that it will skip to try copying the next folder?

At the moment it is putting the scheme folder in to the last destination folder it used.

Thanks.
 
[tt]'...etc etc

for each objSubFolder in colSubFolders
destFolderName=""
schemeNumber = Left(objSubFolder.Name, 3)
for each objSubFolderDest in destSubFolders
if left(objSubFolderDest.Name,3)=schemeNumber then
'wscript.echo(objSubFolderDest.Name)
destFolderName = objSubFolderDest.Name
exit for
end if
next

if IsNumeric(schemeNumber) and len(destFolderName)<>0 then
newpath = objFSO.BuildPath(targetFolder & "\" & destFolderName, "Asbestos")
'wscript.echo "schemeNumber = " & schemeNumber
'wscript.echo "objSubFolder = " & objSubFolder.Name
'wscript.echo "newpath = " & newpath
MakeDir newpath
objFSO.CopyFolder objSubFolder.path, newpath & "\"
end if
next

wscript.echo("Done")
[/tt]
 
I moved the section where the copy happens into the IF block of
Code:
if InStr(objSubFolderDest.Name, schemeNumber) <> 0
Try this:

Code:
Option Explicit
dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")

const schemeFolder = "C:\TEMP\schemes"
const targetFolder = "C:\TEMP\Properties"
dim objStartFolder
dim objTargetFolder
dim colSubFolders
dim destSubFolders
dim objSubFolder
dim objSubFolderDest
dim schemeNumber
dim newpath
dim destFolderName

'make sure scheme folder exists
If not objFSO.FolderExists(schemeFolder) Then
wscript.echo("invalid folder")
wscript.quit
else
Set objStartFolder = objFSO.GetFolder(schemeFolder)
end if

'make sure target folder exists and/or create it
If objFSO.FolderExists(targetFolder) then
Set objTargetFolder = objFSO.GetFolder(targetFolder)
else
wscript.echo(targetFolder & " does not exist")
wscript.quit
'or add code here to create it
end if

Set colSubFolders = objStartFolder.SubFolders
Set destSubFolders = objTargetFolder.SubFolders

for each objSubFolder in colSubFolders

schemeNumber = Left(objSubFolder.Name, 3)


for each objSubFolderDest in destSubFolders


if InStr(objSubFolderDest.Name, schemeNumber) <> 0 then
'wscript.echo(objSubFolderDest.Name)
destFolderName = objSubFolderDest.Name

if IsNumeric(schemeNumber) then
'newpath = objFSO.BuildPath(targetFolder & "\" & destFolderName, "Asbestos")
newpath = objFSO.BuildPath(targetFolder, destFolderName)
newpath = objFSO.BuildPath(newpath, "Asbestos")
'wscript.echo("schemeNumber = " & schemeNumber)
'wscript.echo("objSubFolder = " & objSubFolder.Name)
'wscript.echo("newpath = " & newpath)
'MakeDir(newpath)
objFSO.CopyFolder objSubFolder, newpath & "\"
end if


end if

next



next


wscript.echo("Done")
This will only copy the folder if a match is found. You may want to create some sort of output if a match is not found (eg add the name to a log file).
 
Works like a dream - brilliant many thanks.
 
Does anyone know if there is a limit on how many folders can be copied as the cript seems to die around 124 folders in?

Goves the error "cannot find then the file path" but it's been using that file path 124 times previously!

Tried it twice and died at same folder both times. Manually transfering is fine and when I delete up to 123 then run the script it carries on fine and does the rest.

Ta.

 
Are you sure there is no problem with the destination folders?

You might want to add a check to make sure the destination folder exists before you try to copy the 'scheme' folder.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top