Solved

How to remove arguments from batch file input arguments?

Posted on 2013-10-23
5
1,382 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 85

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 56

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 85

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 56

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 85

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

635 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