MSFN Forum: need small script to check size of folders run at cmdline.. - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

need small script to check size of folders run at cmdline.. Rate Topic: -----

#1 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 02 September 2008 - 08:39 PM

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!

This post has been edited by TranceEnergy: 02 September 2008 - 08:43 PM



#2 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 03 September 2008 - 05:52 AM

...as clear as mud :whistle:

Please try again!

#3 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 03 September 2008 - 07:35 AM

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.

#4 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 8,792
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 03 September 2008 - 08:49 AM

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

#5 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 03 September 2008 - 11:09 AM

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?

#6 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 03 September 2008 - 11:12 AM

The fastest and most suitable method I'd suggest for this task is to use a dedicated utility!

I'll put forward Sysinternals DU (Disk Usage)
@Echo off
(Set D_=C:\Documents and Settings\MyDir)
Du /q "%D_%"|Find "0 bytes">Nul 2>&1&&(Rd/s/q "%D_%")


#7 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 03 September 2008 - 12:29 PM

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.

This post has been edited by TranceEnergy: 03 September 2008 - 12:36 PM


#8 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 8,792
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 03 September 2008 - 12:53 PM

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:

Quote

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

This post has been edited by jaclaz: 03 September 2008 - 01:12 PM


#9 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 03 September 2008 - 01:01 PM

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.

This post has been edited by TranceEnergy: 03 September 2008 - 01:21 PM


#10 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 8,792
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 03 September 2008 - 01:31 PM

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

#11 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 03 September 2008 - 02:31 PM

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

#12 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 03 September 2008 - 07:35 PM

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.

This post has been edited by TranceEnergy: 03 September 2008 - 07:38 PM


#13 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 8,792
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 04 September 2008 - 12:25 AM

@TranceEnergy

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

You just need to read these five pages:
http://www.robvander...edirection.html
http://www.robvander...edirexampl.html
http://www.robvander...m/condexec.html
http://www.robvander....com/ntfor.html
http://www.robvander...tfortokens.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

#14 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 04 September 2008 - 09:00 AM

Bookmarked them in new folder called commandline in portable firefox. I'll have a look after i get some food.

#15 User is online   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,117
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 04 September 2008 - 09:25 AM

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!

#16 User is offline   TranceEnergy 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 963
  • Joined: 23-March 06

Posted 04 September 2008 - 10:17 AM

Interesting idea Yzowl, however the directories i have never had a problem with them being recreated, they are merely a culprit of the windows installation and nlite renaming the folders.
I mean: The directories are there upon first boot after install.
I've tried telling nuhi several times over the years, but it seems to be a problem that just wont go away. And i dont really know what causes it any more, but i know that nlite doesnt rewrite or fix all files to the new
program files/64 bit & x86 path.

Still, i think i might attempt that. This makes me think further. You know desktop.ini, well it is hidden by registry, and some of them can not be found with windows explorer, you need 3d party tools to see them all.
So possibly find what registry key can help with that and make these two empty files with the names of "program files" and "program files (x86)" hidden from there.
I mean so you can't see them even if you have enabled to see all hidden and system files in windows explorer.

Having said that, it would be nice to really be able to get rid of all desktop.ini files.

This post has been edited by TranceEnergy: 04 September 2008 - 10:18 AM


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy