Forum Index

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shuffling A Text File

Post new topic   Reply to topic Forum Index -> Scripting
View previous topic :: View next topic  
Author Message
Site Admin

Joined: 26 Apr 2004
Posts: 356

PostPosted: 2005-05-17 6:36 am    Post subject: Shuffling A Text File Reply with quote

This thread could be used to post other implementations of this (fairly) simple task in other languages (Awk, Perl, Python, C++, VBScript, etc), but I chose to implement this using Windows Scripting via JScript:


 * shuffle.js
 * Copyright (c), 2005, Jeff Schiller
 * Shuffles lines in a text file (good for playlists).  The script
 * can be scheduled in windows Task Schedule by running the file:
 *    cscript.exe //nologo shuffle.js text_file_to_be_shuffled
 * NOTE:  This script overwrites the original file and thus,
 *        should be considered DANGEROUS.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

function die(err) {
function shuffleArray(arr, iters) {
    var times = parseInt(iters);
    var N = arr.length;
    var temp, first, second;
    for(var loop = 0; loop < times; ++loop) {
        first = parseInt(Math.random() * N);
        while( (second = parseInt(Math.random() * N)) == first) { }
        temp = arr[first];
        arr[first] = arr[second];
        arr[second] = temp;

var objArgs = WScript.Arguments;
var objShell = WScript.CreateObject("WScript.Shell");
if(!objArgs || !objShell) {
    die("Could not obtain a WshShell object");

// validate arguments
if(objArgs.length != 1) {
    die("You must supply a text file to shuffle");

var sFilename = objArgs(0);
// Instantiate a File System ActiveX Object:
var fso = new ActiveXObject("Scripting.FileSystemObject");
if(!fso) {
    die("Could not create a FileSystemObject");

var indArr = new Array();
var textArr = new Array();

// 1 is for ForReading, 2 is for Writing
var theFile = fso.OpenTextFile(sFilename, 1);
if(!theFile) {
    die("Invalid filename '" + sFilename + "'");
while(!theFile.AtEndOfStream) {
    indArr[indArr.length] = indArr.length;
    textArr[textArr.length] = theFile.ReadLine();

shuffleArray(indArr, 30*indArr.length);

// now rewrite the file
theFile = fso.OpenTextFile(sFilename, 2, true);
for(loop = 0; loop < textArr.length; ++loop) {
    theFile.WriteLine( textArr[indArr[loop]] );


You must have Windows Scripting 5.6 ([url=]download it here[/url).

Use it like so:

cscript //nologo shuffle.js file_to_shuffle.txt
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Forum Index -> Scripting All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group