Solved

How to remove arguments from batch file input arguments?

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

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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 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.:

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now