Why this Batch script can't handle multiple files ?

July 10, 2015 at 10:46:55
Specs: Windows 7
I need to use FFmpeg's Window$ port to convert FLAC files to MP3-320 by drag-dropping them onto the BAT file and it automatically makes MP3-320 to the very same folder as the FLAC and leaves the window open after it's finished. The script on the bottom of http://forum.doom9.org/showpost.php... (the first post) is the one I used, but replaced the §-q:a 5§ flag in both FFmpeg commands with §-b:a 320k§. When I drag-drop a single file, it works just fine, but when there are >1, it just prints the list of files with full path, but the text, no matter the count of files, doesn't end with double quotes although the last file in the line does start with a double quote as meant.

It's no secret Batch makes little sense, so it doesn't make to me either.

message edited by rautamiekka


See More: Why this Batch script cant handle multiple files ?

Report •


#1
July 10, 2015 at 18:41:58
First, post the script you're trying to run, with some sample file/folder names & paths.
I am guessing you want to drop a folder-name to the batch and have it process all files in that folder. If that is indeed the case, the batch needs to determine if the input is a single file or a folder, and if it's a folder, it needs to enumerate the files explicitly. (batch won't split a folder into it's component files automatically. you have to code for that.) By this: "but when there are >1, it just prints the list of files with full path", i am assuming you mean dropping a directory-name onto the batch. Here's a sample snippet that demonstrates what I feebly attempted to communicate:
@echo off & setlocal
if "%~a1" equ "d--------" (
:: process as a directory/folder feed, not a single file
echo %1 is a directory...
for %%a in (%1\*.*) do (
echo processing %1\file: -------- %%a
type "%%a"
pause
)
) else (
:: here is processes only a single file
type %1
)
pause

::-------- end snippet
I tested this, using drag/drop which I'm not used to using. Apparently, D/D automatically surrounds the space-sep word-group with quotes, since only one token is offered to the batch. Cmd requires explicit quotes to group space-separated words into one token, since multiple tokens are available to the batch from CMD. It's a big bunch of crock initiated by the introduction of spaces into file/folder-names which I continue to deplore and rant against.

message edited by nbrane


Report •

#2
July 11, 2015 at 10:56:37
I thought it's enough if I tell precisely which script with what modifications. And I said I want a long list of files to be processed that I drag-drop to the script. I see no reason why that is unclear.

Report •

#3
July 11, 2015 at 16:10:41

Report •

Related Solutions

#4
July 11, 2015 at 22:30:34
Ok, I get the drift. I can't run your app just to test, so I have substituted "type". This worked for multi-file drag-drop:

@echo off & setlocal
echo %*
echo ------------
pause
echo %1
echo %2
echo %3
pause

:xx
echo processing %1
:: substitute your executable instead of "type" for actual
type %1
pause
shift
if [%1] equ [] goto :eof
goto :xx
::====== end snippet
That's about it for me. Like I said, i'm GUI/drag/drop challenged. The short-filename came in with no quotes, long files with spaces had quotes.. good luck.

message edited by nbrane


Report •

#5
July 12, 2015 at 06:45:23
That is supposed to check for and then create if doesn't exist, MP3 of the FLAC file to same folder ?

Report •

#6
July 12, 2015 at 23:02:50
I got different signals from your material. The "base-line" scripts you provided were not viable nor apparent to me. I saw both file-lists and directories submitted for processing. So, ...
:: ======== begin batch
@echo off & setlocal
SET pathToFFmpeg="C:\_DOWNLOADS_\ffmpeg-20150710-git-033dc39-win64-static\bin\ffmpeg.exe"
:: any executable will serve the purpose. I just used "type" for simplicity.
SET pathToFFmpeg=type
echo %*
pause
:process
set d=%~a1
set d=%d:~0,1%
echo "%~1"
if %d% equ d (
:: process as a directory/folder feed, not a file-name-set
echo "%~1" is a directory...
pause
pushd "%~1"
for %%a in (*.*) do (
echo processing %1\file: -------- "%%~a"
if not exist "%%~dpna.mp3" %pathToFFmpeg% "%%~dpnxa">"%%~dpna.mp3"
pause
)
popd
) else (
:: here processes a single file
echo file-name process: "%~1"
pause
if not exist "%~dpn1.mp3" %pathToFFmpeg% "%~dpn1">"%~dpn1.mp3"
pause
)
shift
if [%1] neq [] goto :process
pause
::======== end script
I left lots of debugging in place (pauses and echos) for you to remove when appropriate. The script should handle multiple-directory as well as multiple single-file-list submissions. (at least, it worked in my tests with plenty of "spaces-in-filenames").

