Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOS batch - File size into variable 1

Status
Not open for further replies.

chibaru

Programmer
Aug 24, 2005
13
US
I'm writing a DOS batch file for Windows XP Pro that will get the file size of a file every 5 seconds and compare it to the last fetched size. This check is testing for job completion. How do I get the file size into a variable for a specific file?
 
This probably isn't a very good way to do it, but something like this should give you a possible way to do it:
Code:
dir /-c [!]mypath&filename[/!] | find "[!]filename[/!]" > c:\temp.txt
type c:\temp.txt
pause
for /f "tokens=1,2,3,4,5 delims= " %%i in ('type c:\temp.txt') do (
echo i is %%i
echo j is %%j
echo k is %%k
echo l is %%l
echo m is %%m
)
What you're looking for seems to come out as %%l for me, but there could be some differences in directory listing styles depending on "dir" output is arranged in different localizations. Correct the path & filename references as appropriate, of course.
 
Thank you smah! I appreciate your quick response!
 
I just noticed that I still left some of my testing junk in there. This is cleaned up & clarified. It could be called as the subroutine 'getsize' as often as needed.
Code:
: getsize
@echo off
::
:: Set some useful variables that can be reused
set filepath=[!]some real path with trailing slash[/!]
set filename=[!]the real filename or variable or...[/!]
set extension=[!]the file exension including the . prefix[/!]
::
:: One line of directory listing gets output to a temporary text file
REM     This is only because the when using FIND, quotation
REM     marks are needed around the string and I couldn't
REM     figure out a good way to include it within the
REM     FOR command below.  There's probably a way around
REM     this, but I didn't bother.
dir /-c "%filepath%%filname%%extension%" | find "%filename%" > c:\temp.txt
::
:: Actually get the string out of that line within the temporary file
REM    Which token it's in might depend on the actual format of the
REM    DIR output used above.  I don't know if this format is different
REM    for different localizations of Windows.  In my case,
REM    it's the 4th token which is comes out as variable l
for /f "tokens=1,2,3,4,5 delims= " %%i in ('type c:\temp.txt') do (
echo The size of %filename%%extension% is %%l
set size=%%l
)
::
:: Now do some math or whatever with %size%
pause
cls
 
Thank you again! It worked after a minor change (filename was mistyped) but that's okay...it was easy!.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top