Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Batch scripting: passing multiple parameters (Read 7891 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Batch scripting: passing multiple parameters

I see that you have discovered a useful tool in this thread; however I'll also mention my batch file, flac-verify.bat.  As normal, drag a folder or file onto the icon to run.
I slightly modified this file (I was learning batch today) to accept multiple files as input, not just a single file or folder.  Here's the section I modified -

Code: [Select]
REM Check whether the parameter is a single file or a folder
:getfiles
IF %1""=="" GOTO endfiles
IF %~z1 EQU 0 (CALL :FolderAction %1) ELSE (CALL :FileAction %1)
shift
GOTO getfiles
:endfiles
This works.

However, this line does not (skips right to label :endfiles) -
Code: [Select]
IF "%1"=="" GOTO endfiles

Can someone tell me why?  That line is practically right out of the ntcmds.chm helpfile under the 'shift' command -
Code: [Select]
Examples
The following batch file, Mycopy.bat, shows how to use shift with any number of batch parameters. It copies a list of files to a specific directory. The batch parameters are represented by the directory and file name arguments.

@echo off
rem MYCOPY.BAT copies any number of files
rem to a directory.
rem The command uses the following syntax:
rem mycopy dir file1 file2 ...
set todir=%1
:getfile
shift
if "%1"=="" goto end
copy %1 %todir%
goto getfile
:end
set todir=
echo All done

Batch scripting: passing multiple parameters

Reply #1
I think it would be because %1 will already be surrounded by quotes, if it has spaces in the path, so you would end up with ""C:\this is\my path\and file.xxx"", and it doesn't like that.

I always use [%1] - I saw it as a tip when I first started batch scripting, probably for this reason.

NB: I've found http://www.ss64.com/ invaluable when writing my scripts.

I actually find it quite interesing to see that code.  I have always been happy using the single file or folder approach; however when I wrote wv-gain.bat I needed to be able to process multiple files as well.  Below is what I came up with:

Code: [Select]
IF NOT EXIST %1 DO GOTO NoParams

REM Set counters
SET /A filesProcessed=0

REM Check whether the first parameter is a file or a folder
IF %~z1 EQU 0 (
  CALL :FolderAction %1
  GOTO :Pause
)

REM ===========================================================
REM  FileAction : single file or list of files has been passed
REM ===========================================================
:FileAction
REM Create list of files by cycling through all parameters
:Do
IF [%1] NEQ [] (
  REM Only add file to the list if its extension is ".wv"
  IF /I [%~x1] EQU [.wv] (
    SET fileList=%fileList% %1
    REM Increase file counter
    SET /A filesProcessed+=1
  )
  SHIFT
  GOTO Do
)

As you can see, it uses a similar "loop and shift" approach.  However, mine requires a sequence of files to pass all at the same time, thus the variable "fileList".  I believe that FLAC.EXE accepts multiple files in the same way, so this approach could be utilised for a FLAC verification script.

I may utilise the code that you have in future, so that users can pass one file, multiple files, or a folder to any of my batch files.  Thanks for the "reminder".  NB: This would be "my" code:

Code: [Select]
REM Check that we have at least one parameter
IF [%1] EQU [] GOTO NoParams

REM Process all parameters
:CheckParameter
IF [%1] EQU [] GOTO EndParameters
IF %~z1 EQU 0 (CALL :FolderAction %1) ELSE (CALL :FileAction %1)
SHIFT
GOTO CheckParameter
:EndParameters
I'm on a horse.

Batch scripting: passing multiple parameters

Reply #2
I understand that "%1" will create a string like ""....."" but if %1 is empty, it should still output "", and thus should work.  After I posted this I did another test last night, and if I place some test flac files in the same directory as the bat file with IF "%1"=="" line, then launch the bat file from a commandline with one or both files specified, IT WORKS.  It fails only if I drag-and-drop the file(s) onto the batch script.  Figure that one out, heh.

Either way, I like your [] solution better.  Incidentally, using the SHIFT command would also work if you passed multiple folders to the batch script, which could be more useful than passing multiple files.

Quikie question, is there a reason in your script you use
Code: [Select]
:Pause
instead of just
Code: [Select]
Pause
(it's a label I'm assuming, not a special command like GOTO:EOF)?
Code: [Select]
REM Check whether the first parameter is a file or a folder
IF %~z1 EQU 0 (
  CALL :FolderAction %1
  GOTO :Pause
)

Batch scripting: passing multiple parameters

Reply #3
It should just be GOTO Pause (although I don't think it matters).

If I use IF "%1"=="" and just double click the batch file (or call it will no params) it jumps to endfiles as expected.  If I run from the command line and pass in file names (no path) with no spaces and no quotes it runs as expected.  If I run on the command line and pass a full path in quotes I get:

Code: [Select]
and was unexpected at this time.


Did your flac files have spaces in the filename, and did you specify the full path with quotes?  If not then it should have worked - it's only when you use quotes as the parameter contains spaces that it falls over.

Anyway, using [%1] is better.

Code: [Select]
C:\Documents and Settings\Neil\Desktop>wraithdu
I'm at endfiles...

C:\Documents and Settings\Neil\Desktop>wraithdu test1.tak test2.tak
File: test1.tak
File: test2.tak
I'm at endfiles...

C:\Documents and Settings\Neil\Desktop>wraithdu "C:\Documents and Settings\Neil\
Desktop\test1.tak" "C:\Documents and Settings\Neil\Desktop\test2.tak"
and was unexpected at this time.
C:\Documents and Settings\Neil\Desktop>
I'm on a horse.

Batch scripting: passing multiple parameters

Reply #4
My test was the same as yours (test1.flac, test2.flac).  I got the same results also, successful if passing a filename, and failed if drag-and-drop (same as passing a full path with quotes).

Well that settles it then.  Seems funny MS would include a help file example with such a glaring pitfall.