Executing GOTO/CALL in a cmd.exe < NotBatch.txt file!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
lmstearn
Posts: 50
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Executing GOTO/CALL in a cmd.exe < NotBatch.txt file!

#31 Post by lmstearn » 07 Nov 2015 04:49

Thanks for the explanation, PenPen, but it didn't do it for me. Here's the revised code:

Code: Select all

@ECHO OFF
CD\
PUSHD C:\Users\New\Desktop
SET KEY_NAME="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
SET "VAL_NAME=Userinit"
CALL SET "CURREGVAL=%SystemRoot%\System32\userinit.exe,"
CALL SET "TMPSTR=%SystemRoot%\Temp\CreateLargeDir.exe, %SystemRoot%\system32\userinit.exe,"
CALL SET "NEWREGVAL=%CURREGVAL:%SystemRoot%\system32\userinit.exe,=%TMPSTR%%"
CALL ECHO %KEY_NAME% %VAL_NAME% %CURREGVAL% %NEWREGVAL% >CON
CALL REG ADD %KEY_NAME% /v %VAL_NAME% /d %NEWREGVAL%
POPD
Exit /b

This is the command used in an elevated cmd window:

Code: Select all

<"C:\Users\New\Desktop\NotBatch.txt" >"C:\Users\New\Desktop\output.txt" cmd.exe

and it returned ERROR: Invalid syntax Type REG ADD /? for usage.
Presumably from the the linked post, the NotBatch parsing is more aligned with the Command line parser than the Batch line parser. And Jebs comment regarding FOR F
DelayedExpansion is active only if it is enabled with the registry key"
is interesting: would that play a role here? David's remark on variable expansion
If next character is % then
Replace %VAR% with value of VAR (replace with nothing if VAR not defined) and continue scan

The value of VAR is better expressed as contents of VAR. VAR is the pointer and %VAR% is the pointee. A possible problem here is the pointee also contains a macro %systemroot% and although this is parsed for console output, it's not passed through to REG correctly.
Another point of interest is this (escaped) code produces no REG related errors in VS:system command. Buuut the value data copied to registry is "C:\Windows\system32\,TMPSTR%%" Almost there. :P
Having googled extensively on this topic, I have not come across one example of posted code where a variable has been used for REG ADD. Looks like it has been broken (at least for NotBatch) for years. :cry:
Last edited by lmstearn on 09 Nov 2015 03:30, edited 4 times in total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Executing GOTO/CALL in a cmd.exe < NotBatch.txt file!

#32 Post by penpen » 07 Nov 2015 10:06

lmstearn wrote:Presumably from the the linked post, the NotBatch parsing is more aligned with the command line parser than the Batch line parser. And Jebs comment regarding FOR F
DelayedExpansion is active only if it is enabled with the registry key"
is interesting: would that play a role here?
No, the cause is simple:
In your oneliner there is no "for /F", and no delayed expansion.


lmstearn wrote:Another point of interest is this (escaped) code produces no REG related errors in VS:System command. But the value data copied to registry is "C:\Windows\system32\,TMPSTR%%" Almost there. :P
My fault:
I've forgotten, that you produce a oneliner.
The TMPSTR variable is undefiined, when the oneliner is built (and escaped expanded).
Therefore %tmpStr% is not replaced by its value and the CLI (command line interpreter) "sees" this string (after the first expansion of the oneliner; == before any call command is executed):

Code: Select all

call SET "NEWREGVALUE=%CURREGVALUE:[SystemRoot]\system32\userinit.exe=%tmpStr%%"
Easy to see now, that "%CURREGVALUE:[SystemRoot]\system32\userinit.exe=%" is replaced by its value ("") and "tmpstr%%" is appended... .
Note: If you use multiple lines (in NoBatch.txt), then this should work, because the variable is defined in the next line, because in that case the CLI executes (and expands) one line after another (not all at once).

So we must workaround this:

Code: Select all

SET "NEWREGVALUE=%CURREGVALUE:%SystemRoot%\system32\userinit.exe=%tmpStr%%"
We do this:
Just use a variable P containing a percentage char so "NEWREGVALUE" is interpreted as a string, and SystemRoot and TMPSTR are expanded correctly => a second call is needed to expand this string.

Result:

Code: Select all

set "P=%"
call call SET "NEWREGVAL=%P%CURREGVAL:%SystemRoot%\system32\userinit.exe,=%TMPSTR%%P%"
Hopefully that does it.

Sidenotes:
1) You probably need another comma character (',') at the end of TMPSTR (because you replace the last in the old regvalue).
2) If i see it right you missed the doublewuotes around %NEWREGVAL% in the "reg add" command;
there are also no doublequotes around %VALUE_NAME%, but that's not a problem, because there are no (command line) delimeters in this string (for example a space character: ' ').


penpen

lmstearn
Posts: 50
Joined: 07 Dec 2014 15:15
Location: Australia
Contact:

Re: Executing GOTO/CALL in a cmd.exe < NotBatch.txt file!

#33 Post by lmstearn » 08 Nov 2015 07:28

That was a good idea, but it didn't parse correctly. However I think we have found the bug!
It's not the percentage expansion or any such. It's the space:

Code: Select all

CALL CALL SET \"NEWREGVALUE=%CURREGVALUE:%SystemRoot%\\system32\\userinit.exe=%SystemRoot%\\Temp\\CreateLargeDir.exe,%SystemRoot%\\system32\\userinit.exe%\"

Works beautifully. However, put a little space after the comma in the replacement string and REG spits the dummy:

Code: Select all

CALL CALL SET \"NEWREGVALUE=%CURREGVALUE:%SystemRoot%\\system32\\userinit.exe=%SystemRoot%\\Temp\\CreateLargeDir.exe, %SystemRoot%\\system32\\userinit.exe%\"

Edit: Having a hard time getting REG to work at all in NotBatch:

Code: Select all

@ECHO OFF
SET KEY_NAME="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
SET "VAL_NAME=CachedLogonsCount"
SET "CURREGVAL=10"
ECHO %KEY_NAME% %VAL_NAME% %CURREGVAL% >CON
REG ADD %KEY_NAME% /v "%VAL_NAME%" /d "%CURREGVAL%"
Exit /b

Hangs REG. However pasting it in to an (Admin) cmd window gets the overwrite prompt. Responding "N" hangs the console and CTRL-C does not break.
The output produced by the crazy loop is a 17+mb file with stuff like:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\>@ECHO OFF
SET KEY_NAME="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
SET "VAL_NAME=CachedLogonsCount"
SET "CURREGVAL=10"
ECHO %KEY_NAME% %VAL_NAME% %CURREGVAL% >CON
REG ADD %KEY_NAME% /v "%VAL_NAME%" /d "%CURREGVAL%"
Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value CachedLogonsCount exists, overwrite(Yes/No)? Value Cached

Edit2: Adopted one of PenPen's tricks to overcome the space bug in system:

Code: Select all

SET \"P= \"  & CALL CALL SET \"NEWREGVALUE=%CURREGVALUE:%SystemRoot%\\system32\\userinit.exe=%SystemRoot%\\Temp\\CreateLargeDir.exe%P%%SystemRoot%\\system32\\userinit.exe%\"

Successfull!

Post Reply