Solved

How to remove arguments from batch file input arguments?

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

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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

706 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

12 Experts available now in Live!

Get 1:1 Help Now