Solved Parameters and shifting - batch

July 26, 2015 at 04:20:37
Specs: Windows 7 Professional, Intel i7 4770k, 16GB 1600MHz RAM
I'm working on a file search script in batch, I'm planning it to recieve parameters like "/Q", "/L", "/CSV" etc...

This is the first time I'm trying this out, and I'm not 100% sure about what I'm doing. Especially when it comes to parameter shifting.

My problem is: When entering a target file (I'm using "test test.txt") which contains spaces (and enclosed in quotation marks), it'll fail, and output

test.txt""=="" was not expected

Here's what I use to recieve and process parameters:

if "%1"=="" goto search
if "%1"=="/?" (
	goto help
if /i "%1"=="/L" (
	set log=1
	set searchlog=%time::=%
	set searchlog=!searchlog:,=!
	set searchlog=!searchlog:.=!
	goto params
if /i "%1"=="/Q" (
	set quiet=1
	goto params
if /i "%1"=="/CSV" (
	set csv=1
	goto params
if /i not "%1"=="/Q" (
	if /i not "%1"=="/L" (
		if /i not "%1"=="/CSV" (
			set file=%~1
			goto params

Better ways to go through with this, or improvements is appreciated as I don't know how you'd normally go through with this.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ

See More: Parameters and shifting - batch

Report •

July 26, 2015 at 18:41:59
✔ Best Answer
I usually just strip quotes then re-establish the quotes as needed:
if "%~0" equ "" echo no more parameters&goto :eof
echo current pararmeter, quoted:: "%~0" --- non-quoted: %0
goto :1
::--------- end snippet

Report •

July 27, 2015 at 10:28:56
I agree with nbrane about stripping quotes by using %~ but the shift command just scales the line parameters, i.e. param 2 becomes 1 and so on while scaled parameters are no more accessible. Param 0 is reserved for the batch name and is wiped out by the first shift issued. So nbrane's example is questionable in my opinion.

Report •

July 27, 2015 at 14:13:44
Consider the following:
C:\>copy con a.bat
@echo %0 %1 %2 %3
@echo %0 %1 %2 %3^Z
        1 file(s) copied.

C:\>a 1 2 3 4 5 6 7 8 9
a 1 2 3
1 2 3 4

How To Ask Questions The Smart Way

Report •

Related Solutions

Ask Question