Solved

Select Random percentage of Text Files

Posted on 2011-03-24
6
317 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 54

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

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 54

Expert Comment

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

~bp
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

679 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