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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

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

#1 Post by nnnmmm » 29 Sep 2024 12:44

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

miskox
Posts: 613
Joined: 28 Jun 2010 03:46

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

#2 Post by miskox » 30 Sep 2024 02:39

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#3 Post by aGerman » 30 Sep 2024 09:40

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

nnnmmm
Posts: 141
Joined: 26 Aug 2017 06:11

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

#4 Post by nnnmmm » 02 Oct 2024 20:26

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

Post Reply