11

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!

Community
  • 1
  • 1
ar4s
  • 111
  • 1
  • 1
  • 3

2 Answers2

10
@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"
Mike Caron
  • 14,351
  • 4
  • 49
  • 77
  • Hi Mike, thanks for your help! I was running the batch by simply dropping .cue files on top of it. After trying what you said I'm simply getting `/dir_path/test.cue"" unexpected at this time` Not exactly sure why...? Thanks again! – ar4s Mar 03 '11 at 19:35
  • My bad! I forgot that Explorer will quote file names. Basically, it was going `if ""somefile\blahbah"" == ""`, and it wasn't liking all the extra quotes. I've updated the script to strip the quotes properly. Also, with this change, the batch file is no longer DOS compatible :( – Mike Caron Mar 03 '11 at 19:40
  • ok, yes this definitely worked. I appreciate your help hugely with this. Cheers! – ar4s Mar 04 '11 at 11:56
10

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.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    [I filed a report regarding quoting files with `&` in their names](https://aka.ms/AA21ws3). Hopefully someone will see it and fix – phuclv Aug 16 '18 at 10:56
  • Hi jeb, is it possible to expand `%%n` to get the path? Like so: `%%~pn` I tried that and it returns: `ECHO is off.` Here's what I tried `echo !item_%%~pn!` – Ste Jan 23 '20 at 12:09
  • @StephenSherry It's possible to expand `%%~pn` but in this code `%%n` contains numbers only, therefore a path doesn't make sense. But you probably want to get the path of the item. But better ask a new question with a reference to this question/answer when useful – jeb Jan 23 '20 at 12:19
  • This works great until I drag a file with a ! (exclamation mark). Does anyone have a good way to allow ! in the dragged file name? – JG1212 Mar 13 '21 at 02:45
  • 1
    @JG1212 I add exclamation support – jeb Mar 13 '21 at 12:46
  • @jeb This revision works perfectly with both & and !. You are amazing!! – JG1212 Mar 16 '21 at 16:06
  • Great solution but doesn't work if the file has `,`, `;` or `=` (unless the filename has spaces, in which case it will be quoted and those characters won't act as delimiters). – Bangaio Oct 27 '22 at 23:55
  • 1
    @Bangaio Yes, this is an effect of the splitting at `for %%G IN (!params!) do `. You can solve it with the AutoRun.bat from [Handling ; (semi-colon) in filenames with this drag and drop routine](https://stackoverflow.com/a/59974834/463115) – jeb Oct 28 '22 at 04:46