Jump to content
Macro Express Forums

How to count files in folders


Recommended Posts

Hi Alexis, try this:

Edited: The variable FileCount is first set to zero.

Replace "C:\\TestFolder" with the path to your own folder.

Cheers

 

<VARIABLE SET INTEGER Option="\x00" Destination="%FileCount%" Value="0"/>
<REPEAT WITH FOLDER Path="C:\\TestFolder" OnlyFiles="TRUE" Destination="%MyStr%" FullPath="FALSE" ProcSubfolders="FALSE"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%FileCount%"/>
<END REPEAT/>

Link to comment
Share on other sites

Just what I was going to suggest but I would add that you don't need to initially set the counter to zero. It's default value is zero even if it doesn't show up that way in the debugger.

 

Alexis you may want to add some conditions. Modern windows OS's often contain hidden and system files like thumbs.db that you do not want to count. There are a hundred ways to skin a cat but I find using a combination of "If File Attribute" Hidden and System usually does the trick.

<IF FILE ATTRIBUTE Filename="c:\\test.txt" Attribute="Hidden"/>
<AND/>
<IF FILE ATTRIBUTE Filename="c:\\test.txt" Attribute="System"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%FileCount%"/>
<END IF/>

Of course you can add conditions forcertian extensions and all kinds of stuff.

Link to comment
Share on other sites

So if you know I knew that why mention it? :) JK. I'm not saying that you can't do it, I just think that in many cases people don't realize you don't need to except in cases like you described. In this case there was no loop. There are several things like that, in fact just the other day I helped a user realize they don't need to include a Macro Return at the end of all their sub-macros.

Link to comment
Share on other sites

Just what I was going to suggest but I would add that you don't need to initially set the counter to zero. It's default value is zero even if it doesn't show up that way in the debugger.

 

Alexis you may want to add some conditions. Modern windows OS's often contain hidden and system files like thumbs.db that you do not want to count. There are a hundred ways to skin a cat but I find using a combination of "If File Attribute" Hidden and System usually does the trick.

<IF FILE ATTRIBUTE Filename="c:\\test.txt" Attribute="Hidden"/>
<AND/>
<IF FILE ATTRIBUTE Filename="c:\\test.txt" Attribute="System"/>
<VARIABLE MODIFY INTEGER Option="\x07" Destination="%FileCount%"/>
<END IF/>

Of course you can add conditions forcertian extensions and all kinds of stuff.

 

I pasted the above code into the latest version of MCP. Clicked the [save] button in the editor and it spit back this:

Line 5: Mismatched 'End If'

Link to comment
Share on other sites

Yeah, I see the same thing. Simply having a single If File Attribute followed with an End If causes it as well so it's not the AND. I'll report it as a bug. Whatever you do don't try to run that script, it will crash MEP!

 

BTW, I made a mistake, there should be Else in there so we count the visible non-system files. That is of course if the AND was working.

Link to comment
Share on other sites

  • 2 years later...

Wonderful - i searched for this for a while.

But what i need is a little bit more. Maybe you can help me for this.

 

Ok i created a FileCOunt.

 

this works fine but now comes the difficult problem. I want to show it but not in a Text box.

There are several possibilities but i do not know what works and what not.

 

So

Possibility 1 (like most)

can place a icon in the taskbar wo tells me how many files are in the folder

 

but i think this can not work (i had a program named samurize there it worked but samurize will not run under Win 7 64 bit in the taskbar)

2.

this could maybe work and will be ok for me

changed an icon in the taskbar depending of how many files are in the folder ( means if there are 50 files the icon changed to lets say 50.ico and if there are 1000 it will change to 1000.ico or similiar.)

if this will not work in the taskbar maybe it can work on the desktop?

 

help would be great

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...