Intro Download and install Frequently Asked Questions Tips and tricks

Homepage







© J.C. Kessels 2009
MyDefrag Forum
May 24, 2013, 12:38:53 am *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Macros not working in included scripts? [solved in 4.2.5]  (Read 1510 times)
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« on: October 22, 2009, 12:49:57 am »

Hi.

I'm using MyDefrag 4.2.3 and trying to write some more complex scripts with least redundancy.
So i wrote a subscript for every subtask, that is done in at least two different scripts.

Now I'm planning to make the three scripts for defragmenting daily, weekly and monthly even one script. The main difference between these standard-scripts is the way to defragment. So I wrote a defragment script for each of the three scripts and named them after the strategy. Now I only need to include Daily.MyD, Weekly.MyD or Monthly.MyD in one script at several locations. To get the name of the script to include I declared a user-defined macro "Strategy".
This looks like
Code:
FileActions
  !include !Strategy!.MyD!
FileEnd
in the script and works as expected.

The Problem is, that I like to do this also in sub-scripts but there the macro isn't replaced. As I told at the beginning I'm using sub-scripts for special sub-tasks, which means I have a script for something that is done in every script before the volume-processing, at the beginning and at the end of every volume. I'm glad the !include!-macro works in included scripts but other macros don't seem to work (I only tested !VolumeType!).

If I call
Code:
Title("Optimize !Strategy!")
in the main-script it works, but calling this in my pre-volume-processing-sub-script doesn't work.
Code:
Title("Optimize !VolumeType!")
also doesn't work in the sub-script - needless to say this makes not much sense, but for testing.

Am I doing something wrong or is this just not implemented/meant to work.

Surprisingly all macros are working in the standard-sub-script Settings.MyD.
« Last Edit: October 31, 2009, 05:50:51 pm by MacJudge » Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7155



View Profile WWW
« Reply #1 on: October 22, 2009, 09:34:25 am »

I have taken a look at the MyDefrag sources and see that the !include! macro is the very last macro to be processed. So macros in the included file are indeed not replaced, as you have noticed. I will change this in the next version, by simply making the !include! macro the very first macro to be processed.
Logged
RobL
JkDefrag Senior
****
Posts: 21


View Profile
« Reply #2 on: October 23, 2009, 05:20:24 am »

My setup (MyDefrag 4.2.3) uses nested includes and macros within the include name in both the first level and second level include and they work.
Jeroen, congratulations for writing an excellent utility.  Will your proposed change break my includes because my include names are not fixed and are built by macros whose values come from environment variables set in the batch file that starts it all?  I just use one script varied by variables in the batch file for different combinations of drives/actiontype.  Please see sample below:

"$ OptimizeMonthly C.CMD" :
   set MyDriveLtr=C
   set MyDefragActn=OptimizeMonthly
   MyDefrag.exe  -r "Scripts\$OptimizeMyDriveLtr.MyD"
   notepad ".\Logs\$%MyDefragActn% %MyDriveLtr%.log"

   set MyDriveLtr=C
   set MyDefragActn=OptimizeNear3GB
   set MyOptFreq=OptimizeMonthly
   MyDefrag.exe  -r "Scripts\$OptimizeNear3GBInMyDriveLtr.MyD"
   notepad ".\Logs\$%MyDefragActn% %MyDriveLtr%.log"

"$OptimizeMyDriveLtr.MyD" :  (part of)
  ...
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Zone 6 - Regular Files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  Message("Zone %1u of %2u","Zone %1u of %2u (Regular Files)")
  FileSelect !Include Includes\$RegularFilesIn!MyDriveLtr!.txt!
    FileActions
      !Include Includes\FileActions-!MyDefragActn!.txt!
      !Include Includes\AddGap-Medium.txt!
      !Include Includes\AppendLogfile-Regular Files.txt!
  FileEnd
  ...

Includes\FileActions-Boot-OptimizeMonthly.txt :
    SortByImportSequence(Ascending)


Includes\$RegularFilesInC.txt :
    not(
      Size(50000000,0)
      or (LastAccessEnabled(yes) and LastAccess(,1 month ago))
      ...
      or FullPath("C:\WINDOWS\system32\SoftwareDistribution","*")
      or FullPath("C:\WINDOWS\system32\URTTemp","*")
      )

