Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

https://stackoverflow.com/questions/63254543/batch-wildcard-in-filename-path-not-working/63369411#63369411

https://stackoverflow.com/questions/63498946/pass-named-parameters-to-batch-script-with-special-characters/63509485#63509485

So I had to do this in a subscript which was passed multiple arguments and I didn't want end users to have to specify if they were using a wildcard. I basically check each File variable once it's been "separated" in the called script to see first if it has a wildcard, then find the file if it does. 

@ECHO OFF
CLS

SET file[0]="C:\find me\file with space2.txt"
::SET file[0]="C:\find me\file with space*.txt"
::SET file[0]="C:\find me\file with space?.txt"
::SET file[0]="C:\find me\file with spac?*.txt"

CALL :FindFileWildCard %file[0]% file[0]
ECHO %file[0]% 
PAUSE
GOTO :EOF

:FindFileWildCard
::Does Path contain WildCards?
ECHO "%~1" | FIND /i "*" >nul
IF %ERRORLEVEL% EQU 0 (
 FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
 SET %2="%%F"
 EXIT /B
 )
)
ECHO "%~1" | FIND /i "?" >nul
IF %ERRORLEVEL% EQU 0 (
 FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
 SET %2="%%F"
 EXIT /B
 )
)
EXIT /B

:EOF

@ECHO OFF
CLS

::set filetofind="C:\find me\file with space.txt"
set file[0]="C:\find me\file with spac?.txt"
CALL :FindFileWildCard %file[0]% file[0]

ECHO %file[0]% 
GOTO :EOF

::Function
:FindFileWildCard
::FOR %%F IN ("%~1") DO (
FOR /F "Tokens=*" %%F IN ('DIR /B /S "%~1"') DO (
SET %2="%%F"
EXIT /B
)

:EOF
  • No labels