Jump to content

Need help with Windows batch file script


Recommended Posts

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.>>

#!/bin/bash

pls=/tmp/$RANDOM.pls

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.it/mplayer-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.

I:\
chcp 65001
set PLS=%RANDOM%
echo %~1 >> %PLS%.pls
dir /b /on | findstr /v /i "%~1" >> %PLS%.pls
mplayer -playlist %PLS%.pls
chcp 437
del *.pls
pause

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.

Link to comment
Share on other sites


So you want to set the 'working' directory to that of the input media file:

PUSHD %~dp1

Then set your temporary playlist name:

SET PLS="%RANDOM%.PLS"

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%

MPLAYER -PLAYLIST %PLS%

Delete used playlist:

DEL %PLS%

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...