Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and drop batch file for multiple files?

Tags:

batch-file

I am hoping to accomplish something similar to the below, but with pcutmp3:

Drag and drop batch file for multiple files?

I'm having trouble wrapping my head around the additions Joey made as I don't usually do this, but I am wanting to drop multiple files (.cue) on a batch file and have it run more than once, which is what is happening with the following lines in the batch file:

@echo off
title pcutmp3
cd /d "F:\pcutmp3"
java -jar pcutmp3.jar --cue %1 --dir "F:\Test"
pause
exit

I've tried adapting Joey's code... but to no avail (I have no clue what I'm doing)

Thanks in advance for any help!

like image 811
ar4s Avatar asked Mar 03 '11 18:03

ar4s


2 Answers

@echo off
title pcutmp3
cd /d "F:\pcutmp3"

:again
if "%~1" == "" goto done

java -jar pcutmp3.jar --cue "%~1" --dir "F:\Test"

shift
goto again

:done
pause
exit

This is your basic "Eat all the arguments" loop. The important part is the shift keyword, which eats %1, and shifts all the arguments down by one (so that %2 becomes %1, %3 becomes %2, etc)

So, if you run it like so:

pcutmp3.bat a b c

It will call java like so:

java -jar pcutmp3.jar --cue "a" --dir "F:\Test"
java -jar pcutmp3.jar --cue "b" --dir "F:\Test"
java -jar pcutmp3.jar --cue "c" --dir "F:\Test"
like image 160
Mike Caron Avatar answered Nov 01 '22 00:11

Mike Caron


Dealing with %1, shift or %* could fail with drag&drop, because the explorer is not very smart, when it creates the command line.

Files like Cool&stuff.cue are not quoted by the explorer so you get a cmdline like
pcutmp3.bat Cool&stuff.cue

So in %1 is only Cool even in %* is only Cool, but after the pcutmp3.bat ends, cmd.exe tries to execute a stuff.cue.

To handle with this stuff you could use this, it catch all filenames by using the cmdcmdline variable.

@echo off
setlocal DisableDelayedExpansion
set index=0
setlocal EnableDelayedExpansion

rem *** Take the cmd-line, remove all until the first parameter
rem *** Copy cmdcmdline without any modifications, as cmdcmdline has some strange behaviour
set "params=!cmdcmdline!"
set "params=!params:~0,-1!"
set "params=!params:*" =!"
echo params: !params!
rem Split the parameters on spaces but respect the quotes
for %%G IN (!params!) do (
    for %%# in (!index!) do (
        endlocal
        set /a index+=1
        set "item_%%#=%%~G"
        setlocal EnableDelayedExpansion
    )
)

set /a max=index-1

rem list the parameters
for /L %%n in (0,1,!max!) DO (
  echo %%n #!item_%%n!#
)
pause

REM ** The exit is important, so the cmd.exe doesn't try to execute commands after ampersands
exit

Btw. there is a line limit for drag&drop operations of ~2048 characters, in spite of the "standard" batch line limit of 8191 characters.

like image 34
jeb Avatar answered Nov 01 '22 01:11

jeb