Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


need help moving set of files randomly

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,36199
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> need help moving set of files randomly
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 need help moving set of files randomly - 7/14/2006 12:07:49 AM   
  blakeph

 

Posts: 17
Score: 0
Joined: 5/28/2006
Status: offline
hi everyone!

I have here a script that will move a set of files to destination folder, the source folder contains only a thousands of "*.txt" files, but is there any other way to move only a total of six randomly selected "*.txt" files from the source folder to destination folder, everytime I run the script?
Please find below my script:

      
Thank you for any help.
 
 
Post #: 1
 
 RE: need help moving set of files randomly - 7/14/2006 12:26:19 AM   
  Streamer

 

Posts: 7
Score: 0
Joined: 7/13/2006
Status: offline
Try the following:


Dim anArray(2)
anArray(0) = "text1.txt"
anArray(1) = "text2.txt"
anArray(2) = "text3.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
For i = 0 to 2
  objFSO.MoveFile ""D:\Selections\" & anArray(i), "E:\Archive\"
Next

(in reply to blakeph)
 
 
Post #: 2
 
 RE: need help moving set of files randomly - 7/14/2006 1:46:12 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
That would move all the files listed. To move a subset of the files you would need to:

Create an array of the file names
get a random number between 0 and the UBound of the array
Move that file
Swap that item of the array with the last item in the array
Redim preserve the array to one less than it currently is
repeat for a many files as you want to move

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Streamer)
 
 
Post #: 3
 
 RE: need help moving set of files randomly - 7/14/2006 2:54:48 AM   
  Streamer

 

Posts: 7
Score: 0
Joined: 7/13/2006
Status: offline
You might want to extract all filenames from a specific folder.

   Dim objFileScripting, objFolder
   Dim filename, filecollection, strDirectoryPath
    strDirectoryPath="D:\Selections\"
    'get file scripting object
    Set objFileScripting = CreateObject("Scripting.FileSystemObject")
    'Return folder object
    Set objFolder = objFileScripting.GetFolder("D:\Selections\")
    'return file collection In folder
    Set filecollection = objFolder.Files
    
    'extract each filenames
    For Each filename In filecollection
          Filename=right(Filename,len(Filename)-InStrRev(Filename, "\"))
    
          msgbox filename                 '  <== replace this and move value to an Array
 
    Next

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: need help moving set of files randomly - 7/14/2006 5:57:34 AM   
  blakeph

 

Posts: 17
Score: 0
Joined: 5/28/2006
Status: offline
hello!

thank you for replying guys, so far, i got the following, am still figuring out the redim preserve:

      

(in reply to Streamer)
 
 
Post #: 5
 
 RE: need help moving set of files randomly - 7/14/2006 6:31:15 AM   
  ebgreen


Posts: 4970
Score: 31
Joined: 7/12/2005
Status: offline
This should get you started. You will want to add some basic validation and error handling, but it should basicly do what you want it to:


      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to blakeph)
 
 
Post #: 6
 
 RE: need help moving set of files randomly - 7/14/2006 6:46:18 AM   
  blakeph

 

Posts: 17
Score: 0
Joined: 5/28/2006
Status: offline
EXECELLENT! thank you so much!:)

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: need help moving set of files randomly - 7/14/2006 9:27:24 AM   
  ehvbs

 

Posts: 2172
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Remebered from a statistic class very long a ago:


      

No array needed and you don't have to work thru the whole folder in most cases.

(nSelect/nFiles will be 1 (=> file will be picked surely), if there just a many files left as
you still need)

< Message edited by ehvbs -- 7/14/2006 9:28:27 AM >

(in reply to blakeph)
 
 
Post #: 8
 
 RE: need help moving set of files randomly - 7/15/2006 1:44:11 PM   
  DiGiTAL.SkReAM


Posts: 1183
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
I'll excercise my Dictionary fetish, and even though ebgreen has already answered, I'll post this.  At least it's a different angle.

[code]
Set oDic = CreateObject("Scripting.Dictionary")
For Each file In CreateObject("Scripting.FileSystemObject").GetFolder("c:\working").Files
 oDic.Add oDic.Count,file
Next
Randomize
For i = 1 To 6
 x = Int(((oDic.Count - 1) - 0 + 1) * Rnd + 0)
 CreateObject("Scripting.FileSystemObject").GetFile(oDic.Item(x)).Move "c:\temporary1\"
 oDic.Remove(x)
Next
[/code

I know that I should have been able to do this same function in 3 or fewer lines, but I guess I'm just wordy.
And frankly, I really like the functionality of the dictionary - being able to remove a key/item pair and have all the counts auto-adjust makes it very handy for situations like this.


_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: need help moving set of files randomly - 7/15/2006 11:18:25 PM   
  ehvbs

 

Posts: 2172
Score: 50
Joined: 6/22/2005
From: Germany
Status: online
Studying ebgreen's and DiGiTAL.SkReAM's approaches to pick random elements
from an array resp. a dictionary I'd like to point out two possible improvements:

When you create a collection of all files in the folder - which could come handy
if you want to do some other processing - it's a pity to do a destructive
operation on the collection to get your ramdom sample. To avoid this for the
array is easy - just use a variable to keep track of the (logical) upper bound
and forgo the (costly) ReDim Preserve:

  Randomize
  nUB = UBound(arrFiles)
  For i = 1 To nQty
    'Move a random file from the Array
     nElement = Int((nUB - 0 + 1) * Rnd)
     oFSO.MoveFile arrFiles(nElement), strDest
     arrFiles(nElement) = arrFiles(nUB)
     nUB = nUB - 1
  Next

The dictionary code would need an additional copy operation (instead of the .Remove()):

  Randomize
  nUB = UBound(arrFiles)
  For i = 1 To 6
     x = Int(((nUB - 1) - 0 + 1) * Rnd + 0)
     CreateObject("Scripting.FileSystemObject").GetFile(oDic.Item(x)).Move "c:\temporary1\"
     oDic.Item(x) = oDic.Item(nUB)
     nUB = nUB - 1
  Next
 
While pedantic in the context of the OT's problem (pick 6 from 'thousands' of files),
some guard against the conceivable case "Quantity > NumberOfFiles" would make the
solutions more robust.

Thanks to blakeph for an interesting problem and ebgreen and DiGiTAL.SkReAM for
stimulating strategies/code!
 

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> need help moving set of files randomly Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts