Solved

Select Random percentage of Text Files

Posted on 2011-03-24
6
311 Views
Last Modified: 2012-05-11
I need to use VB or VBA in excel 2007 to parse a selected folder and choose a random percentage of the files within for QA. Those random files then need to be moved to a second folder for QA review. The selected filenames need to placed in a list within excel for logging purposes.

Is there an easy way to do this?
0
Comment
Question by:txdolfan
  • 3
  • 2
6 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35209784
So, you will specify the source folder name, the destination folder name, and the percentage of files to copy (selecting them randomly)?

~bp
0
 
LVL 4

Expert Comment

by:dagesi
ID: 35210936
I assume you mean to limit the percentage to a certain range...?  Otherwise, a "random percentage of the files" could be anywhere from 0 to 100%.  You sure you didn't mean "randomly select a percentage of the files"...?
0
 

Author Comment

by:txdolfan
ID: 35211882
Yes you will specify the source,destination, and percentage to be randomly selected. So no matter the total of the files I need to randomly select a given percentage, say 10%.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35212108
Okay, here's a vbscript approach that should be pretty close.  Save this file as a VBS file, and run like this:

cscript EE26909629.vbs "C:\EE26909629\from" "C:\EE26909629\to" "C:\EE26909629\log.csv" 10

This will select 10% of the files in "C:\EE26909629\from" randomly, copy them to "C:\EE26909629\to" and write their full paths to "C:\EE26909629\log.csv", which you can then open in Excel.

' Require variables to be defined before usage
Option Explicit

' Define some useful contants
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

' Define all used variables here
Dim oFSO, oArgs, oFolder, oFiles, oPath(), oFile, oLogFile
Dim iIndex, iPercent, iCopy
Dim sFromFolder, sToFolder, sLogFileName, sFileToCopy

' Initialize random number generator; create filesystem object
Randomize
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Make sure enough parms were supplied
Set oArgs = WScript.Arguments
If (oArgs.Count < 4) Then
  WScript.Echo "ERROR: Too few parm specified." & vbCRLF
  WScript.Echo "USAGE:"
  WScript.Echo "  " & Wscript.ScriptName & " [from-folder] [to-folder] [log-file] [percent]"
  WScript.Quit
End If

' Get and validate first parm (from folder path)
sFromFolder = oArgs(0)
If Right(sFromFolder, 1) <> "\" Then sFromFolder = sFromFolder & "\"
If Not oFSO.FolderExists(sFromFolder) Then
  WScript.Echo "ERROR: From folder does not exist."
  WScript.Quit
End If

' Get and validate second parm (to folder path)
sToFolder = oArgs(1)
If Right(sToFolder, 1) <> "\" Then sToFolder = sToFolder & "\"
If Not oFSO.FolderExists(sToFolder) Then
  WScript.Echo "ERROR: To folder does not exist."
  WScript.Quit
End If

' Get and validate third parm (CSV log file path)
sLogFileName = oArgs(2)

' Get and validate fourth parm (percent files to sample)
iPercent = oArgs(3)
If iPercent < 1 Or iPercent > 100 Then
  WScript.Echo "ERROR: percent to select is not between 1 and 100."
  WScript.Quit
End If

' Open CSV file for list of files selected
Set oLogFile = oFSO.OpenTextFile(sLogFileName, ForWriting, True)

' Load files from source folder into array for processing
Set oFolder = oFSO.GetFolder(sFromFolder)
Set oFiles = oFolder.Files
ReDim oPath(oFiles.Count)
iIndex = -1
For Each oFile in oFiles
   iIndex = iIndex + 1
   oPath(iIndex) = oFile.Path
Next

' Randomly select the percent files we want for audit check
For iCopy = 0 To (oFiles.Count * iPercent / 100) - 1
   sFileToCopy = ""
   Do
      iIndex = Int((oFiles.Count - 1) * Rnd)
      If oPath(iIndex) <> "" Then
         sFileToCopy = oPath(iIndex)
         oPath(iIndex) = ""
      End If
   Loop Until sFileToCopy <> ""
   oFSO.CopyFile sFileToCopy, sToFolder
   oLogFile.WriteLine sFileToCopy
Next

' Wrap-up
oLogFile.Close

Open in new window

~bp
0
 

Author Closing Comment

by:txdolfan
ID: 35212487
This solution worked perfectly and got me past my issue using VBA to move the files.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35212498
Great, glad that was useful, thanks.

~bp
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now