I have a batch file and I need to invoke it like this “mybatch.bat -r c:\mydir”, and the batch file loops through the directory and writes file names to the output. The problem I’m facing is that I cannot read parameter “-r”.
Here’s what it looks like:
@echo off
echo [%date% %time%] VERBOSE START
for %%X in (%1\*.xml) do echo [%date% %time%] VERBOSE systemmsg Parsing XML file '%%X'
echo [%date% %time%] VERBOSE END
I can however use %2 instead of %1 and all works fine, but I want to read by parameter. Is this possible?
Cheers!
I’m not entirely sure I see your problem.
%1in this case is clearly-rand you should be using%2which isc:\mydir.If you mean you want to ensure that the user specifies
-rfirst, you can use something like:If the
-ris optional, you can often do:and then use
rflagandfspec.Doing true position-independent parameter parsing in batch is not an easy job. We have a subsystem which does it but unfortunately, it’s proprietary. I will tell you it runs across about 80-odd lines and is not the fastest beast in the world.
My advice would be to impose strict requirements of the argument formats rather than go down the position-independent path. You’ll save yourself a lot of hassles 🙂