Jump to content

need small script to check size of folders run at cmdline..


TranceEnergy

Recommended Posts

Hello.

I've seen some examples of this elsewhere but i thought i would ask for something that does exactly what i need.

I would like to have a small IF script that deletes 2 folders, "program files (x86)" and "program files (x86)(x86)"

If they have folders in them it should not matter, as long as the total size is 0 bytes, meaning there could be files in them, but again, the total size should be 0 bytes.

I hope that was understandable.

Thanks!

Edited by TranceEnergy
Link to comment
Share on other sites


Really?

Okay trying from different angle.

Need a script that checks if total size of a folder, with subfolders, is more then 0 bytes.

If it is 0 bytes (meaning there can be files but size is 0 bytes nevertheless) then it deletes the folder.

If it is 1 bytes or more it doesnt get deleted and just quits.

Link to comment
Share on other sites

Try this:

1) create a New folder, say Y:\empty

2) create a new folder, say Y:\full

3) copy any file(s) to Y:\full

4) Open a command prompt and give in it this command:

DIR Y:\empty

5) now try with

DIR Y:\full

See the difference?

Now, try runnig this:

@ECHO OFF
FOR /F "tokens=1,2,3,4 delims= " %%A IN ('DIR Y:\full') DO (
IF %%B==File (
IF %%A.==0. (ECHO NO files) ELSE ( ECHO %%A File )
IF %%C.==0. (ECHO NO bytes ) ELSE ( ECHO %%C bytes )
)
)

The problem with this approach is with sub-subdirectories, i.e. if there is in the folder:

- no files

- a subfolder

- files in the subfolder

the program won't work correctly, but since you actually need only to check if the total is 0 bytes, you can use the DIR /S like in this:

@ECHO OFF

FOR /F "tokens=1,2,3 delims= " %%A IN ('DIR /S Y:\full 2^>^&1') DO (
IF %%B==File SET Totalbytes=%%C
)
ECHO %Totalbytes% bytes
IF %Totalbytes%.==0. ECHO It's safe to delete

the above still needs to be tested, (the redirection is needed for completely empty folders, in which case the DIR command will return a "File not found"), but you should have a base to work on.

jaclaz

Link to comment
Share on other sites

jaclaz:

I tried running that last script and the only output i got was " bytes", i added a pause to it and runned it as a batch file so i could see the output.

It should be runnable as a batch file btw..

You see i run xp x64 and when renaming the program files folders with help of nlite or otherwise, the setup creates 2 empty folders named "program files (x86)" and "program files (x86)(x86)".

Now they always are emtpy 0 bytes, but in the event i install a program that somehow manages to put files in them, it wouldnt be cool if they were just deleted, which i have usually always done automatically with RD command.

So what now?

Link to comment
Share on other sites

I copied the following to a new batch file called test.bat at c:\ in vmware

@Echo off

(Set D_=c:\test)

Du /q "%D_%"|Find "0 bytes">Nul 2>&1&&(Rd/s/q "%D_%")

and then i have a empty folder called test

now nada happens.

I also tried with system path variable (%systemdrive%\ instead of c:\).

I see you wrote Rd/s/q so i tried with a space there, Rd /s /q , which didnt change outcome, and welll.

I've checked that find command exists and replies so hm...

What am i doing wrong?

and yes i had copied du.exe to vmware's system32. first.

edit2:trying with -q instead... edit3: i tried all sorts of combinations, removing spaces and what not. it doesnt work.

Edited by TranceEnergy
Link to comment
Share on other sites

If I may :) , you seem to have a "wrong" approach to the problem.

The idea is not that one posts a code snippet and it will work "as is" on any system, with any settings, whatever.

The snippet works on my Win2K system, Italian.

The idea is to give an example that you should study, understand, and if needed adapt, integrate and modify to your situation, adding error checks, etc.

Did you do exactly what I suggested before?

i.e. creating the Y:\full directory and putting in it an empty file and an empty folder?

Then running from command line the "simple" command?

Or did you just copied and pasted the snippet and ran it?

Testing the snippet by Yzöwl, did you simply tried before the single commands from command line, like:

Du /q "C:\test"

Of course this is my personal take on the matter, see also the reply I gave here:

http://www.msfn.org/board/Batch-file-to-ed...nf-t122652.html

I mean, if I read someone writing something like:

the above still needs to be tested, (the redirection is needed for completely empty folders, in which case the DIR command will return a "File not found"), but you should have a base to work on.

