Resetting the incoming parameter in a batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
d360991
Posts: 8
Joined: 18 Mar 2012 13:12

Resetting the incoming parameter in a batch file

#1 Post by d360991 » 22 Mar 2012 05:52

I'd like to write a batch file that called "listfiles.bat" which takes an optional parameter for a filter to list files. So, if a parameter is passed, I want my batchfile to do a dir "*parameter*.*." If no parameter is passed, I want my batchfile to do a "dir *.*"

Another variant to my problem is that I will call listfiles.bat from an utility called SlickRun. SlickRun sends a "$w$" as the default parameter. Therefore, inside listfiles.bat, I would like to reset the incoming parameter to a "".

This is what I have. Could you please correct this to achieve the above outcome?

Thank you so much.
-Nina


---------------------------------------------------

if "%1" == "$w$" then
"%1" = ""

if "%1" == "" goto skip1
dir *"%1"*.* /b > file.txt
goto skip2
:skip1
dir *.* /b > file.txt
:skip2

{do other things}

---------------------------------------------------

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Resetting the incoming parameter in a batch file

#2 Post by Squashman » 22 Mar 2012 06:57

IF slick run is always sending a $w$ to your batch file then you could use the SHIFT command to get ride of the initial $w$.

d360991
Posts: 8
Joined: 18 Mar 2012 13:12

Re: Resetting the incoming parameter in a batch file

#3 Post by d360991 » 22 Mar 2012 07:24

OK. How do I incorporate the SHIFT command in my little batch file using an "IF" statement. In other words, what would my batch file in the OP look like?

Thanks very much,
-Nina

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Resetting the incoming parameter in a batch file

#4 Post by foxidrive » 22 Mar 2012 08:05

That's a good idea Squashman.

This is untested:

Code: Select all

@echo off
if /i "%~1"=="$w$" shift

if "%~1"=="" (
dir /b
) else (
dir "*%~1*.*" /b
)

Post Reply