1

I have a batch file being passed a wildcarded file name:

mybat.bat foo\bar\*.stuff

How can I get

"*.stuff"

into a variable?

I know how to do

set the_path=%~p1
set the_file=%~n1

but this results in the_file being one of the files that matched, not the string wildcard spec.

3 Answers 3

3
echo %1
set x=%~1
set x=%x:**=*%
echo %x%

string substituion: replace *<string> with <string>, (being <string> = * in your case)

Sign up to request clarification or add additional context in comments.

5 Comments

That's pretty far out, thanks. What is the first set doing (why does it have a tilde in it)?
Oh, I see, that is like %~n1 . Interesting!
the tilde removes any surrounding quotes (if there are any). Useful for parameters with spaces, which has to have the quotes.
Hang on - it seems that %~1 retains the path in the string. I need to strip the path, and have only the wildcarded file name...
you are correct. The trick is done with the second set (substring substitution). But anyway - I think @aschimpfl's answer is better (generalized for use with any filemask).
2

String substitution as mentioned in this answer works fine as long as the file pattern starts with *.

However, the following code extracts the file name from the path independent from the *; rather it strips everything up to \ from the left side of the string in a loop until no more \ are encountered.
There is a second loop that handles the special case when a file is specified in the current directory of a drive, for instance D:test_???.log:

set "file=%~1"
:LOOP1
if not "%file%"=="%file:*\=%" set "file=%file:*\=%" & goto :LOOP1
:LOOP2
if not "%file%"=="%file:*:=%" set "file=%file:*:=%" & goto :LOOP2
echo "%file%"

2 Comments

you have a point there. Works for every file mask, not only *something
I did wonder about that too. In my case, it always starts with *, but this is definitely a "better" answer in the sense of general purpose, at the expense of more complexity. I went with the other for simple fit-for-purpose, but do appreciate this one.
0

Use the right tool for the job. This is a job for a Regular Expression!

FOR /F "tokens=*" %%F IN ('POWERSHELL -command "'%~1' -Replace '([A-Z]:)?(.*\\)*', ''"') DO ECHO The path is %~dp1 and the filename is %%F

The Regular Expression used in this command matches an optional drive letter and colon, followed by zero or more strings ending with "". Everything following the last "" is the part we want to keep. The PowerShell "-Replace" parameter replaces the matched string (the drive and path) with the empty string, leaving only the filename.extension. If filename.extension contains wildcards, they are left intact.

1 Comment

Welcome to Stack Overflow! Welcome to Stack Overflow! While your answer may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. You can edit your answer to add explanations and give an indication of what limitations and assumptions apply. - From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.