Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Need help with Windows batch file script

- - - - -

  • Please log in to reply
3 replies to this topic


  • Member
  • 4 posts
  • Joined 17-April 12
  • OS:Windows 7 x64
  • Country: Country Flag
Hello. I'm attempting to create a Windows batch script that would mimic the functionality of the following Linux bash script I found through google. As I have no experience with building batch scripts, could someone convert this to something Windows compatible, or help me with the one I've started?
<<Please no Cygwin, or other *nix style Windows environment suggestions as I dual boot. If it's a Windows command line app, I'm open to installing it for the desired results.>>



echo "$1" >$pls && find $(dirname "$1") -maxdepth 1 |grep -v "$1" |shuf >>$pls

mplayer -playlist $pls

rm $pls

I downloaded the Windows 'Intel Nehalem' version of mplayer from here (http://oss.netfarm.i...layer-win32.php) . I extracted the folder into my program files directory, and added the directory to my system path.

Although I've attempted to do it myself, this is the furthest I've gotten, and it has several major flaws.

chcp 65001
echo %~1 >> %PLS%.pls
dir /b /on | findstr /v /i "%~1" >> %PLS%.pls
mplayer -playlist %PLS%.pls
chcp 437
del *.pls

The flaws being...

- 1. I have to hardcode the location of the media files. In the above case 'I:'. I tried using the 'pushd' command, but it opens the location of my batch file, and not the location of the media file used to launch the script.

- 2. I have no clue how to shuffle the order of the lines (ie - the file names) with any of the default Windows command line tools.

- 3. Windows 'dir' command doesn't seem to have a way to omit directory names, so the script ends up trying to play files with those directory name. This prevents me from skipping backwards in the shuffle as it cannot find those file names

- 4. I probably need to use the 'type' command to merge the echoed file name (used to launch script) dumped to playlist 1 followed by a playlist 2 with the '%1' file name omitted as on line 4, but I'm unsure how to do this with the variables. This is so I can have my playlist shuffled, but have the initially launched file play first.

How to remove advertisement from MSFN



    Update Packrat

  • Member
  • PipPipPipPipPipPip
  • 1,353 posts
  • Joined 04-July 05
  • OS:XP Pro x64
  • Country: Country Flag


Don't have much time here, but I'll give you a couple hints:


3. DIR /A-D



    Wise Owl

  • Super Moderator
  • 4,078 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag


So you want to set the 'working' directory to that of the input media file:
PUSHD %~dp1
Then set your temporary playlist name:
It appears you want your playlist but with the input file at the top:
ECHO %~nx1 1>%PLS%
Now it looks as if you want all files in that directory except for the one already chosen and any created playlist:
DIR /B /A-D | FINDSTR /E /V /I /C:"%~nx1" /C:".PLS" 1>>%PLS%
Play the playlist from MPlayer, already in %PATH%
Delete used playlist:

As you say, there is no direct way of shuffling/randomising lines within a file, it can be done but is not a straight forward solution. The simplest solution would be to sort your DIR output using the /S switch which will order according to file size. (In most cases that should be sufficient to randomise the playlist):

replacement line

DIR /B /A-D /O-S | FINDSTR /E /V /I /C:"%~nx1" /C:".PLS" 1>>%PLS%

I hope this helps you out a little.


  • Member
  • 4 posts
  • Joined 17-April 12
  • OS:Windows 7 x64
  • Country: Country Flag
Cool. Thanks for the help people.

These are all the step I took to get this working.

1. Extracting, moving, and adding the Mplayer folder location to my PATH variable.

2. Creating the batch script (mps.bat), and placing it the Mplayer folder.

3. Borrowing the code from HERE for shuffling text files using the default installed Windows Scripting, and placing it in the Mplayer folder.

4. Creating a shortcut to this batch file, choosing an icon for it, and placing it into my 'SendTo' (%APPDATA%\Microsoft\Windows\SendTo) folder.

And here are the batch file contents.

set mp="c:\program files\mplayer"
set pls="%random%"
chcp 65001
pushd %~dp1
echo %~nx1 1>%pls%.m3u
dir /b /a-d /o-s | findstr /e /v /i /c:"%~nx1" /c:".pls" /c:".m3u" 1>>%pls%.pls
cscript //U //nologo %mp%\shuffle.js %pls%.pls
type %pls%.pls 1>>%pls%.m3u
chcp 437
mplayer -fs -playlist %pls%.m3u
del %pls%.*

I have this setup for file-names needing uni-code (ie - cyrillic, japanese, etc.). I didn't know how to launch batch files with the (cmd /u) setting, so I used the quick and dirty code page method. I also added the cscript '//U' setting to jscript file. If you don't require uni-code file-name support you can omit the double then merged playlist method. This script also works from the command line via "mps [TAB COMPLETION]".

Edited by VipT, 02 May 2013 - 08:41 PM.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users