Get file size in KB/MB/GB in windows batch file
I have a Windows (Windows 7/2008 R2) batch file which takes daily backup of file.(Oracle dump export file e.g dump-2016-10-17.DMP)
I want to add the FILE size (in a variable) which should be in KB/MB/GB according to the size, or at least it should get file size in MB. CODE:
f1 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 :END – Syed Jahanzaib Oct 17 ’16 at 8:41
4 Answers 4
Just as an alternative, you can use the output of robocopy command to retrieve the required information
robocopy will be used to only list ( /l ) the information of the file being copied, removing any non needed information from the output (the rest of the switches) and using a for /f command to tokenize the output lines and read only the file size
In the case of folders, the same idea can be used, but instead of reading the data from the file list, we can use a recursive «copy» and retrieve the data from the job summary
Switches changed to retrive only the job summary, and filtered to only read the line containing the Bytes information.
edited 2016/10/26 — changed robocopy switches to face permission problems, changed Byte: retrieval to : line filter plus skip clause just in case there is a locale without the Bytes: string, changed storage unit detection.
8 Free Tools to Find What Files and Folders are Taking Up Hard Drive Space
When someone tells you they’re running out of hard drive space, the first thing you would probably tell them to do is to clean up the system using a program such as CCleaner. That will help remove all the junk and temp files that can take up several Gigabytes on badly affected systems. Uninstalling old software might free up some more space depending on how many useless programs are installed.
Even after doing those things, a system might still be left with less than 10% of free space remaining and Windows doesn’t really like that. Even if you have the operating system on an SSD or an NVMe drive, Windows will still slow down and misbehave if it runs out of available space. Other locations on the drive might have been filled with data that you or most cleaning programs wouldn’t normally look in.
The normal thing to do would be to check the properties of each top level folder and navigate your way down until the culprit files and folders are found. This could certainly waste a lot of time and isn’t an efficient method. Thankfully there are much better solutions for how to find out what files and folders are taking up hard drive space, and here’s a selection of 8 free tools to do just that.
TreeSize Free is a popular tool because it quickly gives you a fair idea of which folders are taking up the most space. The occupied proportion of space each folder takes up is shown by a series of bars, and when you click to open each subfolder, the same bar layout shows the space proportions for that folder. With version 4.0 In 2017, TreeSize Free was updated to include the popular treemap view which was taken from its paid Professional version
Click the upper half of the Open Directory icon to open a requester or the lower half to open a drive list. TreeSize uses the MFT (Master File Table) to scan NTFS drives so is incredibly fast. By default, the file and folder tree is displayed with the size bars. To also open the treemap, click on View > Show Treemap Chart. The treemap can be shown on the right or bottom, be in 2D or 3D, and show labels/files, free space and etc.
Changes can also be made to the way the folder tree looks. For example, the text can be changed from size to file count to a percentage. You can also change the gradient colors on the size bar and change the UI itself to a dark or touch theme. Include/Exclude filters can be applied, and the program can be configured to let you easily run a scan from the Explorer context menu. Installer and portable versions are available.
2. SpaceSniffer
We’re fans of SpaceSniffer because apart from being quite fast, it’s live visual treemap layout can quickly give a very good idea of which files or folders are taking up the biggest amount of space on your drive. This has an advantage over software that only shows a bar or pie chart because you can already see several layers down. A massive file buried inside multiple subfolders is far easier to spot without touching anything.
Simply choose a drive or folder to scan from the box when you start SpaceSniffer and within a few seconds you will be able to recognize larger folders (brown boxes) and files (blue boxes) filling up the display. The bigger the box, the more space it occupies. Folders can be drilled down even further by double clicking on them, or the level of detail can be increased using the blue box icons in the toolbar.
The tri-colored box icon also shows different types of files with different color boxes. For example, the blue boxes are archives and disc images while the yellow boxes are executables and system files. There are other color styles for audio, video, and images or you can even create your own. Other aspects of the treemap display can be altered from the Edit > Configure window. SpaceSniffer is a zipped portable executable.
3. RidNacs
This program is like a simpler and easier to use version of TreeSize in that it displays a number of bars to represent the percentage of space occupied on the drive or in the folder. It can show files in the tree and also the folder size, percent and number of files all at once though which might be more helpful.
To scan, press the yellow folder icon in the toolbar or click on the favorites button which should have your drives and any previously visited folders listed for quick access. The scan is reasonably fast and a report can be saved in a TXT, HTML, XML or CSV file. The size bars can also be changed to different preset colors which is useful as the default blue stripe isn’t great (the image shows the Spectrum bar).
RidNacs has a portable and setup installer version. The website is in German but the download page is self explanatory and the program itself defaults to English.
4. WizTree
WizTree is by Antibody Software who also make the useful WizMouse and WizFile utilities. The program is very similar to the popular but very old WinDirStat (see below) but is much faster to display its results. WizTree is one of the fastest tools of this type and on par with TreeSize because it also uses the Master File Table to scan the drive. There is an animated donate button in the window but it isn’t really a problem.
Select a drive or specific folder from the drop down and the results will be shown pretty quickly. The window is split into the file/folder tree view, the filetype view and the treemap at the bottom. Switching from Tree View to Flat View will change the window to a search filter and file display with options to sort the top files by size, modified date and etc.
Additional options include the ability to open a command prompt here, copy the file path, export to CSV, save the treemap as an image, dump the MFT file, and change the default treemap colors. Portable and installer versions are available.
5. WinDirStat
WinDirStat is another tool to use the treemap visual interface which shows the size of individual files as a series of boxes to represent their space on the drive. It’s also still a popular tool considering there have been no updates since 2007. Some useful extra functions are included such as opening a Command Prompt at the location you’re clicking on, copying the file path or emptying the recycle bin before a scan.
If you have more than one drive to scan they can be selected on startup by highlighting which ones you want. The display is split into three parts; one for the treemap, one for the directory tree and the top right window for telling you what the treemap colors represent. WinDirStat works on just about any version of Windows right back to Windows 95. WinDirStat never officially had a portable version but you can either extract the installer with 7-Zip or go to PortableApps.com to get one.
6. Xinorbis
If you’re someone that likes to look at large amounts of information then Xinorbis could be for you. This is because it analyzes and gives you details for just about everything that’s occupying your hard drive space and the data is displayed in a number of different ways. There are separate windows for things like file types, extensions, history, dates, magnitude and size levels. Data can be viewed as text based tables, bar charts or pie charts.
The initial Summary screen will give some useful details about the number of files/folders, the largest file and folder, and bars for which category they fall in to. Click Folder Properties and all the different ways the statistics can be presented are available. There are other useful sections like finding duplicate files, search, folder structure/map/exploder views, and opening separate windows to create your own custom graphs and charts.
There are also options to create your own custom reports in HTML or CSV. Xinorbis is probably the most comprehensive tool of this type so it does require some time to figure things out. Installer, portable and also a useful command line version is available for download.
7. Folder Size
The free version of Folder Size is perfectly usable but lacks some features that are reserved for the shareware versions. These include filters, export/print results, scanning over a network, multiple scans and command line options. There are a few ads for other products by the developer in the UI, but they aren’t too intrusive.
Start the program and choose the drive from the drop down, then press the green scan icon, or a folder using the green/yellow icon. You will get both a text based view of the folder sizes and also the graphical view, either of which can be turned off. Switch between a pie chart or a bar graph using the icons in the lower pane. Then simply navigate your way around to see which files and folders are taking up space. Both portable and installer versions are available.
8. JDiskReport
JDiskReport is a tool that runs using Java meaning you need the Java Runtime Environment installed to use it. What’s quite good is the different ways in which you can visually view the information. For instance, the size of a folder or drive can be displayed as different looking charts such as a pie, ring, bar or a details list. The color of the charts or the interface look can be changed and a context menu option can be integrated into Explorer.
Starting the program gives options to select the folder or drive to scan along with a few previous scan locations and the option to reload a previously saved scan result. There are different tabs to view the information in different ways including a size tab that gives the overall size of the selected location, a Top 50 tab which lists the 50 largest/oldest/newest files, and size/modified/filetype distribution maps.
Download the Zip archive on the website to use the portable version. If it doesn’t run, right click on jdiskreport.jar > Open with > “Java Platform SE Binary”. Being made in Java means JDiskReport is supported on a number of platforms including Mac OSX and Linux as well as Windows.
Important Note: Unless you’re very sure the files and folders listed can safely be deleted, don’t delete them from within these programs. Just because the data might be taking up lots of space, you need to check first that it’s not part of the operating system or installed software which could cause crashing or instability problems if you remove it.
GetFileSize function (fileapi.h)
Retrieves the size of the specified file, in bytes.
It is recommended that you use GetFileSizeEx.
Syntax
Parameters
A handle to the file.
A pointer to the variable where the high-order doubleword of the file size is returned. This parameter can be NULL if the application does not require the high-order doubleword.
Return value
If the function succeeds, the return value is the low-order doubleword of the file size, and, if lpFileSizeHigh is non-NULL, the function puts the high-order doubleword of the file size into the variable pointed to by that parameter.
If the function fails and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE. To get extended error information, call GetLastError. When lpFileSizeHigh is NULL, the results returned for large files are ambiguous, and you will not be able to determine the actual size of the file. It is recommended that you use GetFileSizeEx instead.
If the function fails and lpFileSizeHigh is non-NULL, the return value is INVALID_FILE_SIZE and GetLastError will return a value other than NO_ERROR.
Remarks
You cannot use the GetFileSize function with a handle of a nonseeking device such as a pipe or a communications device. To determine the file type for hFile, use the GetFileType function.
The GetFileSize function retrieves the uncompressed size of a file. Use the GetCompressedFileSize function to obtain the compressed size of a file.
Note that if the return value is INVALID_FILE_SIZE (0xffffffff), an application must call GetLastError to determine whether the function has succeeded or failed. The reason the function may appear to fail when it has not is that lpFileSizeHigh could be non-NULL or the file size could be 0xffffffff. In this case, GetLastError will return NO_ERROR (0) upon success. Because of this behavior, it is recommended that you use GetFileSizeEx instead.
Transacted Operations:В В If there is a transaction bound to the file handle, then the function returns information for the isolated file view.
In WindowsВ 8 and Windows ServerВ 2012, this function is supported by the following technologies.