Jump to content

CSV to Variable


Recommended Posts

I have a CSV that looks like:

HKEY_USERS\.DEFAULT

HKEY_USERS\S-1-5-19

HKEY_USERS\S-1-5-20

HKEY_USERS\S-1-5-21-797462679-422022576-1523300623-1000

HKEY_USERS\S-1-5-21-797462679-422022576-1523300623-1000_Classes

HKEY_USERS\S-1-5-18

I need a way to take every line (and it cannot be limited to just the five because there may be more) turn them into a variable that can then be ran through a command like this:

REG SAVE HKEY_USERS\* <save_filename>.hiv

I have been trying this for days and haven't been able to come up with anything that works even remotely.

Can anyone help me here?

Link to comment
Share on other sites

Post some of the code that you have tried and I'm sure we can help you out with it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I should state that the ultimate goal of this code is to copy all available NTUSER.dat files from a running computer, so if anyone has thoughts on a more appropriate way of doing this, feel free to yell at me. :D This was the simplest solution I could come up with (Creating a CSV of User ID's then using that data and plugging it through REG SAVE HKU).

Well the first portion of this creates the CSV

;Run script as an administrator.
#RequireAdmin
;Open Command Prompt
Run("cmd.exe")
;Match Windows by SubString
Opt("WinTitleMatchMode", 2)
;Wait until the program opens.
WinWaitActive("cmd")
;Query for NTUSER SID
Send("REG{space}QUERY{space}HKU{space}>{space}" & @ScriptDir & "\SID.csv{enter}")
;Close CMD
WinClose("cmd.exe")

Basically I am lost on what to do with the CSV. I tried doing a FileRead with StringRegExp. But I couldn't even get a msgbox to display properly.

Then I thought _FileToArray might work with this.

#include <file.au3>
Dim  $aRecords
Dim $bRecords[1][4]
$aRecords = "SID.csv"
If Not _FileReadToArray(@ScriptDir & "\SID.csv", $aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array      error:" & @error)
   Exit
EndIf
ReDim $bRecords[$aRecords[0]+1][4]
For $x = 1 to $aRecords[0]
    $temp1=StringSplit($aRecords[$X],";")
    For $y = 1 to 3
        $bRecords[$x][$y]=$temp1[$y]
    Next
Next
For $i=1 to UBound($bRecords,1) - 1
    For $J=1 to UBound($bRecords,2) - 1
        MsgBox(0,"Test" & String($i),$bRecords[$i][$j])
    next
Next

These attempts were pieced together from other suggestions from other posts and trying to apply them to my need.

But, I don't think my syntax or something is right because I cannot get anything from CSV to output properly. I am new to programming so all the logic behind the script is messing me up. Which is where I am at, trying to get the CSV into an output that can become a variable.

Thoughts?

Edited by SaintedRogue
Link to comment
Share on other sites

First of all there is no reason to be using a CSV file there. A simple text file will do what you want.

Second. There is a much easier way to create thw file than what you did,

Third. What's with the StringSplit() using ";" since there is no ";" in the file?

If you insist that it be sent to a file then this is an easier method.

RunWait(@ComSpec & " /c " & 'Reg query hku > "' & @DesktopDir & '\testFile.txt"', "", @SW_HIDE)

Now to handle the results you can do it with a simple file read using a RegExp.

$aLines = StringRegExp(FileRead(@DesktopDir & "\testFile.txt"), "(?m:^)\h*\S.+(?:\v|$)+", 3)
If NOT @Error Then
For $i = 0 To Ubound($aLines) -1
  $s_Val = $aLines[$i]
  MsgBox(0, "Result " & $i+1, $s_Val)
Next
EndIf

Combine those 2 with simple error checking and you get this.

RunWait(@ComSpec & " /c " & 'Reg query hku > "' & @DesktopDir & '\testFile.txt"', "", @SW_HIDE)
If @Error Then ;; Just adding an error check here
    MsgBox(0, "Error", "Unable to create the file")
    Exit
EndIf
$aLines = StringRegExp(FileRead(@DesktopDir & "\testFile.txt"), "(?m:^)\h*\S.+(?:\v|$)+", 3)
If NOT @Error Then
For $i = 0 To Ubound($aLines) -1
  $s_Val = $aLines[$i]
  MsgBox(0, "Result " & $i+1, $s_Val)
Next
EndIf

EDIT:

I should add that the code I posted isn't what I would use at all.

I would be using Run() instead of RunWait() and reading the StdOut stream into a variable and then processing that with the RegExp() thereby eliminating the need for creating any files at all like below.

Local $s_Out = ""
$h_Proc = Run(@ComSpec & " /c " & "Reg query hku", "", @SW_HIDE, 0x08)
While 1
    $s_Out &= StdoutRead($h_Proc)
    If @Error Then ExitLoop
WEnd
$aLines = StringRegExp($s_Out, "(?m:^)\h*\S.+(?:\v|$)+", 3)
If NOT @Error Then
For $i = 0 To Ubound($aLines) -1
  $s_Val = $aLines[$i]
  MsgBox(0, "Result " & $i+1, $s_Val)
Next
EndIf
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You could always drop the DOS command altogether and and not have to weed out the heading lines, blank lines, and control characters.

#include <Array.au3>
#include <File.au3>
Global $key, $file = FileOpen(@ScriptDir & "\SID.txt", 2), $aLines
While 1
$key += 1
    $var = RegEnumKey("HKEY_USERS", $key)
    If @error Then ExitLoop
FileWriteLine($file, $var)
Wend
FileClose($file)
_FileReadToArray("SID.txt",$aLines)
_ArrayDisplay($aLines)
Link to comment
Share on other sites

RegEnumKey() is actually a good way to go but it doesn't look to me like he needs the file at all. Certainly not the way he was using it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Very well could be that I don't need a file. My mind tends to shoot for the more complicated solutions, I am not a coder by nature so I often miss out on easier solutions. Spiff, your array idea may actually be a good solution. I will do some tinkering and see if I can get that to work. I appreciate the insight.

Link to comment
Share on other sites

With your guys help, I think I have it doing what I want. However I am sending the variable to process like so:

Send(REG SAVE $var <save location>)

It sends what I want to the command prompt however the variable causes the command to hit enter and break what I need entered.

So it actually is sending

REG SAVE $VAR {enter}

<results in broken syntax error>

Rest of my command

And then loops like it should doing the same for each.

Ways to fix this? I did try sending raw as well, which produced the same results

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...