Page 1 of 1

Resetting the incoming parameter in a batch file

Posted: 22 Mar 2012 05:52
by d360991
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}

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

Re: Resetting the incoming parameter in a batch file

Posted: 22 Mar 2012 06:57
by Squashman
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$.

Re: Resetting the incoming parameter in a batch file

Posted: 22 Mar 2012 07:24
by d360991
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

Re: Resetting the incoming parameter in a batch file

Posted: 22 Mar 2012 08:05
by foxidrive
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
)