Recursive Actions

(imported topic written by RichB91)

I have a nagging feeling that the code we’re using is just not being efficient. In several places, I have code that does two or more of the same actions just with different folder names (or *.exe names, or processes, or Reg entries, or…)

Using the example below that tests for the existence of three different directories and creates them if they aren’t present, it is being done with three separate If/Then constructs.

Is there a way, possibly using RegEx or some kind of

It Of List of Folders

, to perform these three steps with fewer lines of code?

Again, this particualr example isn’t really a serious problem. It’s just three short If/then statements. But sometimes there are five to ten items in the list that need the same action and I’d really rather not duplicate the code so often. It makes future edits highly problematic.

// Do not continue unless the OS is Windows

Continue If {name of operating system as string as lowercase contains “win”}

// Set Task parameters

Parameter “DirScripts” = “C:\companyname_Tools\BigFix\Scripts”

Parameter “DirArchives” = “C:\companyname_Tools\BigFix\Archives”

Parameter “DirLogs” = “C:\companyname_Tools\BigFix\Logs”

// If companyname proprietary BigFix Script directory doesn’t exist, create directory

If {not exists folder (parameter “DirScripts”)}

waithidden cmd.exe /C mkdir “{parameter “DirScripts”}”

endif

// If companyname proprietary BigFix Archive directory doesn’t exist, create directory

If {not exists folder (parameter “DirArchives”)}

waithidden cmd.exe /C mkdir “{parameter “DirArchives”}”

endif

// If companyname proprietary BigFix Log directory doesn’t exist, create directory

If {not exists folder (parameter “DirLogs”)}

waithidden cmd.exe /C mkdir “{parameter “DirLogs”}”

endif

(imported comment written by SystemAdmin)

Something like this might be what you’d prefer:

//-- Set up parameter containing the commands needed to create any directories that don’t exist and then run

//-- the .bat file that gets created.

Parameter “Script_code” = “{(“mkdir %22” &(concatenation “%22%0d%0amkdir %22” of (it whose (not exists folder it) of (“c:\temp”;“c:\temp\test”;“c:\temp\test2_doesntexist”;“c:\temp\test3_doesntexist”))) & “%22”)}”

delete __appendfile

appendfile @echo off

appendfile {Parameter “Script_code”}

copy __appendfile create_directories.bat

waithidden create_directories.bat

You could get a bit cleverer about the relevance statement (just make it a list of the paths and get the relevance in the appendfile section to juggle the data).

For me, the fact that you have 8 lines of comments/parameters and only 10 lines of useful code (which seem too obvious to require commenting in such detail) makes me think that the slightly verbose if/endif statements aren’t really the problem. And I wouldn’t normally have the comment I listed above, when I do, I tend to find that a succinct statement above a number of commands is helpful enough for later editing.

Even if your list is massive, that code above will only be 11 lines (or a bit more if your parameter line wraps). In a similar fashion, you should be able to condense many repetitive code statements down to a few lines in this manner.

Hope this helps.

-Jim