Report •

#7
July 13, 2015 at 08:47:28
Looks better now, but there's a small mistake in some spot cuz FFmpeg complains there's no suitable format for output cuz the file extension isn't mentioned: http://s284544448.onlinehome.us/pub...

Report •

#8
July 13, 2015 at 19:29:27
Probably this line:
if not exist "%~dpn1.mp3" %pathToFFmpeg% "%~dpn1">"%~dpn1.mp3"

should be:
if not exist "%~dpnx1.mp3" %pathToFFmpeg% "%~dpnx1"


message edited by nbrane


Report •

#9
July 14, 2015 at 09:16:28
http://s284544448.onlinehome.us/pub...

Just in case, the code: http://pastebin.com/rvvWHF2q and I pasted drag-dropped 2 files at once from different folders.

message edited by rautamiekka


Report •

#10
July 14, 2015 at 20:40:05
My mistake as usual. Pls see my previous response which I fixed. Maybe that's not the problem though. My script tried to submit the entirely qualified filename starting with drive-letter. I suggest simply "trapping" the input-stream:
@echo off >log & setlocal
echo simulated commandline stream will be sent to file 'log'.
SET pathToFFmpeg="C:\_DOWNLOADS_\ffmpeg-20150710-git-033dc39-win64-static\bin\ffmpeg.exe"
:: any executable will serve the purpose. I just used "type" for simplicity.
rem SET pathToFFmpeg=type
echo %*
pause
:process
set d=%~a1
set d=%d:~0,1%
echo "%~1"
if %d% equ d (
:: process as a directory/folder feed, not a file-name-set
echo "%~1" is a directory...
pause
pushd "%~1"
for %%a in (*.*) do (
echo processing %1\file: -------- "%%~a"
if not exist "%%~dpna.mp3" >>log echo %pathToFFmpeg% "%%~dpnxa">"%%~dpna.mp3"
pause
)
popd
) else (
:: here processes a single file
echo file-name process: "%~1"
pause
if not exist "%~dpn1.mp3" %pathToFFmpeg% >>log echo "%~dpn1">"%~dpn1.mp3"
pause
)
shift
if [%1] neq [] goto :process
pause
::======== end script
Then look at the ouput "log" and see what's missing from the submitted commandline.

message edited by nbrane


Report •

#11
Report •

#12
July 15, 2015 at 21:14:31
My mistake (again):
echo processing %1\file: -------- "%%~a"
if not exist "%%~dpna.mp3" >>log echo %pathToFFmpeg% "%%~dpnxa">"%%~dpna.mp3"
pause
)
popd
) else (
:: here processes a single file
echo file-name process: "%~1"
pause
if not exist "%~dpn1.mp3" %pathToFFmpeg% >>log echo "%~dpn1">"%~dpn1.mp3"
pause
)

The extra output was my screwup. Should look like this

echo processing %1\file: -------- "%%~a"
if not exist "%%~dpna.mp3" >>log echo %pathToFFmpeg% "%%~dpnxa" "%%~dpna.mp3"
pause
)
popd
) else (
:: here processes a single file
echo file-name process: "%~1"
pause
if not exist "%~dpn1.mp3" >log echo %pathToFFmpeg% "%~dpnx1" "%~dpn1.mp3"
pause
)
::------------ end

I'm also assuming that the executable takes two main inputs: inputfilename outputfillename


Report •

#13
July 16, 2015 at 09:10:09
I give up, you clearly have no understanding what you should do to begin with.

Report •

Ask Question