Computing.Net > Forums > Programming > Reading another file's size as a variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Reading another file's size as a variable

Reply to Message Icon

Name: sandsb
Date: October 27, 2009 at 14:49:01 Pacific
OS: Windows XP
Product: Microsoft Windows xp professional edition
Subcategory: Batch
Comment:

I need my batch file to do something (or not) based on the size of another file.

I have a file called c:\myfile.txt and I need a batch file that will read myfile.txt's file size into a variable. I tried doing a dir c:\myfile.txt > text.txt and then reading in that text file but I don't know how to get the filesize only into a variable. Anybody?????



Sponsored Link
Ads by Google

Response Number 1
Name: Wahine
Date: October 27, 2009 at 19:48:49 Pacific
Reply:

This might do what you want:

@echo off
cls

for /f %%1 in ('dir/b myfile.txt') do (
    set filesize=%%~z1
)

echo %filesize%


0

Response Number 2
Name: Judago
Date: October 28, 2009 at 01:00:32 Pacific
Reply:

The process can be simplified further(avoiding the "dir" process):

for %%a in ("c:\myfile.txt") do (
    set filesize=%%~za
)


Batch Variable how to


0

Response Number 3
Name: Mechanix2Go
Date: October 28, 2009 at 01:43:28 Pacific
Reply:

Hi Judago,

nice n smooth


=====================================
Helping others achieve escape felicity

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Reading another file's size as a variable

Batch File- Subfolder as a Variable www.computing.net/answers/programming/batch-file-subfolder-as-a-variable/14153.html

Tricky Batch File Code? www.computing.net/answers/programming/tricky-batch-file-code/18784.html

Read a txt file to a variable www.computing.net/answers/programming/read-a-txt-file-to-a-variable/16422.html