Includes\AppendLogfile-Regular Files.txt :
    AppendLogfile(".\Logs\$!MyDefragActn! !MyDriveLtr!.log","Regular Files
    !Include Includes\ZoneStatistics.txt!
    ------------------------------------------------------------------------------
    ")
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7155



View Profile WWW
« Reply #3 on: October 23, 2009, 08:03:33 am »

Will your proposed change break my includes
From your example I see that the macros in the included files are all used in an AppendLogfile command. This command will feed it's input through the macros, and this is why it works in your case. The change in the next version should not change how your scripts work, it will just replace the macros sooner.
Logged
RobL
JkDefrag Senior
****
Posts: 21


View Profile
« Reply #4 on: October 23, 2009, 12:12:51 pm »

Thanks for the quick reply.
There is also !MyDriveLtr! inside the line "  FileSelect !Include Includes\$RegularFilesIn!MyDriveLtr!.txt! "
and !MyDefragActn! inside the line "  !Include Includes\FileActions-!MyDefragActn!.txt!  "
Logged
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« Reply #5 on: October 27, 2009, 11:29:10 pm »

Thanks Jeroen for the new version.

I tested my Scripts with 4.2.4 but nothing changed for me.
I've the same problems like in my first post.

I've attached my modifications to the standard-scripts, so if somebody likes to try them and search for the error...

* Scripts.zip (2.81 KB - downloaded 72 times.)
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7155



View Profile WWW
« Reply #6 on: October 28, 2009, 09:50:16 am »

I've attached my modifications to the standard-scripts
I have taken a look and I see that you are using a macro as the filename in a !include! macro (i.e. "!include !Strategy!.MyD!"). I am sorry but MyDefrag is not smart enough for that. You will have to do something else.

Tip: The "MyDefrag.debuglog" contains the expanded script, look for the line number there.
Logged
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« Reply #7 on: October 28, 2009, 07:56:04 pm »

Ok, I see, you just try to replace something between two exclamation-marks as a macro. That explains why MyDefrag always complains about not finding a script called like the directory of my scripts.

I've misinterpreted the sentence "Macros may be nested, that is, the VALUE of a macro may contain another macro" but as I can read now you emphasized something important.

I can imagine you have more important things on your todo-list than to write something more greedy and intelligent... but perhaps you find some time in the future.

So that's the answer, why
Code:
!include !Strategy!.MyD!
is not working, but what's with
Code:
Title("Optimize !Strategy!")
in the sub-script? As I've read in the MyDefrag.debuglog, this isn't replaced in the expanded script, too.
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7155



View Profile WWW
« Reply #8 on: October 28, 2009, 09:18:43 pm »

The new version first expands the "!include !Strategy!.MyD!" macro. But it cannot find the filename, so the program exits with an error message. It never gets to expanding the other macros.
Logged
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« Reply #9 on: October 28, 2009, 09:58:49 pm »

Ok, if I replace
Code:
!include !Strategy!.MyD!
with
Code:
!include Monthly.MyD!
in the sub-script, everything is working fine.

But I still can't understand why the code is working in the main-script but not in the sub-script.
Logged
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« Reply #10 on: October 29, 2009, 01:16:21 pm »

I slept one night over the problem and now I think I have a solution.

I wrote my algorithm in pseudo-code.

At first I think of building an array with all macros. Like told in the manual, at first we import the macros from the Windows environment and than from the commandline (overwriting macros that are there) and than we add all build-in macros (again overwriting macros, that were defined before). The last macro to import is the !include!-macro.
Code:
function macroReplace (scriptCode, macros)
{
  for each (macros as macro => value)
  {
    if(macro == ...)
    {
      // this should work in most cases
      scriptCode = stringReplace (macro, value, scriptCode)
    }
    elsif(macro == 'include')
    {
      get all includeMacros from scriptCode
      for each (includeMacros as includeMacro)
      {
        get scriptName from includeMacro
        get subScriptCode from scriptName
        // the recursive call ensures, that every sub-script is treated like the main-script
        scriptCode = stringReplace (includeMacro, macroReplace (subScriptCode, macros), scriptCode)
      }
    }
  }
}

// simple call to replace the macros in the scriptcode
scriptCode = macroReplace (scriptCode, macros)

By replacing the include-macro as last macro we ensure that all other macros are replaced before and we can use macros in the script-name to be included.

I hope this helps to improve the scripthandling a little bit.
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7155



View Profile WWW
« Reply #11 on: October 29, 2009, 01:38:51 pm »

By replacing the include-macro as last macro we ensure that all other macros are replaced before and we can use macros in the script-name to be included.
See my original answer to your original question.
Logged
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« Reply #12 on: October 29, 2009, 03:23:52 pm »

Hmm... your answer looks like you didn't take a look at my code. Is it to complicated?

Obviously sub-scripts are treated different than the main-script in your implementation. My solution proposes something to make all scripts equal.
Logged
MacJudge
JkDefrag Supporter
***
Posts: 15


View Profile
« Reply #13 on: October 31, 2009, 05:48:34 pm »

Again a new version and I tested my scripts with it and... tadaa... it works. Smiley

Good job. Thanks for all your work.

Now I can try some more sophisticated scripts to find some other bugs. Wink
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7155



View Profile WWW
« Reply #14 on: October 31, 2009, 07:10:54 pm »

Thanks for the feedback. I'm glad to hear it works for you now.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!