Solved

Select Random percentage of Text Files

Posted on 2011-03-24
6
319 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 56

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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 56

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 56

Expert Comment

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

~bp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

630 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