0

I have bought an USB key that can read MP3s. Sadly, it doesn't have a "random" mode, so it's always reading music in the same order. I would like to randomize this "easily". For that, I decided to, regularly, replace each file's name by the hash of its previous file name (and append .mp3, in order not to confuse the player).

I am used to bash, not batch, but I tried a few things. Here is how far I can get:

for %f in (.\*.mp3) do for /F %i in ('echo %f | hashsum') do @echo move %f %i.mp3

A few notes :

  • I couldn't find how to use batch's certutil to generate the hash of a random string, so I'm using hashsum
  • This uses one % instead of two, because it's not yet in a batch script
  • The second for loop comes from here
  • This doesn't do what I expect. I'm getting the error that the pipe (|) was unexpected.

I'm sure I'm getting close, but two points are still obscure:

  1. Can't certutil help me generate the hash of a string? (without using temporary files)
  2. How can I pass the output of a (piped) command as the argument of another command?
aschipfl
  • 33,626
  • 12
  • 54
  • 99
fzd
  • 765
  • 1
  • 6
  • 19
  • 2
    I'd have thought that just prepending each name with a defined number of `%random%` digits would randomise the names for playing purposes. e.g. rename `walk.mp3` `[8756]walk.mp3`. Then you could just replace the prepended random string as frequently as you needed to change the play order. e.g. replace all numeric strings between brackets with new random numbers. _This way you'd still be able to read the actual music names too!_ – Compo May 04 '18 at 18:22
  • 2
    Your command looks ok, you just have to escape the vertical bar `^|` like this `for %f in (.\*.mp3) do for /F %i in ('echo %f ^| hashsum') do @echo move %f %i.mp3` BTW Compo's suggestion has the charme that you still know what title is playing. –  May 04 '18 at 18:29
  • 1
    No, `certutil` is not capable of generating the has of a string unfortunately... – aschipfl May 04 '18 at 19:05
  • @Compo That's indeed a possibility, but as I'm not familiar at all with batch, search-and-replace methods are a bit out of reach for now. But it would indeed be a good idea. – fzd May 05 '18 at 06:49
  • Well I could have helped you with a script which would do that, but you didn't give me that opportunity, because you accepted an answer on the same visit as you posted the above comment. _Was it reasonable that you took 13 hours to respond to my comment, but I'm given no time at all to respond to yours!_ – Compo May 05 '18 at 10:37

1 Answers1

1

How about

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=1*delims=" %%a IN (
 'dir /b /a-d "%sourcedir%\*.mp3" '
 ) DO ren "%sourcedir%\%%a" "!random!.mp3" 2>nul
)

GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

This sets a variable containing the required directoryname then renames every .mp3 file in that directory to a random number.mp3 which avoids the hashing completely.

If the rename attempts to rename a file to an existing name, the 2>nul suppresses the error message - but the sequence should still be random after the procedure is run.

Magoo
  • 77,302
  • 8
  • 62
  • 84