Solved

How to remove arguments from batch file input arguments?

Posted on 2013-10-23
5
1,257 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 83

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 83

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 83

Expert Comment

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

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write an app 10 70
bash script to unzip a file 5 50
unix in java example 9 62
Control Number of Log Files -Perl 7 71
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

786 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