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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need advice for file copy script

Status
Not open for further replies.

dburrows1278

Technical User
Jan 25, 2008
13
US
I've got a job that copies select files to a particular folder on a daily basis. Our requirements have recently changed in such a way that I need these files split up into 2 folders instead of just 1.

Usually there are some 80,000 files and I need a way to copy half of them into 1 folder and the other half into another. I was running in the direction of a loop, but I'm just not figuring it out...

Any ideas???
 
dburrows1278,
how are you going define which file goes to which folder?
based on that in your for files area put a contional if then else.
regards,
longhair
 
thanks for the reply, longhair -

I was thinking of that but my concern is it seems really resource intensive. My initial thought was to decide based on filesize since they range pretty evenly between 10-80k but I started 2nd guessing that option.

I'm really curious if there's a way to have it count the files it's copying and say copy to Folder1 up to 50000 files and then copy the rest to Folder2. I was playing around with loops but they just restart the copy at the beginning so it hasn't worked.
 
What have you tried so far and where in your code are you stuck ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is what I'm playing with:

Code:
Dim fso, strMonth, strDay,strComputer, strFolder,intRC,strSQLString,errResults, counter

Set fso = CreateObject("Scripting.FileSystemObject")

if len(datepart("m",date-1) ) = 1 then
	strMonth = "0" & datepart("m",date-1)
else
	strMonth = datepart("m",date-1)
end if

if len(datepart("d",date-1) ) = 1 then
	strDay = "0" & datepart("d",date-1)
else
	strDay = datepart("d",date-1)
end if

'****Create destination folder
fso.CreateFolder ("\\server\destination\"  & datepart("yyyy",date-1) & strMonth  & strDay & "-1")
fso.CreateFolder ("\\server\destination\"  & datepart("yyyy",date-1) & strMonth  & strDay & "-2")

'****Copy all images
For i = 1 To NumOfFiles
  If i Mod 2 = 0
	fso.CopyFile  "\\server\source\" & datepart("yyyy",date-1) & strMonth  & strDay & "\*_18003*.img","\\server\destination\"  & datepart("yyyy",date-1) & strMonth  & strDay & "-1"
  Else
	fso.CopyFile  "\\server\source\" & datepart("yyyy",date-1) & strMonth  & strDay & "\*_18003*.img","\\server\destination\"  & datepart("yyyy",date-1) & strMonth  & strDay & "-2"
End If
 
so what is not working?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Sorry, should have stated - it's not copying anything. I can comment out everything but 1 of the copy statements and then it will copy...
 
I should say also, the directories will create and no errors will be given.
 
Where does NumOfFiles come from?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Good catch. Adjusted, and the entire script runs without error EXCEPT instead of copying to alternating folders (trying to get it to copy one file to the -1 folder and the next file to -2 and so on) it's copying everything to each folder... Here's the adjusted code:

Code:
'Function Main()
Dim filesys, folder, files, numoffiles, strMonth, strDay, strComputer, strFolder, intRC, strSQLString, errResults

'Main = DTSTaskExecResult_Success
const overwriteexisting = true
Set filesys = CreateObject("Scripting.FileSystemObject")
set folder = filesys.getfolder("c:\testing\testing\")
set files = folder.files
NumOfFiles = files.count

if len(datepart("m",date-1) ) = 1 then
	strMonth = "0" & datepart("m",date-1)
else
	strMonth = datepart("m",date-1)
end if

if len(datepart("d",date-1) ) = 1 then
	strDay = "0" & datepart("d",date-1)
else
	strDay = datepart("d",date-1)
end if

'****Create destination folder
filesys.CreateFolder ("c:\Vangent\20080122" & "-1")
filesys.CreateFolder ("C:\Vangent\20080122" & "-2")

'****Copy all files
For i = 1 To NumOfFiles
	If i Mod 2 = 0 Then
		filesys.CopyFile "C:\testing\testing\*.img" , "C:\testing\20080122-1\", OverwriteExisting
	Else
		filesys.CopyFile "C:\testing\testing\*.img" , "C:\testing\20080122-2\", OverwriteExisting
End If

'End Function
Next
 
This line:

filesys.CopyFile "C:\testing\testing\*.img" , "C:\testing\20080122-1\", OverwriteExisting

says "copy every file in C:\testing\testing\ to the backup folder. Usinf the * wildcard means "Every file".

Change the logic so you iterate through every file in the files collection (pseudo-code):

i=1
For Each oFile in Files
If i Mod 2 = o Then
oFile.copy Destination 1
Else
oFile.Copy destination 2
End If
i = i + 1
Next

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Tip: instead of using strMonth, strDay, DatePart ...
strDest = Year(Date-1) & Right("0" & Month(Date-1),2) & Right("0" & Day(Date-1),2)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Looks like that did it. So much thanks!!!! This is a huge life-saver. I'm a total n00b at scripting so thanks for your patience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top