Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to know if a batch's parameter is empty  (Read 12554 times)

0 Members and 1 Guest are viewing this topic.

ind57

  • Guest
How to know if a batch's parameter is empty
« on: June 29, 2007, 07:05:52 AM »
Hi,
The question may appear stupid but I'm newbie in MS-Dos batches...
I'd like to test if a parameter pasted in my command line of my batch is present or not, I tryed:

IF "%1X"=="X" GOTO :syntaxe

The problem is when my parameter %1 still have quotes...
Thanks in advance for your answers,
ind57

PS: WinXP SP2 French, Dell latitude D520, 1go, Core Duo.

contrex

  • Guest
Re: How to know if a batch's parameter is empty
« Reply #1 on: June 29, 2007, 08:35:44 AM »
This is how we check if a parameter is blank

If "%2"=="" goto blank2

Moi je suis rosbif, mais j'aime bien Indochine!




ind57

  • Guest
Re: How to know if a batch's parameter is empty
« Reply #2 on: June 29, 2007, 08:53:24 AM »
Thanks contrex but it doesn't solve my problem.
My command line is:
run.bat "c:/test java/"

When I use:
echo %1
There is:
"c:/test java/"

So when I test:
IF "%1"=="" THEN ...
There is the message:
java""=="" était inattendu.
which could be translated in English by:
java""=="" was unattempted

In fact there is no problem when the parameter is not between quotes, but I don't know how checking if it is between quotes or not. ???

ind57

PS: t'as plutôt un bon accent  ;)

contrex

  • Guest
Re: How to know if a batch's parameter is empty
« Reply #3 on: June 29, 2007, 09:34:47 AM »
Here is how to solve the problem. The message you got that qqch était inattendue, in English versions of Windows will be "was unexpected at this time".

1. When you pass a parameter in quotes, which you need to do if it has spaces in it, you will have a problem in the batch file if you need to do a test such as this

if "%n"==""

because the quotes already in the parameter are added to the quotes in the "%n" and that makes the batch file crash. It also crashes if you do this

if %n==""


2. Les telles situations étaient prévues by the authors of the batch language (Windows 2000 and XP I mean). The following allows to remove quotes from a parameter

Suppose that %1 is "Nicola Sirkis" (with quotes)

then %~1 is Nicola Sirkis (without quotes)

The tilde character "~" (I don't know its name in French), when placed before the parameter number (or a letter if it is a FOR variable), removes any quotes surrounding that parameter or variable.

Consider the following batch file

Code: [Select]
@echo off
echo   passed parameter is %1
echo dequoted parameter is %~1

if "%~1"=="" echo parameter 1 is blank

I called it qparam.bat

here are its results

Code: [Select]

F:\test\param>qparam Nicola Sirkis
  passed parameter is Nicola
dequoted parameter is Nicola

F:\test\param>qparam "Nicola Sirkis"
  passed parameter is "Nicola Sirkis"
dequoted parameter is Nicola Sirkis

F:\test\param>qparam
  passed parameter is
dequoted parameter is
parameter 1 is blank












« Last Edit: July 02, 2007, 04:02:05 AM by contrex »

ind57

  • Guest
Re: How to know if a batch's parameter is empty
« Reply #4 on: July 02, 2007, 03:56:24 AM »
Thank you very much Contrex, c'est pile poil ce que j'voulais!
merci encore,
ind57