batch-filesubstitutionexpandmodifiers

explanation for call expansion failing


I'm looking for some tips in troubleshooting the failure of call to expand t, z, and a.

Example:

for /F "delims=" %%F in (
  'dir /b "%source%\*." '
) do if not exist "%target%\%%~nF.jpg" copy "%source%\%%~F" "%target%\%%~nF.jpg"

for /F "delims=" %%B in (
  'dir /b "%target%\*.jpg"'
) do echo Size: %%~fB

This example runs perfectly, but when I edit that last line to be

%%~zB

I get multiple lines of output that just say Size:

Separately, if I just run

for /F "delims=" %%B in (
  'dir /b "%target%\*.jpg"'
) do echo Size: %%~zB

in a batch file by itself, it runs as expected, showing me actual file sizes. I've tried the same with %~a and %~t and get the same results; no output when run with the other copy sequence, and correct output when run alone.

Not sure what's going on here since %~f works just fine. Thanks to anyone that can help. I don't want to run two batch files. Shouldn't be necessary IMHO.


Solution

  • I'd suggest you carefully examine the output using %%~fB. The report is probably showing the current directory, not %target% (at least, it does for me...)

    Fix:

    PUSHD %target%
    
    for /F "delims=" %%B in (
      'dir /b "*.jpg"'
    ) do echo Size: %%~zB
    
    POPD