Batch wildcard strange behavior

130 Views Asked by At

I have a list of files that I'm looping through that match a certain size letter (A,B,C,D). The files are of the form ###T#####A###_# rev 1.dxf, where the rev 1 is only there some of the time, and A refers to the size, which is A, B, C, or D. When I try to loop through these in a set D.dxf or B.dxf, some A files are also found. I currently use the pattern ?????????A*.dxf, but would like to expand this to more file types without having to make multiple batch files. Interestingly, if I use the pattern TA*.dxf, the wildcard behaves normally.

Why does this happen, and how can I fix it while still being to catch files where the A may be at the beginning, end, middle, etc? If you need any clarification or extra information, feel free to ask.

Here is my relevant code:

FOR %%S IN (A,B,C,D) DO (
echo Converting size %%S. . .
FOR %%F in ("%filepath%\?????????%%S*.dxf") DO (
    echo Converting %%~nxF to PDF, size %%S
    SET %%S=!%%S! "%%~pF%%~nF.pdf"
    "C:\Program Files\AutoDWG\AutoDWG DWG to PDF Converter\d2p.exe"  /InFile %%~fF /OutFile %%~nF.pdf /Watermark %~dp0%%Swatermark.wdf /InConfigFile %~dp0%%S.ddp
)
echo:
echo Combining %%Ss. . .
pdftk !%%S! cat output "%filepath%\print\%%Ss.pdf"
echo Combined
echo:
)

EDIT: I'm running this on 32-bit Windows XP. Does this have anything to do with this thread? I will investigate when I get home.

EDIT 2: I've now figured out what the problem is. When I have several files with the same beginning characters, the 8.3 short names contain a hexadecimal number, which may match one of the letters I'm searching for. How can I discard short name matches in my for loop?

1

There are 1 best solutions below

0
On BEST ANSWER

Your link to the Strange Windows DIR command behavior thread seems to be a good thought. From RBerteig's thorough answer: Wild cards at the command prompt are matched against both the long file name and the short "8.3" name if one is present.... Try next approach:

SETLOCAL enableextensions enabledelayedexpansion
:::
pushd %filepath%
FOR %%F in ("*.dxf") DO (
  set "fname=%%~nF"
  set "fmatch="
  set "char04=!fname:~3,1!"
  set "char10=!fname:~9,1!"
  if /I "!char04!"=="T" (
    FOR %%S in (A B C D) do if /I "!char10!"=="%%S" set "fmatch=!fname!"
  )
  if defined fmatch (
     echo Converting %%~nxF to PDF, size !char10!
     rem another stuff here
  )
)
popd