FreeSoftwareServers
FreeSoftwareServers

Reputation: 2791

Batch Wildcard in FileName/Path not Working

I'm more of a bash guy then batch and I'm struggeling to understand why this wildcard doesn't work. I have a batch file to print documents, but I want to wildcard the revision number.

Eg: This works:

@ECHO OFF
CLS

ECHO PRINTING HR PACKAGE
PAUSE

SET PDF_DS_P=call "Print_PDF_Double_Sided.cmd"
SET PWD=\Orientation Package\HR\
SET F1="%PWD%HR Docs\HR Welcome (rev02.00).pdf"
%PDF_DS_P% %F1%

Double_Sided.CMD

%PRINTCMD% "%~1" "\\%SERVER%\%SHARE%"

But if I do the following, it breaks the script:

SET F1="%PWD%HR Docs\HR Welcome (rev*).pdf"

Upvotes: 0

Views: 548

Answers (2)

FreeSoftwareServers
FreeSoftwareServers

Reputation: 2791

Turned Aacini's answer into a function, but noticed a caveat straight away (which I didn't debug as I can live with).

Caveat: This found the first alphanumerical result, which if there was multiple files, would be the worst option, I'd want the last alphanumerical result. Feel free to add a better answer for an up vote!

@ECHO OFF
setlocal enabledelayedexpansion
CLS

SET PWD="C:\tmp
set  RevFile=%PWD%\Rev*.pdf"
ECHO %RevFile%
CALL :FINDFILEWILDCARD %RevFile% RevFile

:FINDFILEWILDCARD
for %%f in (%~1) do (
 SET %2="%%~f"
 EXIT /B
)

ECHO %RevFile%
PAUSE

Upvotes: 0

Aacini
Aacini

Reputation: 67216

In bash a wildcard is automatically expanded to a series of files and execute a command with each file. In Batch no. In Batch you must expand explicitly a wildcard via a FOR command and then execute the desired command with the FOR parameter:

@ECHO OFF
CLS

ECHO PRINTING HR PACKAGE
PAUSE

SET PDF_DS_P=call "Print_PDF_Double_Sided.cmd"
SET PWD=\Orientation Package\HR\

for %%f in (rev*.pdf) do (
   %PDF_DS_P% "%PWD%HR Docs\HR Welcome (%%~Nf).pdf"
)

Upvotes: 2

Related Questions