I challange you solving this !

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

I challange you solving this !

#1 Post by Ed Dyreen » 24 Sep 2011 05:33

'
I'm still struggling with this:

Trying to build a variable composed from commands like:

Code: Select all

set "$?=echo.This &echo.works &set /a $error = 0"
I'm building this macro from within a macro, I try to have it executed from the same macro.
Meaning I can't use immediate expansion like:

Code: Select all

%$?%
The only thing I think may be possible is using 'for' to somehow evaluate it.
I tried something like:

Code: Select all

for /f "usebackq" %%! in ( `!$?!` ) do %%!
This is the idea: ( Left out newline etc.. )

Code: Select all

@echo off &SetLocal EnableExtensions EnableDelayedExpansion

set ^"@macro=(
  set "$?=echo.This &echo.works &set /a $error = 0"
  for /f "usebackq" %%^^^! in ( `^^^!$?^^^!` ) do %%^!
)"

%@macro%

pause
EndLocal &exit /b 0
If this can be done then it's possible to exec macros from within macros without increasing their size :?
Last edited by Ed Dyreen on 26 Sep 2011 04:16, edited 2 times in total.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: I challange you solving this.

#2 Post by Ed Dyreen » 26 Sep 2011 04:00

'
Is it possible, and why is it so important ?

DOS interprets a single line or somehow grouped commands.
It then executes them ( from memory ) and continues the next line.
It will do so until the batch file ends.

Macros are fast because they don't require DOS to read from file as they are substituted from memory ( only one read ).
In other words the more things can be done in memory, the fewer times the file has to be accessed,
the faster the program will run.
The technique ( if possible ) should allow macro's to be build up in memory.
Many file accesses could then be eliminated, making the code much more faster.

:roll: Please help me help u, this will benefit us all :roll:

Voodooman
Posts: 12
Joined: 25 Sep 2011 07:45

Re: I challange you solving this !

#3 Post by Voodooman » 26 Sep 2011 08:28

Someone played Portal to much :lol: "thank you for helping us help you help us all".
But is think that "cake is lie".


Im not quite sure what you mean by macro, but its pretty much easy to set any line of command or anything as variable and then read them from memory!
However im not sure this will give you any advantage in speed.
Line by line reading of file is not that slow, interpreter is slow itself and "macros" like this could make it even slower.

To set multiline value you need to use ^ symbol in the end of every line except last, this symbol in the end of line makes next line considered as continuation of line, in addition you can use & symbol before or after ^ to separate commands, they will act exactly as when they executed line by line.

But you should mess a lot with escape symbols, avoid quotes in quotes or use a lot of tricks to escape them, this could be quite tricky and messy!

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: I challange you solving this !

#4 Post by Ed Dyreen » 26 Sep 2011 21:11

'
Cold coffee, I'm not a newbie :wink:

Code: Select all

::--------------------------------------------------------------------------------------------------------------------------
set "$Defines=FileOpen" &set "$Details=Open a file [in registry]"
::
set ^"$Usage.@%$Defines%=^
 Usage: %%@forL%% ( 'r#File' ) %%@%$Defines%%% %$n1c%
  r#File      : File to open %$n1c%
 ReturnConsole: Contents of $File %$n1c%
  $error      : unaffected by function, panic otherwise "
::
2>%@Dcon% ( %@forA% ( '"@%$Defines%"¦"EnableDelayedExpansion"' ) %@Macro.Begin% )
::(
%   % set ^"@%$Defines%=!@Udelim! ( %$n1c%

%      % SetLocal EnableExtensions EnableDelayedExpansion %$n1c%
%      % set "$File=%%~a" %$n1c%
%      % !@n2echo! @FileOpen: '^^^!$File^^^!' %$n1c%

%      % set %E5%"@rule=%E5%>nul %E5%( ^^^!$n0^^^!^^^^%$n1c%
%$c2%
%      %   set /a $? = 1 ^^^!$n0^^^!^^^^%$n1c%
%      %   2%E5%>nul reg delete "^^^!$ED.Path^^^!" /v FileOpen /f %E5%&%E5%&%E5%( ^^^!$n0^^^!^^^^%$n2c%
%$c2%
%      %     2%E5%>nul reg query "^^^!$ED.Path^^^!" /v "FileOpen.^!$File^!" %E5%|%E5%|%E5%( ^^^!$n0^^^!^^^^%$n2c%
%$c2%
%      %       set /a $? = 0 ^^^!$n0^^^!^^^^%$n1c%
%      %       %E7%!@forU%E7%! %E5%( '"^^^!$ED.Path^^^!", "FileOpen.^!$File^!", "^!$Trace^!", REG_SZ' %E5%) %E7%!@RegWriteSSS%E7%! ^^^!$n0^^^!^^^^%$n1c%
%$c2%
%      %     %E5%) ^^^!$n0^^^!^^^^%$n1c%
%      %     %E7%!@forU%E7%! %E5%( '"^^^!$ED.Path^^^!", FileOpen, Ready, REG_SZ' %E5%) %E7%!@RegWriteSSS%E7%!  ^^^!$n0^^^!^^^^%$n1c%
%$c2%
%      %   %E5%) ^^^!$n0^^^!^^^^%$n1c%
%$c2%
%      % %E5%) %E5%&if %E11%!$?%E11%! neq 0 ^" %$n1c%
%      % !@forL! ( '' ) !@While! ^>nul %$n1c%

%      % !@nError! %$n1c%
%      % EndLocal %$n1c%
%   % )"
::)
2>%@Dcon% %@Macro.End%
::
%@COMMIT%
::
%@forU% ( '"aFile"' ) %@FileOpen%
::--------------------------------------------------------------------------------------------------------------------------
::
goto :skip "()"
%@endoftest%
:skip ()

My/our contribution, a function definitions list :arrow:
viewtopic.php?f=3&t=1893

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: I challange you solving this !

#5 Post by dbenham » 26 Sep 2011 23:11

I've already used the basic concept in my macro.RtnN macro to pass multiple values across the ENDLOCAL barrier. But it relies on a newline character separating each command. The FOR /F parses and executes each line separately.

I don't think there is a general solution Ed. It only works with a limited subset of batch file constructs because much of the interesting batch interpretation/parsing takes place before the FOR variable expansion. Anything that depends on this early parsing will fail.

I found a useful archive of one of jeb's posts here: The Secrets of Batch File Interpretation It helps to predict what might succeed and what might fail.

A few things I'm pretty sure will fail based on jeb's post:
- Multiple commands separated by & (I've confirmed that this fails)
- Conditional success and error processing using && and ||
- Redirection using >, >>, or <
- Piped output using |
- Any type of block processing using ()

I think there may also be problems with IF and FOR statements, but I'm not sure.

Dave Benham

Post Reply