Jump to content
Macro Express Forums

Size of folder/Recognize new files added


patgenn123
 Share

Recommended Posts

I know Macro Express Pro has this, but is there anyone that might know how to create a macro that every so often can either a) count the number of files in a folder and be able to recognize that new files have been added or B) Count the size of the folder and be able to recognize the most recently added files in that folder?

 

Pat

Link to comment
Share on other sites

Counting files in a folder, using ME 3......

 

 

Hi Pat,

 

I love questions that make me learn new ME tricks. This is one of them. It wasn't terribly difficult, to do, either. My code uses Registry reads and writes, but those can be easily replaced by .txt or .ini files if you're hesitant about registry - I was until only a few months ago. They aren't nearly as scary as I had previously thought, if you don't mess with any system values. Stick within your own home-made registry folders, and you should be fine.

 

This particular macro will prompt you to point to the folder you want to have counted, then it will count the files within that folder. It isn't set to count the files within sub-folders, however.

 

If you know which folder will need to be counted every time, then you can eliminate the prompt.

 

Please note that if you use the prompt, the macro will record different values for each folder you count. This way if you have to count the contents of multiple folders, you only need one macro and it will never confuse the contents of any two folders (unless you somehow have two folders with the same name on the same file-path... though I'm pretty sure that isn't possible).

 

Here's the code:

Variable Set String %T1% from Folder Name
Read Registry Integer: "CountFiles%T1%"
Repeat with Folder
 Variable Modify Integer: Inc (%N2%)
Repeat End
Write Registry Integer: "CountFiles%T1%"
If Variable %N1% = variable %N2%
 Text Box Display: No New Files
Else
 If Variable %N1% > variable %N2%
Text Box Display: Some Files Removed
 Else
Text Box Display: New Files Detected
 End If
End If

 

To avoid text-wrapping conflicts, I'll provide the DE code as an attachment.

Count_Files.mex

Link to comment
Share on other sites

I think I might know. Have a macro run that adds all the names to a text file and also have a counter. Compare the text file after the first run to the next run and if the first run text file has less than the second run(and so forth) then copy the names of the missing files to a new variable and there's your difference!

 

The only question is: I don't know how to use the registry like you did. How can you stuff large amounts of text into the registry?

 

Pat

Link to comment
Share on other sites

Ok, Pat, you made me work for this..............

 

Very good. The question then becomes, what files were recently added? I really should have been more clear.

Here's what I've got. It works, and I like it. However, there may be a more elegant way (I always feel like that when there are multiple If/Else/End If sequences together).

 

It does more than you asked for:

  1. If the number of files match, it tells you if any of them are different. It specifies which files have been removed and which ones are new
  2. If there are fewer files now than before, it tells you which ones have been removed.
  3. If there are more files than previous runs, it tells you which ones are new.

I didn't take the next logical step, which would be to point out if files were removed, even if there are more files now than before; and likewise to point out if new files were added, even if there are fewer items now than previously.

 

Using the logic of the macro as it is, I am sure it won't be too hard to figure out how to do that.

 

Switch / Case functions might be useful for streamlining this macro, but I honestly have no idea how their logic works. As it stands, this macro works great, if a little longer than expected (considerably longer than the original).

 

I'm putting the attachment before the Script because... well, the script is LONG (even more now that I've added Remarks):

 

Count_Files.mex

 

// T1 - Folder Path
// T2 - File Name(s) within Folder (T1) - Only 1 at a time
// T3 - Previous Contents of Folder (T1)
// T4 - Current Contents of Folder (T1)
// T5 - Receiving Variable for Text File Processes
// T5 (continued) - Missing and/or New Items
// T10 - File Name
// N1 - Number of Previous Items
// N2 - Number of Current Items

// Select Folder
Variable Set String %T1% from Folder Name
Variable Set String %T10% from Environment Variable
Convert Filename "%T10%" to a long filename and store in %T10%
Delete File or Files: "%T10%Temp1.txt"
Delete File or Files: "%T10%New Items.txt"
Delete File or Files: "%T10%Missing Items.txt"
// Read Registry
Read Registry Integer: "CountFiles%T1%"
Read Registry String: "StoreFiles%T1%"
// Process Items in Folder
Repeat with Folder
 Variable Modify Integer: Inc (%N2%)
 Variable Modify String: Append %T2% to Text File
Repeat End
Variable Set String %T4% from File: "%T10%Temp1.txt"
// Write New Info to Registry
Write Registry Integer: "CountFiles%T1%"
Write Registry String: "StoreFiles%T1%"
Variable Modify String: Save %T3% to Text File
// Detect New Items
Text File Begin Process: "%T10%Temp1.txt"
 If Variable %T3% does not contain variable %T5%
Variable Modify String: Append %T5% to Text File
 End If
Text File End Process
// Detect Missing/Removed Items
Text File Begin Process: "%T10%Temp3.txt"
 If Variable %T4% does not contain variable %T5%
Variable Modify String: Append %T5% to Text File
 End If
