Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2860 connectés 

  FORUM HardWare.fr
  Programmation
  Divers

  HELP : Utilisation des arguments dans un batch

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

HELP : Utilisation des arguments dans un batch

n°210970
Ill Nino
Love
Posté le 09-09-2002 à 14:15:49  profilanswer
 

J'aimerais savoir, dans un .bat (batch DOS) le nombre d'arguments passe en ligne de commande avec le fichier...Comment on fait ???
 
Merci


---------------
Faut qu'on court comme des lions, des tigres et des ours!
mood
Publicité
Posté le 09-09-2002 à 14:15:49  profilanswer
 

n°210978
tomlameche
Et pourquoi pas ?
Posté le 09-09-2002 à 14:26:28  profilanswer
 

Ill Nino a écrit a écrit :

J'aimerais savoir, dans un .bat (batch DOS) le nombre d'arguments passe en ligne de commande avec le fichier...Comment on fait ???
 
Merci




Heu c'est pas très clair.
Tu veux quoi, le nombre d'arguments passés ou savoir comment passer des arguments ?
Pour passer des arguments, tu fais simplement  
bidule.bat [arg1] [arg2] ... etc et tu les récupères avec la variable %1, %2, etc ...
Pour connaitre le nb d'arguments, ben je sais pas ...

n°210980
Ill Nino
Love
Posté le 09-09-2002 à 14:27:57  profilanswer
 

C'est justement le nombre que je veux savoir... :(


---------------
Faut qu'on court comme des lions, des tigres et des ours!
n°211064
Carbon_14
Posté le 09-09-2002 à 15:16:01  profilanswer
 

Peut-être par déduction :
if (%3 == NULL) then ' y en a 2
if (%99 == NULL) then ' y en a 98
 
(je sais plus si on met = ou == (déformation :D)
si NULL existe, ...)
 
A moins que %0 existe (caché) et donne ce nombre ???


Message édité par Carbon_14 le 09-09-2002 à 15:17:06
n°211160
kayasax
Posté le 09-09-2002 à 16:01:08  profilanswer
 

a priori y a pas de fonction pour recuperer le nombre d'argument  
mais le nombre d'arguments est limité à 9 par defaut donc la soluce de carbon_14 (par deduction) est pitetre la bonne
 


Command line parameters
Batch files can only handle parameters %0 to %9
 
%0 is the program name as it was called,
%1 is the first command line parameter,
%2 is the second command line parameter,
and so on till %9.
 
OK, tell me something new.
 
Since %0 is the program name as it was called, in DOS %0 will be empty for AUTOEXEC.BAT if started at boot time.
This means that, in AUTOEXEC.BAT, you can check if it is being started at boot time or from the command line, for example to prevent loading TSR's twice.
 
 
--------------------------------------------------------------------------------
 
SHIFT
The batch file's limitation to handle parameters up to %9 only can be overcome by using SHIFT.
Let us assume your batchfile is called with the command line parameters   1 2 3 4 5 6 7 8 9 10 11 .
Now %1 equals 1, %2 equals 2, etcetera, until %9, which equals 9.
After your batch file handled its first parameter(s) it could SHIFT them (just insert a line with only the command SHIFT), resulting in %1 getting the value 2, %2 getting the value 3, etcetera, till %9, which now gets the value 10.
Continue this process until at least %9 is empty.
Use a loop to handle any number of command line parameters:
 
 
@ECHO OFF
:Loop
IF "%1"=="" GOTO ContinueHere your batch file handles %1SHIFT
GOTO Loop
:Continue
 
 
In Windows NT 4, 2000 and XP you can SHIFT the command line parameters starting from the nth positions using SHIFT's /n switch, where n can be any (integer) number between 0 and 8: SHIFT /4 will leave %0 through %3 untouched, and shift %5 to %4, %6 to %5, etcetera.
To use this feature, Command Extensions should be enabled.
 
 
--------------------------------------------------------------------------------
 
Delimiters
Some characters in the command line are ignored by batch files, depending on the DOS version, wether they are "escaped" or not, and often depending on their location in the command line:
 
commas ("," ) are replaced by spaces  
semicolons (";" ) are replaced by spaces  
"=" characters are often replaced by spaces  
the first forward slash ("/" ) is replaced by a space only if it immediately follows the command, without a leading space  
multiple spaces are replaced by a single space  
I know of several occasions where these seemingly useless "features" proved very handy.
Keep in mind, though, that these "features" may vary with the operating systems used.
 
More on command line parsing can be found on the PATH and FOR (especially FOR's interactive examples) pages.
 
 
--------------------------------------------------------------------------------
 
More options in Windows NT 4/2000/XP
Windows NT 4 introduced a set of new features for command line parameters:
 
%*   will return all command line parameters following %0; keep in mind that Windows NT 4 will insert an extra space before %*, whereas 2000 and XP will remove all leading spaces from %*;  
%~dn   will return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC);  
%~pn   will return the directory of %n if %n is a valid path or file name (no UNC);  
%~nn   will return the file name only of %n if %n is a valid file name;  
%~xn   will return the file extension only of %n if %n is a valid file name;  
%~fn   will return the fully qualified path of %n if %n is a valid file name or directory;  
 
Windows 2000 and XP add even more options.
More information can be found at the page explaining NT's CALL command.
 
To remove the leading space of %* introduced by NT 4 use the following commands:  
 
 
SET commandline=%*
IF NOT CMDEXTVERSION 2 SET commandline=%commandline:~1%
 


 
source : http://www.robvanderwoude.com/index.html


---------------
All we need is a soul revolution
n°211192
Carbon_14
Posté le 09-09-2002 à 16:32:23  profilanswer
 

Sinon, on pourrait écrire un tout petit soft DOS qui renvoie en ERRORLEVEL le nombre d'arguments.
Faut voir comment il veut les traîter ensuite. S'il fait un test
if ERRORLEVEL 1 then
if ERRORLEVEL 2 then
if ERRORLEVEL 3 then
 
on revient à la méthode du début :)....
 
Il veut peut-être être sûr qu'il ne manque pas un argument à une ligne de commande (c'est plutôt au soft de rouspéter ?).


Message édité par Carbon_14 le 09-09-2002 à 20:08:01

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Divers

  HELP : Utilisation des arguments dans un batch

 

Sujets relatifs
[javascript]Help (newbie inside)utilisation des raccourcis alt + ..
[opengl] helpHelp, Récupération de la valeur d'un menu déroulant
[PHP] Probleme avec la fonction mail() -- Help me !!![ASP - ACCESS] remplir une BD HELP !
[Netscape] Utilisation de <layer>Video et media player Help
[C++ BUILDER]Utilisation de TImage [RESOLU]Help Calcul avec JavaScript
Plus de sujets relatifs à : HELP : Utilisation des arguments dans un batch


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR