Convert BAT syntax into WBT

Started by das479, June 22, 2020, 07:42:22 AM

Previous topic - Next topic

das479

I'm exporting a registry key to a REG file, changing a part of it and importing it back. 

This works in DOS:  REG EXPORT "HKU\S-1-5-21-3879748759-1532710683-557706590-1001\SOFTWARE\Foxit Software\Foxit PhantomPDF 9.0\Preferences\Security\TrustedFolders" c:\users\dennis_sauer\documents\regfile.reg /y

I can't get the equivalent into Winbatch.  I found code that someone suggested and copied it with some tweaks, but it doesn't work yet.  I think it is a syntax error, but I'm not sure.  The RUNWAIT line doesn't produce the REG file.  I verified the FULL09Path and TempRegFile are set correctly to the paths you see above.  Since the SID after the HKU changes with each user I can't hard-code it.  But I have extracted the SID into the Full09Path. 

The replace key (which I haven't gotten to yet) is going to be the Full10Path.

I tried REGEDIT and REG EXPORT ... it won't accept the word "export" with REG because REG is the .exe. 

Can anyone suggest what the proper syntax would be to export the registry key?

Thank you!

-Dennis


;The /E:A parameter below tells RegEdit to export the file as an ASCII file that
;WinBatch can handle.
RunWait("REG",'/Export HKU\%Full09Path% %TempRegFile%') ;Exports the registry key
GoSub SEARCHREPLACE ;Replace Search with Replace, effectively renaming the key reference in the reg file
RunWait("REGEDIT.EXE",'/s %TempRegFile%') ;Import modified .REG file containing the renamed items
FileDelete(TempRegFile) ;Cleanup
Exit
 
:SEARCHREPLACE
  fs = FileSize( TempRegFile )
  binbuf = BinaryAlloc( fs*2 )
  Sourc = BinaryRead( binbuf, TempRegFile )
  num = BinaryReplace( binbuf, Search,Replace ,CaseSensitive)
  BinaryWrite( binbuf, TempRegFile)
  BinaryFree(binbuf)
  Return

ChuckC

Take a look at the Consolidated WIL Help and search for the help topics for the Reg*() functions.

WinBatch can directly access the registry, so it would be significantly more expedient to deprecate the usage of old .BAT file techniques and simply read the desired value directly, make the substitution in the string and then write the value directly to the registry using nothing but WIL statements in your script.

das479

I've been all over the REG() commands and there doesn't seem to be one to copy a Registry key or Export a Registry Key ... at least I haven't found it.   I'm not a programmer by profession so I'm not sure of ways around it.  The "Run" or RunWait" commands seem like they should work.  I'll keep plugging along.

ChuckC

You're missing the point and needlessly complicating things for yourself.  The code snippet that you are asking for assistance with is doing things that are notorious when it comes to creating reliability problems in a script.  You are combining legacy-style "percent" substitution of a WIL variable in a string, which is always a *BAD* idea.  Then you're shelling out to an external console mode utility telling it to dump a registry key to a file, which your script then has to read in and possibly deal with non-existence of the file if the external program failed for some reason.  Then there are binary buffer manipulations followed by writing the buffer back out to a file followed by running an external utility to "import" that modified content back into the registry.

Now, why on earth would you want to jump thru all of those hoops and all of the "glue" tying those various steps together in a rather tenuous manner when the Reg*() functions give you everything that you need to perform those tasks using nothing but WIL code and no external utilities?

Start with the following:

regvalue = RegQueryStr(@REGUSERS, "S-1-5-21-3879748759-1532710683-557706590-1001\SOFTWARE\Foxit Software\Foxit PhantomPDF 9.0\Preferences\Security\TrustedFolders", 0)

Use WinBatch Studio so that you can interactively examine variable values in the debugger.

Note the value contained in the variable "regvalue".

You can directly use other functions to manipulate that string value.  If you know the exact case of the string, then the StrReplace() function is your friend.  Otherwise, you can use a binary buffer and the corresponding case-insensitive string replacement, and the Binary[Peek|Poke]Str[W]() functions are your friends when it comes to getting a string into or out of a binary buffer.

Finally, RegSetValue() is what you would use to write the modified value back to the registry.

It's not rocket science and you don't have a be an experienced software engineer.  Just follow the bread crumbs laid out above, read the help topics, look at the usage examples in each help topic and have fun playing around with each function in WinBatch Studio until you are comfy with what each one is doing and then string them together.  You're looking at a script that's about 10 to 15 lines in length, maximum, to perform the desired task, and with no dependencies on anything outside of that bit of script code to do the job.

das479

Thank you!   I'll work with that. 

jmburton2001

Quote from: das479 on June 22, 2020, 10:17:26 AMI'm not a programmer by profession...

I'm not a programmer either, not by a long shot.

What you're trying to do is the equivalent of creating a "Rube Goldberg machine". You're trying to make your registry changes the most convoluted way possible and your method is rife with potential failure points. With Winbatch you can directly manipulate the registry and bypass all the frustration you're experiencing.

Look in the WIL Consolidated help file for "Registration Functions" and also look at the "Registry Searcher Extender (aka Reggie)". The tech database is filled with useful code snippets.

Using these tools you can directly query the registry for keys, create keys, extract keys, manipulate values and insert them into keys. The uses are endless.

ChuckC's a rock star and KNOWS what he's talking about.