Text File End Process
// Establish Results
// ~~~1. Same number of Items
If Variable %N1% = variable %N2%
 // ~~~1a. Items do not match 100%
 If File Exists "%T10%New Items.txt"
Text Box Display: Files Changed
Program Launch: "%T10%New Items.txt"
Window Reposition: Top Left - New Items.txt
Variable Set Integer %N10% from Width of Window
Program Launch: "%T10%Missing Items.txt"
Window Reposition: Missing Items.txt - (Left: %N10%, Top: 0)
 Else
// ~~~1b. Perfect Match
Text Box Display: No New Files
 End If
Else
 // ~~~2a. More Previous Items Than Current
 If Variable %N1% > variable %N2%
Text Box Display: Some Files Removed
Program Launch: "%T10%Missing Items.txt"
Window Reposition: Top Left - Missing Items.txt
 Else
// ~~~2b. Fewer Previous Items Than Current
Text Box Display: New Files Detected
Program Launch: "%T10%New Items.txt"
Window Reposition: Top Left - New Items.txt
 End If
End If
// Delete Temp1.txt but leave the other Temp files for reference
//  (until macro runs again and they are deleted).
Delete File or Files: "%T10%Temp1.txt"

Link to comment
Share on other sites

Registry: A New-ish File System.........................

 

The only question is: I don't know how to use the registry like you did. How can you stuff large amounts of text into the registry?

I may be wrong about this, but the Registry is essentially a file-system like any other on your computer. But instead of being dependent on C:/ D:/ etc. and file paths set up on the surface, the paths take a back-door approach instead.

 

It's this "back door" that makes them a little more... um... risky isn't the right word. But you want to be cautious when you go in the back door, because that is also how you get to a lot of very sensitive system files that you really don't want to mess around with. Unless you are a real programmer.

 

So, when you go in the back door, you stay away from the folders that you don't know anything about (in my case: all of them), and you just create new paths which create new folders - the contents of which are all yours, to do with as you please.

 

To create these paths using ME, you just go to the System commands, select Read or Write Registry (in ME3, you have to specify whether it's a decimal, integer, or string - MEP does away with these distinctions). Then you just Browse to the path where you want to set up your personal Registry folder... I've heard it recommended to use the HKEY_CURRENT-USER (HKCU) folder, which is the one I use. You can also use HKEY_LOCAL_MACHINE (HKLM). The difference being that HKCU is accessible to you only, while HKLM is accessible to anybody using your machine.

 

Then you create the rest of the path by typing it out. Example, if you open the HKCU folder, you get a slew of system folders that you don't want to touch. So just make sure you've selected the HKCU, so the path bar says:

 

Path: HKEY_CURRENT_USER\

 

And now you just add to that the path you want to create. Example:

 

Path: HKEY_CURRENT_USER\ME3Variables\TestFolder\

 

This will create the new sub-folder under the HKCU: ME3Variables and a sub-folder to it: TestFolder

 

Note: I don't use spaces in my Registry folder and file names... I am certain you can... because some of the built-in system files do... but I didn't know that for sure when I started, now I have a habit.

 

Easy as pie. Easy as eating pie. Easy as throwing a pie in the face of %politician of your choice%.

 

Anyway, I'm far from an expert, but I was terrified of using the Registry, until a few helpful posters on these boards were able to lift the veil of ignorance from my eyes a little at a time. Now I just use caution with it - like anything in life that is a little dangerous, but extremely useful (fire, knives, and Axe Body-Spray come to mind).

 

If you want to change the Read/Write commands, you can. You can make the variables pull from (using Set Variable from file) and save to (using the 2nd tab of the Modify String command) .txt files. Your results will be essentially the same. I've just gotten into the habit of using the Registry, because it's easy, and possibly faster.

Link to comment
Share on other sites

I know Macro Express Pro has this, but is there anyone that might know how to create a macro that every so often can either a) count the number of files in a folder and be able to recognize that new files have been added or B) Count the size of the folder and be able to recognize the most recently added files in that folder?

As an alternative, try FileNotify

Link to comment
Share on other sites

As an alternative, try FileNotify

 

I suppose if you're a SysOp or similar, but otherwise I'd say: "Only if you're brave!" What an obscure installation process! This is what I saw running install.bat. After entering a username and password half way through I got the unhelpful message that "System.ComponentModel.Win32Exception: No mapping between account names and security IDs was done".

 

In case you're curious, here's the full text displayed:

 

C:\Program Files\FileNotify2>C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\insta

llutil.exe fnService.exe

Microsoft ® .NET Framework Installation utility Version 2.0.50727.3053

Copyright © Microsoft Corporation. All rights reserved.

 

 

Running a transacted installation.

 

Beginning the Install phase of the installation.

See the contents of the log file for the C:\Program Files\FileNotify2\fnService.

exe assembly's progress.

The file is located at C:\Program Files\FileNotify2\fnService.InstallLog.

Installing assembly 'C:\Program Files\FileNotify2\fnService.exe'.

Affected parameters are:

logtoconsole =

assemblypath = C:\Program Files\FileNotify2\fnService.exe

logfile = C:\Program Files\FileNotify2\fnService.InstallLog

 

********************************************

Here I entered a username and password

********************************************

 

An exception occurred during the Install phase.

System.ComponentModel.Win32Exception: No mapping between account names and secur

ity IDs was done

 

The Rollback phase of the installation is beginning.

See the contents of the log file for the C:\Program Files\FileNotify2\fnService.

exe assembly's progress.

The file is located at C:\Program Files\FileNotify2\fnService.InstallLog.

Rolling back assembly 'C:\Program Files\FileNotify2\fnService.exe'.

Affected parameters are:

logtoconsole =

assemblypath = C:\Program Files\FileNotify2\fnService.exe

logfile = C:\Program Files\FileNotify2\fnService.InstallLog

 

The Rollback phase completed successfully.

 

The transacted install has completed.

The installation failed, and the rollback has been performed.

 

C:\Program Files\FileNotify2>net start FileNotify2

The service name is invalid.

 

More help is available by typing NET HELPMSG 2185.

 

 

C:\Program Files\FileNotify2>pause

Press any key to continue . . .

 

I like Steve's better!

;)

 

--

Terry, East Grinstead, UK

Link to comment
Share on other sites

Empty Folders (and other missing files), the bane of my macros.........

I tried it (in ME Pro) . One small point: It doesn't like empty folders, which give: "Line 27: Debug Error: The file could not be located."

Nice find, Terry!

 

I hadn't even thought about running it against an empty folder. I just tried it with ME3, and though I don't get a "Line 27" error, I do get a "file cannot be found" error (the temp1.txt file).

 

That's something that I think could be fixed with a brief "if not file exists / create file" sequence, but I'd have to look into it a little further.

Link to comment
Share on other sites

I suppose if you're a SysOp or similar, but otherwise I'd say: "Only if you're brave!" What an obscure installation process! This is what I saw running install.bat. After entering a username and password half way through I got the unhelpful message that "System.ComponentModel.Win32Exception: No mapping between account names and security IDs was done".

...

...

I like Steve's better!

Well, I've installed and used this utility on several machines with absolutely no problems. My reaction, Terry, is the same as Cory's (I think it was Cory) recently - your machine is wondrous strange and produces errors in many circumstances which appear unique to you and your configuration. If ever there was a candidate for a complete rebuild of everything (OS and all apps) from scratch, I'd say yours would be the one! :rolleyes:

Link to comment
Share on other sites

  • 2 weeks later...

It's an interesting error. In my code Line 21 is a Remark, which means it has no value whatsoever to the actual functioning of the macro. Since your error is providing a "line" error, I'm presuming that means you're using Macro Express Pro (since I've only gotten errors specifying line numbers in Pro), and this macro was specifically built for Macro Express 3.7d. So I'm not really in a position this very minute to test and find the cause of the error you've experienced.

 

However, my first guess is that when selecting the file from the original prompt (line 12 in the original code), you are selecting an incorrect file... or, since you said the file causing the error doesn't even exist on your machine, the code following line 12 - the code that chnges the name of the file - has an error in it, changing the name of the file to .db3

 

Of course, I don't know for certain. Export the code, and attach it to a reply so that we can look at it exactly as you have it. Also, be sure to provide the actual Macro Express version you are using. I'll be happy to look into it.

Link to comment
Share on other sites

  • 3 months later...

As a quickie for folder size I used GetDirSize in AutoIt (can return size, # of files, # of folders). If you are passing the foldername in a variable from ME (no folder selection dialog needed), it's convenient. Even more convenient in MEPro (External Script). A couple of lines in AutoIt. Easy to add foldername select in ME and display results in ME Text Box if desired.

Link to comment
Share on other sites

Registry: A New-ish File System........................

 

I know that some forum members are very enthusiastic about using the registry! I usually prefer text files because you can read them any time you want with ease. Most of us are not aware of all the implications of registry entries. In the case of your macro (correct me if I'm wrong), it appears to write the filenames of every file in every folder that you operate the macro on, into the registry, permanently. Each of those could be 25k filenames (unlimited). Is the registry really intended for storage of huge quantities of information? I think of the registry as holding ini values, mrus, system info and that sort of thing. Not a bulk storage location. Comments?

Link to comment
Share on other sites

I know that some forum members are very enthusiastic about using the registry! I usually prefer text files because you can read them any time you want with ease. Most of us are not aware of all the implications of registry entries. In the case of your macro (correct me if I'm wrong), it appears to write the filenames of every file in every folder that you operate the macro on, into the registry, permanently. Each of those could be 25k filenames (unlimited). Is the registry really intended for storage of huge quantities of information? I think of the registry as holding ini values, mrus, system info and that sort of thing. Not a bulk storage location. Comments?

Agreed - I wish software designers would share your opinion!

Link to comment
Share on other sites

It brings up the issue of what else is in the registry. In my limited travels through the registry I've not come across anything that "goes off the page". Now I wonder. Someone needs to write a macro to find the longest key in a registry!

Some binary and multi-string values get pretty long (or are you referring to the length of a key name?).

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

×
×
  • Create New...