Embedded IF-ELSE sentence in a FOR-DO loop

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Embedded IF-ELSE sentence in a FOR-DO loop

Post by MigrationUser »

10 Dec 2012 22:18
maskinmesteren

I have made a script that should be able to count number of files and size in a specific folder.
However it does not work after having embedded an IF-ELSE sentence. What is wrong?
The IF-ELSE sentence is to be extended so I can separate files in size<1000 byte, 1000<size>1000000, size>1000000
Right now it only separate at 1000 byte.

Here you can see the script:

Code: Select all

echo off
cls
setlocal

if exist D:\scripts\size.txt (
del /f /s /q D:\scripts\size.txt>nul
)

if exist D:\scripts\sum.txt (
del /f /s /q D:\scripts\sum.txt>nul
)

set /p sti=Angiv stien til den mappe der skal samment‘lles :
for /r "%sti%" %%S in (*.*) do (
set /A ST=%%~zS
if %ST% LSS 1000 (
echo %%~zS %%~nS %%~xS>> D:\scripts\size1.txt
set /A N1=N1+1
set /A T1=T1+ST
) else (
if %ST% GEQ 1000 (
echo %%~zS %%~nS %%~xS>> D:\scripts\size2.txt
set /A N2=N2+1
set /A T2=T2+ST
)
)
echo Antal og sum af mindre filer >> D:\scripts\summation.txt
echo %N1% >> D:\scripts\summation.txt
echo %T1% >> D:\scripts\summation.txt
echo.
echo Antal og sum af mindre filer >> D:\scripts\summation.txt
echo %N2% >> D:\scripts\summation.txt
echo %T2% >> D:\scripts\summation.txt
start /d D:\scripts size1.txt
start /d D:\scripts size2.txt
start /d D:\scripts summation.txt
endlocal

exit
sincerely
MASKINMESTEREN

----------------------------

#2 11 Dec 2012 02:16
RG


Take a look at the my post on DELAYED EXPANSION. May not be a perfect explanation, but should get you going.
viewtopic.php?f=2&t=27
I did not test this but you should be able to make you code work with these changes:
setlocal
to
setlocal enabledelayedexpansion

replace your corresponding lines with this:

Code: Select all

if !ST! LSS 1000 (
   echo %%~zS %%~nS %%~xS>> D:\scripts\size1.txt
   set /A N1+=1
   set /A T1=!T1!+!ST!
) else (
if %ST% GEQ 1000 (
   echo %%~zS %%~nS %%~xS>> D:\scripts\size2.txt
   set /A N2+=1
   set /A T2=!T2!+!ST!
   )
)
Last edited by RG (11 Dec 2012 04:34)

Windows Shell Scripting and InstallShield

----------------------------

#3 11 Dec 2012 15:33
bluesxman


Or even (also untested):

Code: Select all

for /r "%sti%" %%S in (*.*) do (
if %%~zS LSS 1000 (
    echo %%~zS %%~nS %%~xS>> D:\scripts\size1.txt
    set /A N1+=1
    set /A T1+=%%~zS
  ) else (
    echo %%~zS %%~nS %%~xS>> D:\scripts\size2.txt
    set /A N2+=1
    set /A T2+=%%~zS
  )
)
But bear in mind that you're going to start seeing problems with either of these if you have more than 2GB of data to track. You'll also see odd behaviour it any file has a size of exactly "0", "1" or "2" bytes due to the lack of a "space" before the ">>" operators.

Last edited by bluesxman (11 Dec 2012 15:33)

cmd | *sh | ruby | chef

----------------------------

#4 11 Dec 2012 20:26
maskinmesteren

First of all: Thanks to both of you.
You have to know that english is not my native language (if you have not notised :-) )
However I hope you understand anyway - until now it seems to be the case.

second: I have tried both examples.
The example from bluesxman worked perfectly. The other from RG did unfortunately not work.
However it was worth a try.

RG:
what does the exclamation marks around the variables do (just to know in another situation)

Both of you:
1. How du you make those nice blue frames where you put in code?

2. How do I build-in more IF-ELSE sentences
If I want to separate files like this:
- files up to 1000 byte
- files from 1 kiloByte up to 1 MegaByte
- files bigger than 1 MB

Last:
Thanks for the quick respond.

sincerely
MASKINMESTEREN

----------------------------

#5 11 Dec 2012 23:48
maskinmesteren


I made it work - however I would not have done it without your help :-)

Question:
1. How do I make those nice blue frames/boxes to insert code in?

2. what does the exclamation marks around variables do?

3. comments from Bluesxman:
But bear in mind that you're going to start seeing problems with either of these if you have more than 2GB of data to track. You'll also see odd behaviour it any file has a size of exactly "0", "1" or "2" bytes due to the lack of a "space" before the ">>" operators
I have made spaces before the ">>" operators - why is it that I need to do so?
And in a folder containing files with a size > than 2 GB the program calculates a little bit wrong (about 50 kB) - why and how can I improve this?

Here is my code to calculate number of files and size:

Code: Select all

