Page 1 of 1

getting a filesize doesnt work without * in it, wish to know why

Posted: 29 Sep 2024 12:44
by nnnmmm

Code: Select all

@ECHO OFF
SET DSPEC=C:\ZTREE
SET FNAME=ZTW.HST

REM FOR /R "%DSPEC%" %%V IN ( ZTW*.HST ) DO (

FOR /R "%DSPEC%" %%V IN ( "%FNAME%*" ) DO (
   SET /A FSIZE=%%~zV
rem SET FSIZE=%%~zV
rem  ECHO %%~zV
)
IF %FSIZE% LSS 5000 ECHO %FNAME% SIZE IS LSS THAN 5000
IF %FSIZE% GTR 5000 ECHO %FNAME% SIZE IS GTR THAN 5000
ECHO The file size of %DSPEC%\%FNAME% is %FSIZE% bytes

:END
PAUSE
why do i need this asterisk * in %FNAME%*? or it doesnt work.
except for "call :filesize" one, i tried just about most codes going around in the internet, but none of them worked
%FNAME%* was my invention by accident, i have been using this accident maybe over 20 years, i wish to understand

Re: getting a filesize doesnt work without * in it, wish to know why

Posted: 30 Sep 2024 02:39
by miskox
SS64 has this (https://ss64.com/nt/for_r.html)
Unlike some other variants of the FOR command, with FOR /R you must include a wildcard (either * or ?) in the 'set' to get consistent results returned. In many cases you can work around this by adding a single character wildcard e.g. if you are looping through multiple folders to find the exact filename myfile.txt you could instead specify myfile.t?t
Saso

Re: getting a filesize doesnt work without * in it, wish to know why

Posted: 30 Sep 2024 09:40
by aGerman
The first question that I have is WHY do you need FOR /R. Do you really need to search the file recursively in subdirectories? I'm asking because ...

Code: Select all

ECHO The file size of %DSPEC%\%FNAME% is %FSIZE% bytes
... indicates you already know that the full name of the file is %DSPEC%\%FNAME%. If so, FOR /R is the wrong command. Just use a simple FOR, like ...

Code: Select all

FOR %%V IN ("%DSPEC%\%FNAME%") DO ECHO Size: %%~zV


FWIW You have seemingly ^* nested FOR /R loops that I don't understand at all.

^* closing parenthesis missing

Steffen

Re: getting a filesize doesnt work without * in it, wish to know why

Posted: 02 Oct 2024 20:26
by nnnmmm
AA= REM FOR /R "%C1%" %%V IN (%FNAME1%*) DO (SET /A FSIZE1=%%~zV)
BB= FOR %%V IN ("%C1%\%FNAME1%") DO (SET /A FSIZE1=%%~zV)

i replaced AA= with BB=

i have a tendency to stick with one structure that worked no matter how bad it was. because i dont know much about DOS commands and operators even after ~40 years.

i probably also tried BB= from in the internet, somehow i dismissed it maybe they were using SetLocal EnableExtensions EnableDelayedExpansion along with it, but i dont remember it well
thanks for this.

side note:
Ztree\ZTW.HST this ztree's history file, i have been protecting it for ~30 years,
while i am using ZTree, if PC crashes or stops, ZTW.HST becomes ~0 byte file
without me knowing, i could backup the Ztree with ~0 byte history file
then i could lose several 100s commands in it built for 30 years