Solved

Select Random percentage of Text Files

Posted on 2011-03-24
6
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

751 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