Computing.Net > Forums > Programming > Batch file based on folder sizes

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.

Batch file based on folder sizes

Reply to Message Icon

Name: FutureBatch
Date: November 18, 2008 at 01:08:55 Pacific
OS: Windows XP
CPU/Ram: 2 GB
Product: Intel
Comment:

Hello everyone,

im trying to make a batch file, which could check the size of 3 folders and move files from them under these conditions:
a) there are 3 folders in drive D:\
folder1, folder2 and folder3, everyday their size is getting bigger.
b) if the overall size of all 3 folders gets greater than 63 GB, then the files inside of all 3 folders in D:\ should be moved to drive E:\backup, so this way D:\ has 63 GB free space again.

I hope someone could show me the way, how to implement this task with batch, any help would be appreciated, thanks :)



Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: November 18, 2008 at 02:24:32 Pacific
Reply:

Here's a batch file you can use, but first, a few caveats:

1. If there are alternate data streams in any of those files, then the hidden data sizes won't be counted, so you'll be underestimating the total folder sizes.

2. If there are any hidden files, they won't be counted.

3. If any of the files are compressed with NTFS compression, it is the uncompressed sizes which are reported, so you'll be overestimating the sizes.

4. This batch file assumes all files are in the top-level folder: it does not do subfolders.

With this in mind, here is one way to calculate the file size totals in a batch file:

====================8<============cut==here============8<======================
@echo off
setlocal
rem totalBlocks accumulates the number of 512-byte allocation units
set /a totalBlocks = 0
for %%f in (d:\folder1\* d:\folder2\* d:\folder3\*) do set /a totalBlocks += (%%~zf+511)/512
set /a totalKB = %totalBlocks% / 2
echo Total %totalKB% KB
if %totalGB% geq 64512 (
echo Greater than 63 GB, moving to E: to make space...
move D:\folder1\* E:\backup\folder1\
move D:\folder2\* E:\backup\folder2\
move D:\folder3\* E:\backup\folder3\
) else (
echo Less than 63 GB, not moving files yet.
)


0

Response Number 2
Name: FutureBatch
Date: November 18, 2008 at 04:18:15 Pacific
Reply:

Hi klint,

thanks for your fast reply, I get the following output when i run your recommended batch file (I have added pause at the end of the code)otherwise it just flashes:

Total 9805680 KB
Greater than 63 GB, moving to E: to make space...
Cannot move multiply files to single file
Cannot move multiplay files to single file
Cannot move multiplay files to single file
press any key to continue...

I wonder, if it has any impact, that all files in two folders are *.rar and the third folder has *.bkf files. Its obvious that total 9805680 KB isnt greater than 63 GB and the real size currently is 53 GB of all three folders. If you have any ideas, im glad to hear them :)


0

Response Number 3
Name: klint
Date: November 18, 2008 at 05:21:07 Pacific
Reply:

Sorry, the code was untested. I got my MB and GB mixed up. Please replace the following:

set /a totalKB = %totalBlocks% / 2
echo Total %totalKB% KB
if %totalGB% geq 64512 (

with this:

set /a totalMB = %totalBlocks% / 2048
echo Total %totalMB% MB
if %totalMB% geq 64512 (

As for the problem with moving files, replace the MOVE commands with the following:

if not exist E:\backup\folder1 md E:\backup\folder1
if not exist E:\backup\folder2 md E:\backup\folder2
if not exist E:\backup\folder3 md E:\backup\folder3
move D:\folder1\* E:\backup\folder1
move D:\folder2\* E:\backup\folder2
move D:\folder3\* E:\backup\folder3

This leaves one more problem outstanding. You said the total was actually 53 GB, but my batch file calculated about 9.8 GB. Can you re-confirm the sizes? Could it be that you have hidden files or subfolders, which are not being counted?


0

Response Number 4
Name: FutureBatch
Date: November 18, 2008 at 06:27:04 Pacific
Reply:

Big thanks klint, now everything works fine :) I think I got inspired by your batch example and I think I would definitely learn more about batch, cuz it makes everything much more simple :) thanks again, goodluck :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch file based on folder sizes

copy or move files based on file extensions www.computing.net/answers/programming/copy-or-move-files-based-on-file-extensions/20072.html

batch file help www.computing.net/answers/programming/batch-file-help/13207.html

Move a XML file based on content www.computing.net/answers/programming/move-a-xml-file-based-on-content/18945.html