Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to remove arguments from batch file input arguments?

Posted on 2013-10-23
5
1,282 Views
Last Modified: 2013-10-29
Hi,
I have a batch file and I pass some number of arguments to this batch file.

%* holds all the input arguments.

What I want is to be able to pass all these input arguments except the first one to another function from within the batch file.

I tried shift but it did not work.

Note: I don't know the number of input arguments therefore, I need to capture everything after the first one.

Example:
Let say I pass a b c d to this batch file

ECHO %1
pushd %1
ECHO %*
mw.bat -using A @perl //some/path/to/a/MYFunction %*

Open in new window


I want to pass b c d to MYFunction inside this batch file.

How can I do it?
0
Comment
Question by:Tolgar
  • 3
  • 2
5 Comments
 
LVL 84

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 39594618
Try this:
@echo off
setlocal
shift
:ArgumentsLoop
set ArgsExceptFirst=%ArgsExceptFirst% %1
shift
if not "%~1"=="" goto ArgumentsLoop
ECHO %1
pushd %1
ECHO %*
echo Remaining arguments: %ArgsExceptFirst%
mw.bat -using A @perl //some/path/to/a/MYFunction %ArgsExceptFirst%

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39594851
Here's a slightly more compact way to handle the same thing:

@echo off
echo %1
pushd %1
echo %*
for /F "tokens=1* delims=, " %%A in ("%*") do (
  call mw.bat -using A @perl //some/path/to/a/MYFunction %%B
)
popd

Open in new window

~bp
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39594955
billprew,
that will choke if
- the first argument contains a space or a comma, like test.cmd "a b" "1 1" "2 2"
- any argument contains an ampersand, like test.cmd foo "Aberfitch & Crombie"
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 39595278
Okay, fair enough, figured it was too clean to be dependable :).

In your approach, I think the PUSHD needs to happen before the "loop", not after, otherwise %1 will be null after the loop finishes, right.  So maybe like:

@echo off
setlocal
echo %*

echo %1
pushd %1

shift
:ArgumentsLoop
set ArgsExceptFirst=%ArgsExceptFirst% %1
shift
if not "%~1"=="" goto ArgumentsLoop

echo Remaining arguments: %ArgsExceptFirst%
call mw.bat -using A @perl //some/path/to/a/MYFunction %ArgsExceptFirst%

Open in new window

~bp
0
 
LVL 84

Expert Comment

by:oBdA
ID: 39595315
So true. Figured copy and paste of the original code was too easy :).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question