Frage

I'm new to making Batch files and ran into a problem. My plan is to compare the size (in byte) of a file (unknown file name but with a fixed extension) with a few 'maxfilesizes' and do different stuff depending on the size of the file.

@echo off
for /F %%a in ('dir /b *.bin') do set file=%%~na
:BEGIN
for %%X in (%file%.bin) do if %%~zX LSS 1500000000 goto 15MB
for %%X in (%file%.bin) do if %%~zX LSS 5000000000 goto 50MB
for %%X in (%file%.bin) do if %%~zX LSS 10000000000 goto 100MB
for %%X in (%file%.bin) do if %%~zX LSS 15000000000 goto 150MB
goto Error
:15MB
echo Less than 1500000000b. Do stuff here.. 15MB.
goto TheEnd
:50MB
echo Less than 5000000000b. Do stuff here.. 50MB.
goto TheEnd
:100MB
echo Less than 10000000000b. Do stuff here.. 100MB.
goto TheEnd
:150MB
echo Less than 15000000000b. Do stuff here.. 150MB.
goto TheEnd
:Error
echo File size greater than 14GB. Aborting..

:TheEnd
echo All done.
pause

I have tested this batch with a file that is ~8.000.000.000 bytes so ':100MB' should be triggered, but it is not. Instead I always get 'File size greater than 14GB. Aborting..' Please keep in mind that I'm a beginner, so the above batch is probably not the most efficient one.. Any help would be much appreciated.

EDIT: @Aacini thank you so much, I've just tested your code with some of my files and it does exactly what I wanted it to do.

[...] but I think you should review the numbers you use in the bytes -> MB conversion...

These are no conversions. I only use them for the goto's because if filesize less than 1500000000 bytes, I want the file to be split into 15MB parts (echos will be replaced by commands). Hence the -> goto 15MB. Hope that clears it up. Thanks again!

War es hilfreich?

Lösung

You may just eliminate the three last digits of the size to convert bytes to KB, that is equivalent to divide it by 1000. If you are interested in the size in MB, the difference does not matter:

@echo off
for %%a in (*.bin) do set fileSize=%%~Za
set fileSize=%fileSize:~0,-3%
:BEGIN
if %fileSize% LSS 1500000 goto 15MB
if %fileSize% LSS 5000000 goto 50MB
if %fileSize% LSS 10000000 goto 100MB
if %fileSize% LSS 15000000 goto 150MB
goto Error

You may also achieve the comparisons this way:

:BEGIN
for %%a in (15 50 100 150) do if %fileSize% LSS %%a00000 goto %%aMB
goto Error

Note: Previous code duplicate the behavior of your own, but I think you should review the numbers you use in the bytes -> MB conversion...

Andere Tipps

Here's what you can do - If you are trying to "scan" from a specific path (for example C:\Users\%username%*.extension) you can use the following command to "loop" through all directories and subdirectories you specify:

FOR /R "[Drive][Path][*]" %%A in ('*.extension') DO ECHO %%~nA

You can replace %%~nA with the type of information you want to display (keep in mind that you must include ~%% and A in this. You can change n. You can also include more than just one, like %%~nxA). I hope this helps with what you want. If you want to do more than just DO ECHO you can do the following

DO ECHO (
echo %%~nA
echo %%~xA
echo Scanning...
)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top