Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 :)

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.
)

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 :)

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\folder3This 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?

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 :)

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

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