I see four possibilities:

1) ALREADY know what that sentence means and what "redirection" means

2) try and find some documents/example to understand the sentence and what "redirection" is

3) ask to the OP :

What is "redirection"?

What do you mean?

4) This kinda stuff is too much for me, I give up

You seem to have chosen a fifth way:

5) Ignore whatever the OP wrote, just copy and paste the lines and see if it works..... :w00t:

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

jaclaz:

Yes i tried that. If i hadnt then i wouldnt have posted back that it didnt work eh, as in it doesnt remove the folder that is empty.

The script Yzowl posted seems to make sense to me, and since it doesnt work, im kinda clueless for the time being. I've tried removing various symbols and so on, and i dont get any effect.

I know that DU.exe can work and do the job because it outputs info with the example you just posted.

I honestly can't see where the error is.

editIt looks like the output of find goes to nul which is what? now outputting to nil: on amiga would just make it disappear. nul means errors are hidden, hidden how?

If i just run Find "0 bytes" , find starts and seems to do nothing, and output is nil, lol.

I didn't realize i had to sign up to dos classes to get help. I wont bother this forum with such questions again.

Edited by TranceEnergy
Link to comment
Share on other sites

Sorry, we were cross-posting.

My snippet is not intended to delete anything.

It is ONLY intended, if token 2 equals to "File" AND token 3 equals to "0" to output the number of bytes.

If the output of the DIR command NEVER has "File" as token 2, the variable %Totalbytes% will never be SET.

The (better) lines from Yzöwl use the FIND utility to search for "0 bytes" in the output of Du /q, if this output does not contain "0 bytes" it won't do anything, viceversa, if "0 bytes" is found, it will Remove the Directory.

In both cases no output will be displayed.

jaclaz

Link to comment
Share on other sites

The reason you've got nothing happening from using my example is because you've got no files in the directory structure you're testing.

DU does not list a file size if there are no files to list, (my mistake was to believe that you had some 0 byte files regardless).

The fix for that scenario would be to replace Find with Findstr and look for both return outputs!

@Echo off
(Set D_=C:\Documents and Settings\MyDir)
Du /q "%D_%"|Findstr "0\ bytes No\ matching">Nul 2>&1&&(Rd/s/q "%D_%")

I hope this satisfies you a little better!

Additional Note:

Although difficult to tell from the written word, it does appear that your responses are a little over-aggressive.

I have tried to give you help on more than one occasion recently and although I've done so without question, it does appear that you're less than thankful of the help you are receiving.

I would appreciate it if that trend didn't continue

Link to comment
Share on other sites

jaclaz on post #8, no i didnt just copy &paste and cross fingers, i tried modifying it, but to no avail.

That you consider me ignorant, well thanks.

Yzowl:

Do not confuse my disappointment in myself with you or any one else. Sometimes it's just frustrating when in my perspective at least i try and make it work, and nada.

I appreciate your efforts i really do :wub: , but people learn things differently, i learn better from seeing how things work in the example i set forth. I wrote that i specfiically had two folders named what i said, so i dont know how much more better i can give a real world example.

FYI i was writing arexx scripts, that did these kind of operations back in the days on bbs's, but that was a long time ago, and i'm more into other stuff like photoshop etc, iow scripting doesnt really interest me anymore, i just had this particular task it would be great to just get done in a safer manner.

I see many people asking for help, and why can't i thought.

I found some links on the subject syntaxes and all on ss64.com, maybe i can read up when i have a moment.

Edited by TranceEnergy
Link to comment
Share on other sites

@TranceEnergy

NO intention whatsoever to offend you in any way, believe me. :)

You just need to read these five pages:

http://www.robvanderwoude.com/redirection.html

http://www.robvanderwoude.com/redirexampl.html

http://www.robvanderwoude.com/condexec.html

http://www.robvanderwoude.com/ntfor.html

http://www.robvanderwoude.com/ntfortokens.html

To be able to understand the batch files, only the first three if you just want to understand the ones by Yzöwl.

As soon as you have the time for doing so, you will see that everything will become clear, why the batches did not work for you, and how they can be changed to make them work.

jaclaz

Link to comment
Share on other sites

I've had another thought, first though how did you get on with my replacement code?

After removing the directories, create zero byte files, possibly even with 'read only', 'hidden' or 'system' attributes and give them the names of those directories. What will happen then is that the system will be unable to re-create those directories due to the names already existing and will of course not be able to add files to them either!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...