0

I have found a case that seems a bit surprising to me, related to the DIR /B command and the ~t and ~z modifiers for BAT variables. I have a test environment with some files located in a folder ("c:\users\g\documents\bat\carpeta prueba"), a TXT file (LISTA.TXT) with a list of some of those files and a BAT to obtain certain information through the DIR /B command and modifiers. The BAT is as follows:

@echo off
set carpresp=c:\users\g\documents\bat\carpeta prueba
set listarch=c:\users\g\documents\bat\carpeta prueba\lista.txt
rem pushd %carpresp%
for /f "tokens=* usebackq" %%x in ("%listarch%") do for /f "tokens=*" %%a in ('dir /b "%carpresp%\%%x"') do echo %%a %%~ta %%~za %%~na
rem popd

If I run it by enabling PUSHD/POPD, I get all the information, but if I run it as shown, the information associated with the ~t and ~z modifiers does not appear, as can be seen in the attached trace:

C:\Users\g\Documents\BAT>type "c:\users\g\documents\bat\carpeta prueba\lista.txt"
backup_pba.bak
backup_salida.bak
backup_kk.bak
backup_result.bak
cataratas.pdf
backup_info.bak
petroglifos.gpx

C:\Users\g\Documents\BAT>dir "c:\users\g\documents\bat\carpeta prueba"
 El volumen de la unidad C es Windows8_OS
 El número de serie del volumen es: BA10-8F02

 Directorio de c:\users\g\documents\bat\carpeta prueba

31/08/2022  09:50    <DIR>          .
31/08/2022  09:50    <DIR>          ..
20/07/2022  08:37    <DIR>          alabamaCarp
30/08/2022  18:38                80 backup_info.bak
30/08/2022  18:37               315 backup_kk.bak
30/08/2022  18:38               105 backup_lista.bak
30/08/2022  14:46             2.865 backup_pba.bak
30/08/2022  18:38               103 backup_result.bak
30/08/2022  18:27               233 backup_salida.bak
02/08/2022  10:30           100.874 Cataratas.pdf
31/08/2022  09:51               118 lista.txt
16/08/2022  16:00            96.726 Petroglifos.gpx
02/08/2022  07:07               102 result.txt
              10 archivos        201.521 bytes
               3 dirs  499.963.985.920 bytes libres

C:\Users\g\Documents\BAT>type pba1.bat
@echo off
set carpresp=c:\users\g\documents\bat\carpeta prueba
set listarch=c:\users\g\documents\bat\carpeta prueba\lista.txt
rem pushd %carpresp%
for /f "tokens=* usebackq" %%x in ("%listarch%") do for /f "tokens=*" %%a in ('dir /b "%carpresp%\%%x"') do echo %%a %%~ta %%~za %%~na
rem popd


C:\Users\g\Documents\BAT>pba1
backup_pba.bak   backup_pba
backup_salida.bak   backup_salida
backup_kk.bak   backup_kk
backup_result.bak   backup_result
Cataratas.pdf   Cataratas
backup_info.bak   backup_info
Petroglifos.gpx   Petroglifos
C:\Users\g\Documents\BAT>type pba1.bat
@echo off
set carpresp=c:\users\g\documents\bat\carpeta prueba
set listarch=c:\users\g\documents\bat\carpeta prueba\lista.txt
pushd %carpresp%
for /f "tokens=* usebackq" %%x in ("%listarch%") do for /f "tokens=*" %%a in ('dir /b "%carpresp%\%%x"') do echo %%a %%~ta %%~za %%~na
popd


C:\Users\g\Documents\BAT>pba1
backup_pba.bak 30/08/2022 14:46 2865 backup_pba
backup_salida.bak 30/08/2022 18:27 233 backup_salida
backup_kk.bak 30/08/2022 18:37 315 backup_kk
backup_result.bak 30/08/2022 18:38 103 backup_result
Cataratas.pdf 02/08/2022 10:30 100874 Cataratas
backup_info.bak 30/08/2022 18:38 80 backup_info
Petroglifos.gpx 16/08/2022 16:00 96726 Petroglifos
C:\Users\g\Documents\BAT>

Can someone explain this behavior to me? Thank you,

GGG

GGG
  • 35
  • 7
  • 3
    `dir /b "%carpresp%\%%x"` not really needed at all here returns just the file name with file extension without path. So the loop variable `a` has assigned only the file name with file extension. __FOR__ has to access for `%%~ta %%~za` the file system with the file name assigned to the loop variable `a` which is without path. So the file system searches for the file in the current directory which is not the directory `c:\users\g\documents\bat\carpeta prueba` and for that reason cannot find the file. For that reason __FOR__ cannot determine last modification time and file size of the file. – Mofi Aug 31 '22 at 09:36
  • 2
    The solution would be `for /F "usebackq eol=| delims=" %%I in ("%listarch%") do for %%J in ("%carpresp%\%%I") do echo %%I %%~tJ %%~zJ %%~nI` which does not only work, but is also much faster has there is not started in background for each file name in the list file one more `cmd.exe` with option `/c` and the `dir` command line. See also [Issue 7: Usage of letters ADFNPSTXZadfnpstxz as loop variable](https://stackoverflow.com/a/60686543/3074564) for the reason using here `I` and `J` instead of `x` and `a` as loop variables. – Mofi Aug 31 '22 at 09:36
  • Perfect both, the explanation and the solution. Thank you very much – GGG Aug 31 '22 at 10:38

0 Answers0