set /p sti=Angiv stien til den mappe der skal samment‘lles :
for /r "%sti%" %%S in (*.*) do (
    if %%~zS LSS 1000 (
        echo %%~zS %%~nS %%~xS >> D:\scripts\size1.txt
        set /A N1+=1
        set /A TX1=%%~zS
        set /A TX1/=1
        set /A T1+=TX1
    ) else (
        If %%~zS LSS 1000000 (
            echo %%~zS %%~nS %%~xS >> D:\scripts\size2.txt
            set /A N2+=1
            set /A TX2=%%~zS
            set /A TX2/=1
            set /A T2+=TX2
        ) else (
            echo %%~zS %%~nS %%~xS >> D:\scripts\size3.txt
            set /A N3+=1
            set /A TX3=%%~zS
            set /A TX3/=1000
            set /A T3+=TX3
        )
    )
)

set /A N=(N1+N2+N3)
set /A T1/=1000
set /A T2/=1000
set /A T3/=1
set /A T=(T1+T2+T3)

echo Antal og sum af mindre filer >> D:\scripts\summation.txt
echo %N1% >> D:\scripts\summation.txt
echo %T1% >> D:\scripts\summation.txt & echo.

echo Antal og sum af mellem store filer >> D:\scripts\summation.txt
echo %N2% >> D:\scripts\summation.txt
echo %T2% >> D:\scripts\summation.txt & echo.

echo Antal og sum af store filer >> D:\scripts\summation.txt
echo %N3% >> D:\scripts\summation.txt
echo %T3% >> D:\scripts\summation.txt & echo.

echo Antal og sum af alle filer >> D:\scripts\summation.txt
echo %N% >> D:\scripts\summation.txt
echo %T% >> D:\scripts\summation.txt & echo.

start /d D:\scripts size1.txt
start /d D:\scripts size2.txt
start /d D:\scripts size3.txt
start /d D:\scripts summation.txt
endlocal

exit
sincerely
MASKINMESTEREN

----------------------------

#6 12 Dec 2012 13:04
RG


1. Scroll through the help here for info on that and other useful things:
http://ss64.org/help.php#url
You make the blue code box by enclosing your code in ^[code^] and ^[/code^] without the carrets.
Code and a couple ohers are available for you to click on the bottom of the Quick Post window too.
Play with that a little bit and use the 'Preview' tab before you 'Submit' so you can see what it will look like.
2. My code would not work if you did not change your

setlocal

to

setlocal enabledelayedexpansion

The exclamation marks around the variable instead of % indicate that you are using delayed expansion of the variables (expand at run-time instead of at load-time).
This can be useful at times, but in this case you got it working with bluesxman's solution without delayed expansion, so I would stick with that.

Last edited by RG (12 Dec 2012 13:08)

Windows Shell Scripting and InstallShield

----------------------------

#7 12 Dec 2012 17:16
maskinmesteren


Thanks for your reply

I must have read it wrong because I tried

setlocal enableextensions

My fault sorry
Thanks for help with layout on this site - Nice.
How do I solve the calculating problem ?

As I am pretty new within programming i CMD it is easy to see I have a lot too learn :-)

However here is my code:

Code: Select all

echo off
cls
setlocal

for %%A in (D:\scripts\size.txt D:\scripts\size1.txt D:\scripts\size2.txt D:\scripts\size3.txt D:\scripts\summation.txt) DO (
	if exist %%A (
		del /f /s /q %%A>nul
	)
)

set /p sti=Angiv stien til den mappe der skal samment‘lles : 
echo. & echo Vent mens der regnes...
for /r "%sti%" %%S in (*.*) do (
	if %%~zS LSS 1000 (
		echo %%~zS %%~nS %%~xS >> D:\scripts\size1.txt
		set /A N1+=1
		set /A TX1=%%~zS
		set /A TX1/=1
		set /A T1+=TX1
	) else (
		If %%~zS LSS 1000000 (
			echo %%~zS %%~nS %%~xS >> D:\scripts\size2.txt
			set /A N2+=1
			set /A TX2=%%~zS
			set /A TX2/=1
			set /A T2+=TX2
		) else (
			echo %%~zS %%~nS %%~xS >> D:\scripts\size3.txt
			set /A N3+=1
			set /A TX3=%%~zS
			set /A TX3/=1000
			set /A T3+=TX3
		)
	)
)

set /A N=(N1+N2+N3)
set /A T1/=1000
set /A T2/=1000
set /A T3/=1
set /A T=(T1+T2+T3)

echo Antal og sum af mindre filer >> D:\scripts\summation.txt
echo Antal filer %N1% >> D:\scripts\summation.txt
echo Samlet størrelse %T1% kB >> D:\scripts\summation.txt
echo. >> D:\scripts\summation.txt

echo Antal og sum af mellem store filer >> D:\scripts\summation.txt
echo Antal filer %N2% >> D:\scripts\summation.txt
echo Samlet størrelse %T2% kB >> D:\scripts\summation.txt
echo. >> D:\scripts\summation.txt

echo Antal og sum af store filer >> D:\scripts\summation.txt
echo Antal filer %N3% >> D:\scripts\summation.txt
echo Samlet størrelse %T3% kB >> D:\scripts\summation.txt
echo. >> D:\scripts\summation.txt

echo Antal og sum af alle filer >> D:\scripts\summation.txt
echo Antal af alle filer %N% >> D:\scripts\summation.txt
echo Samlet størrelse af alle filer %T% kB >> D:\scripts\summation.txt
echo. >> D:\scripts\summation.txt

start /d D:\scripts size1.txt
start /d D:\scripts size2.txt
start /d D:\scripts size3.txt
start /d D:\scripts summation.txt
endlocal

exit
sincerely
MASKINMESTEREN

----------------------------

#8 12 Dec 2012 18:00
RG


How much accuracy do you need?
With SET /A you are typically limited to 32 bits (2147483647 would be largest number... about 2GB). This may vary by OS.
One thing you can do is truncate your string, and instead of displaying in bytes you would display in bytes, MB, GB, TB, etc... but you lose some accuracy and it is a bit more complicated. How large do you need to go?

Windows Shell Scripting and InstallShield

----------------------------

#9 12 Dec 2012 18:59
dbenham


As far as I know, all Windows versions of CMD use the same 32 bit signed integer implementation.

At DosTips I've posted Rules for how CMD.EXE parses numbers.

The 2GB limit can cause real problems when testing file sizes or free space on terabyte disk drives. On StackOverflow I posted a Q&A regarding this issue: Windows batch file IF failure - How can 30000000000000 equal 40000000000?

Dave Benham

----------------------------

#10 12 Dec 2012 19:34
maskinmesteren


I have a Dell N5010 with Windos 7 (64 bit).
However I get this message when running the program:

Code: Select all

Ugyldigt tal.Tal begrænses til 32-bit præcision.
Can I run the script as 64 bit instead of 32 bit?
I want to be free to select every folder/drive without thinking about the size.
The message shown above came when I tried my D:\ drive (about 385 GB and only filled 195 GB)

I will tjeck out your replies in the meenwhile

sincerely
MASKINMESTEREN

----------------------------

#11 12 Dec 2012 19:36
maskinmesteren


Sorry - the message (in english):

Code: Select all

Invalid number. Numbers will be limited to 32-bit precision.
sincerely
MASKINMESTEREN

----------------------------

#12 12 Dec 2012 19:46
RG

64 Bit/32 Bit CMD won't matter. Still limited to 32 bits

Windows Shell Scripting and InstallShield

----------------------------

#13 12 Dec 2012 22:59
maskinmesteren


I found out that the CMD I run IS 64 bit ("C:\Windows\System32\cmd.exe").
It is possible to run a 32 bit session ("C:\Windows\SysWOW64\cmd.exe").
I don't see the reason why that should be interesting.

Second: The commands you run from a 64 bit cmd version is often 64 bit commands and
The commands you run from a 32 bit cmd version is often 32 bit commands.
You can see this in the tasklist where it is shown as *32 after the process.

DBENHAM:
The link "Rules for..." is apparently what I am looking for - however I have no idea what you meen :-(
Damn it's tough reading (for a novice like me)
I'll have to read that a couple of times or more :-)

RG:
64/32 bit of no importans...
I see what you meen because it is the very calculation that is the problem.
Maybe it is not important - allthough i like precise calculation.

Using the program it find (redirected) 3 big files that is absolutly identical but searching the same folder with DIR results only in 2.
How is that?

sincerely
MASKINMESTEREN

----------------------------

#14 12 Dec 2012 23:33
RG


Check the properties on your 3 files.... maybe one of them is hidden????
Do DIR /S to traverse subdirectories... FOR /R is doing so.

Yes, on a 64 bit system there are 2 CMD.exe's. It does make a difference for some things. If you are accessing the registry it may matter.
Furthermore, there is a 64 bit ODBC and a 32 bit ODBC... if you ever get into that.

************** Added 12/13/2012 **************
%windir%\syswow64\odbcad32.exe <--- this is 32 bit version
%windir%\system32\odbcad32.exe <--- this is 64 bit (native) version
YES that is correct! I did not mistype the above 2 lines smile

For example...
1. Open a 64 bit CMD window and enter the command REGEDIT.
Now expand HKLM\Software and notice that you see a Wow6432Node... which is the 32 bit area of the registry,
2. Open a 32 bit CMD window and enter the command REGEDIT.
Now expand HKLM\Software and notice that you do not see a Wow6432Node... because you have no choice and have been redirected to the 32 bit area.
3. Open a 64 bit CMD window and enter the command ECHO.%ProgramFiles% and you will see C:\Program Files
4 Open a 32 bit CMD window and enter the command ECHO.%ProgramFiles% and you will see C:\Program Files (x86)
So there is some redirection that is done for you.
These are just a few examples.

Last edited by RG (13 Dec 2012 20:25)

Windows Shell Scripting and InstallShield

----------------------------

#15 13 Dec 2012 22:47
maskinmesteren


I used Dir /s but that won't show hidden or system files.
To find that I'll need /as or /ah
The dir /r is to find alternative streams (something I've recently learned what is) - thats nice:-)

Nice thanks for the appetizer with odbcad32.exe and regedit :-)

sincerely
MASKINMESTEREN
Post Reply