Batch file - find free space and file size

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Batch file - find free space and file size

Postposted on Mon Dec 06, 2010 9:35 pm

Hi, I am basically just trying to make a batch file that finds out... Is a file smaller than the free space available? If yes then continue.

But files and disk space and be many GB so the usual batch file commands don't work because more than 2GB and you go out of range. I don't really write any code so maybe there is some external exe I can use or something? Because I can't work it out.
Duck
Gerbil
 
Posts: 29
Joined: Wed Sep 28, 2005 7:30 am

Re: Batch file - find free space and file size

Postposted on Mon Dec 06, 2010 11:56 pm

Duck wrote:the usual batch file commands don't work because more than 2GB and you go out of range.
PowerShell command shouldn't have the same problem. ex: http://technet.microsoft.com/en-us/libr ... 92684.aspx
wibeasley
Gerbil Elite
Gold subscriber
 
 
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Batch file - find free space and file size

Postposted on Tue Dec 07, 2010 6:25 pm

Do you know any other ways it can be done?
Duck
Gerbil
 
Posts: 29
Joined: Wed Sep 28, 2005 7:30 am

Re: Batch file - find free space and file size

Postposted on Tue Dec 07, 2010 7:18 pm

What other approaches have you considered or investigated?

I'd probably use a compiled language, like C#. But that's because I already familar with it. If you don't know either, Batch & PowerShell languages should be easier to learn. How much time do you have before the assignment is due?
wibeasley
Gerbil Elite
Gold subscriber
 
 
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Batch file - find free space and file size

Postposted on Tue Dec 07, 2010 10:37 pm

I found a VBS script to check file sizes but came up with the same problem if you try and use numbers bigger than 2GB.

It is no assignment. It's just this batch program I wrote to transcode video automatically. I just need to do a disk space check at the beginning otherwise it will break down when the disk space is too low. I'm no programmer really. I call other programs in the batch file and use a load of error checking along the way. I'm pretty pleased how it's turned out so far considering all the complexity I've had to add.

I was hoping there was just some exe file I could download that could return the size of files in MB and maybe another for disk space so I can use them in my batch file.

For now I can get the file size like this...
Code: Select all
set size=%~z1
set size=%size:~0,-6%
echo Size is... %size% million bytes

So will find out how many MB a file is more or less. It's real ugly but I can make is work good enough. I was just hoping there was a much better way of doing it.
Duck
Gerbil
 
Posts: 29
Joined: Wed Sep 28, 2005 7:30 am

Re: Batch file - find free space and file size

Postposted on Tue Dec 07, 2010 11:05 pm

Very low tech - What if you copy the file then check does it exist at the target destination? If yes, success and continue. Else issue with permissions or space, pause the batch file with appropriate message.
PhilipMcc
Gerbil
Gold subscriber
 
 
Posts: 88
Joined: Thu Feb 05, 2009 10:15 am
Location: Pittsburgh

Re: Batch file - find free space and file size

Postposted on Tue Dec 07, 2010 11:20 pm

Duck wrote:I just need to do a disk space check at the beginning otherwise it will break down when the disk space is too low. I'm no programmer really. I call other programs in the batch file and use a load of error checking along the way. I'm pretty pleased how it's turned out so far considering all the complexity I've had to add.
If you want to stick with batch files, but there's not an easier way to do it, it may be more economical to spend money on a larger HDD, than to spend time hacking something that might be brittle ultimately. If there's money in the budget for a few HDDs, does that avoid the problem?
wibeasley
Gerbil Elite
Gold subscriber
 
 
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Batch file - find free space and file size

Postposted on Wed Dec 08, 2010 1:20 pm

Ok, thanks. I assumed there was just a good way to do it that I couldn't work out what it was.

I will stick with batch files and just be inaccurate. I should be able to make sure that there is more than enough space which should be good enough for this.
Duck
Gerbil
 
Posts: 29
Joined: Wed Sep 28, 2005 7:30 am

Postposted on Thu Dec 16, 2010 4:13 pm

I just wanted to update this with some pure dos type ways of doing it. They are reasonably accurate and the free space batch file is not the typical way of doing it. (usually it is done with "DIR | FIND free space" type command).


Prints out free space on C: or any other drive if you edit it...
Code: Select all
:: Example batch file to show free disk space on C:


@echo off
cls
echo.
echo Free Space on C:
echo ========================================


for /f "tokens=1-3" %%n in ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:""') do set free=%%n& set total=%%p
echo.
echo %free% bytes free


set free=%free:~0,-3%
set /a free=%free%/1049
echo.
echo %free% MB free (approx and underestimated)


set /a free=%free%/1024
echo.
echo %free% GB free (approx and underestimated)


pause > NUL



Prints out file size of a file passed to it as an argument...
Code: Select all
:: Example batch file to show a file's size


@echo off
cls
SET DONE=0
SET Bytes=%~z1
SET KB=%Bytes:~0,-3%
SET MB=%Bytes:~0,-6%
SET GB=%Bytes:~0,-9%
SET TB=%Bytes:~0,-12%


echo.
echo %~f1
echo ===============================================================================
echo.


IF "%KB%" EQU "" SET DONE=B
IF %DONE% EQU B echo Size is... %Bytes% Bytes
IF %DONE% EQU B GOTO END


IF "%MB%" EQU "" SET DONE=K
IF %DONE% EQU K SET /a KB=(%BYTES%/1024)+1
IF %DONE% EQU K echo Size is... %KB% KB
IF %DONE% EQU K GOTO END


IF "%GB%" EQU "" SET DONE=M
IF %DONE% EQU M SET /a MB=(%BYTES%/1048576)+1
IF %DONE% EQU M echo Size is... %MB% MB
IF %DONE% EQU M GOTO END


IF "%TB%" EQU "" SET DONE=M
IF %DONE% EQU M SET /a MB=%KB%/1049
IF %DONE% EQU M echo Size is... %MB% MB (approx)
IF %DONE% EQU M GOTO END


SET DONE=G
IF %DONE% EQU G SET /a GB=%MB%/1074
IF %DONE% EQU G echo Size is... %GB% GB (approx)
IF %DONE% EQU G GOTO END


:END
pause > NUL
Duck
Gerbil
 
Posts: 29
Joined: Wed Sep 28, 2005 7:30 am

Re: Batch file - find free space and file size

Postposted on Thu Dec 16, 2010 5:55 pm

I'm glad DOS was past its popularity peak when I started programming 10 years ago.

In the line, "set /a free=%free%/1049", what is the role of "1049" in the conversion?
wibeasley
Gerbil Elite
Gold subscriber
 
 
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest