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!
 
You will get that error if the 'Asbestos' folder is missing (or misspelled). Try the following code to test if the folder exists and log an error if it does not.

Code:
Option Explicit
dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
dim objShell
set objShell = createobject("Wscript.Shell")

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
dim logFile
Set logFile = objFSO.CreateTextFile(schemeFolder & "errorlog.txt", True)

'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)
if objFSO.FolderExists(newpath) then
objFSO.CopyFolder objSubFolder, newpath & "\"
else
LogFile.WriteLine("Error: " & newpath & " does not exist")
end if

end if

end if

next

next
LogFile.Close
objShell.Run schemeFolder & "errorlog.txt"

wscript.echo("Done")
 
Hi - thanks for that but just checked and the folder(s) were spelled correctly - they were all created by copying and pasting from the same "template" or layout of folders so they will all be identically named.

Lost to why this is happening then???

Ta.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top