0

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!

11
  • 1
    hm, please check your conversions from bytes to megabytes and gigabytes. Commented Nov 25, 2013 at 17:55
  • THe problem is when you use the for command. It is looking through the contents of the file, not the file itself Commented Nov 25, 2013 at 17:56
  • Andy T: I don't think there's something wrong with the bytes. @Monacraft: When I do -- for %%X in (%file%.bin) do echo %%~zX bytes -- it correctly echos the bytes. What exactly do you mean? How do I change the for command so it looks through the file itself? Commented Nov 25, 2013 at 18:08
  • You state that 15000000000 bytes is 150MB (all other conversions are same). please check this: google.co.uk/… Commented Nov 25, 2013 at 18:12
  • If you're assuming one million bytes per megabyte, 15 MB would be 15,000,000 or 15000000. Commented Nov 25, 2013 at 18:14

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.