0

I am attempting to read one file, store it in a variable and replace text in file 2 with the data from file 1. However, my output does not seem accurate. Here is my source:

$orders = Get-Content -path F:\test2.txt -Raw
$files = Get-Content -path F:\unt.txt -Raw 
 foreach($file in $orders)
{
 $files.replace("var",$_) |  Set-Content -path .\test3.txt
}

When I use "$_", the 'var' is replaced, but there seems to be no data. I've also used $orders, but I can't seem to increment the data on the string. The file I'm attempting to edit is as shows My unt.txt file is as follows and all instances of 'var' would need to be replaced but iteratively from the array, so the first var would be replaced by $orders[0], the second by $orders[1], but I am having difficulty.:

autECLSession.autECLPS.SendKeys "var"

autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"

autECLSession.autECLPS.SendKeys "var"

However, when the file runs it shows as:

autECLSession.autECLPS.SendKeys ""

autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"

autECLSession.autECLPS.SendKeys ""

Furthermore, when I try to use the variable, $orders, I can't seem to figure out how to track the data being sent, an example of the output looks like it is just pasting the entire variable. I attempted to use Split, but I run into the same problem:

autECLSession.autECLPS.SendKeys'H00697581'
H00699775
M00690370
A00694622
D00690481
U00695159
H00699618
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"
autECLSession.autECLPS.SendKeys'H00697581'
H00699775
M00690370
A00694622
D00690481
U00695159
H00699618

Apologies for not including the text, but here are my files. I am attempting to take the input from test2 and replacing all instances of 'var' in my unt.txt, but currently it is repeating the first spot in the array, H00697581. My test2 file:

H00697581
H00699775
M00690370
A00694622
D00690481
U00695159
H00699618

The test.3.txt is just the output, but I expect it to look like so:

autECLSession.autECLPS.SendKeys'H00697581'
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"
autECLSession.autECLPS.SendKeys'H00699775'
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"
autECLSession.autECLPS.SendKeys'M00690370'

etc...

2
  • 1
    Could you post an example of “test2.txt”, “unt.txt”, and what you expect “text3.txt” to look like. There’s a few issues in your code - e.g. using the -raw switch on get-content when probably meaning to read file lines as an array, trying to use the automatic variable “$_” (which only works in pipelines) as the foreach iterator variable, and overwriting the same file (text3.txt) on every iteration, but I don’t really get what you’re actually trying to do, so a concrete example of input files and expected output would help...
    – mclayton
    Commented Mar 11, 2020 at 22:35
  • Thank you, I have updated the original. @mclayton
    – JustinB123
    Commented Mar 11, 2020 at 23:23

2 Answers 2

1

I can't realy get, what you're trying to replace with what. Can you give an examples of your two Input-Files and expected Output-File?

Next: Part of your Issue might be the "-Raw":

$orders = Get-Content -path F:\test2.txt -Raw
foreach($file in $orders) { ... }

When using -raw, $orders will contain one (1) string. So, your "foreach"-Loop will exactly run once, with the whole contents of test2.txt in $file. Without -raw, $orders will contain an Array of strings. So, your "foreach"-Loop will run one time per line in test2.txt (and always have this line in $file)

Last but not least: The naming is misleading. You have a variable $files and one which is named $file, and you have a foreach-loop. Usually, you'd expect to be $file one of the $files in a loop => I'd recommend you to change names to "foreach ($order in $orders)", than it gets clearer what is meant.

Reagards

Filipp

1
  • 1
    Also, trying to use “$_” as the iterator variable inside the foreach :-)
    – mclayton
    Commented Mar 11, 2020 at 22:42
0

I'm making a bit of a leap here, but I think the example below will do roughly what you want in a console:

$commands = @(
    "autECLSession.autECLPS.SendKeys `"{0}`"",
    "autECLSession.autECLOIA.WaitForInputReady",
    "autECLSession.autECLPS.SendKeys `"[enter]`"",
    ""
);

$files = @(
    "H00697581",
    "H00699775",
    "M00690370"
);

$script = @();
foreach( $file in $files )
{
    foreach( $command in $commands )
    {
        $script += $command -f $file;
    }
}

write-host ($script | fl * | out-string);

which outputs:

autECLSession.autECLPS.SendKeys "H00697581"
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"

autECLSession.autECLPS.SendKeys "H00699775"
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"

autECLSession.autECLPS.SendKeys "M00690370"
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"

To turn that back into something that does file-based IO, you can do this instead:

$commands = Get-Content -Path "commands.txt";
$files    = Get-Content -Path "files.txt";

$script = @();
foreach( $file in $files )
{
    foreach( $command in $commands )
    {
        $script += $command -f $file;
    }
}

Set-Content -Path "script.txt" -Value $script;

where "commands.txt" is:

autECLSession.autECLPS.SendKeys "{0}"
autECLSession.autECLOIA.WaitForInputReady
autECLSession.autECLPS.SendKeys "[enter]"

Not the answer you're looking for? Browse other questions tagged or ask your own question.