Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I need some help please.
I need to create a batch file in which i am going to have scheduled. I am unsure on which OS it will be running on, but will be 1 of the following: XP, NT, 2000, Server 2003.
I am looking for a way in batch that I can check the size of a file, if it is greater than 500mb, i will perform a loop.
I am having trouble with working out how to get the file size and check if it is greater than 500mb.
Any help on how to perform this command is greately appreciated.
Cheers,
damiike

To check the file size you need to assign the file name to an external or internal variable and then use the ~z modifier, e.g. %~z1.
Then to chek the size use an If statement
If %~z1 gtr 524288000 GoTo :SomeLabel
What above posted works in all NT based systems i.e. the ones you referred as candidate environments (but NOT under Win 9X/ME).
If the file name is on the command tail, it is assigned to a variable without problems, if however you catch the file name inside the script to setup an internal variable use an internal subroutine
Call :SomeLabel FileName
and code the routine as below at the bottom of your script
GoTo :EOF
:SomeLabel
......
.....
GoTo :EOF

Thanks for the help IVO,
i've attempted the following, here is my code, still struggling to get the size if statement to work, the following is my code/errors im getting:code:
echo on
set 1 =C:\test.txtIf %~z1 GTR 524288000 GoTo :action
echo off
rem selects all time / date information for setting datestamp / timestamp details
:action
For /f "tokens=1-8 delims=:/-,. " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
set dow=%%i
set %%a=%%j
set %%b=%%k
set %%c=%%l
set hh=%%m
set min=%%n
set ss=%%o
set cs=%%p
)
)rem set variables to datetime/timestamp needed.
SET DATESTAMP=%yy%%mm%%dd%
SET TIMESTAMP=%hh%%min%
----error:
C:\Documents and Settings\maubor\Desktop\Seal>echo on
C:\Documents and Settings\maubor\Desktop\Seal>set 1 =C:\plossys3.cfg
524288000 was unexpected at this time.
C:\Documents and Settings\maubor\Desktop\Seal>If GTR 524288000 GoTo :action
C:\Documents and Settings\maubor\Desktop\Seal>-----------
as you can see, the C:\test.txt variable never is questioned in the if statement, and the size condition does not test if the file is greater than the 500mb, all the other code is working correctly.
I appreciate your time and effort in helping me with this.
Cheers,
damiike

Start your code as below
@Echo Off
For %%I in (C:\test.txt) Do If %%~zI gtr 524288000 GoTo :action
Rem selects all time / date information for setting datestamp / timestamp details
:action
where the For %%I statement (that fits just one row starting with For and ending with :action) is a trick to assign the file name to an internal variable (%%I) that is an item different from an environment variable as the operand of a Set statement. Environment variables can't be manipulated by variable's modifiers.
Sorry my raw english confused you as I tried to point out this issue.
As you need more support contact me by e-mail as this thread is fading out.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |