user3642282
user3642282

Reputation: 21

How to loop through files in a folder pass a value and insert text into a file

Hello I have this code I've been working on to loop through a folder add file name to command line parameter of an executeable then output the results to a text file.. The code works for one interation but does not seem to loop through all the files and append to the text file. Can you take a look at my structure and see why it is not looping through all the files and appending. Regards.

$Path = "C:\rawfiles"
$files = Get-ChildItem C:\rawfiles\*.001
ForEach ($file in $files) { 
    c:\outputfiles\ldump.exe $file.fullName > c:\outputfiles\test9.txt -Append
    "=======End of Batch========" | Out-File c:\outputfiles\test9.txt -Append
}

Upvotes: 2

Views: 27734

Answers (1)

Keith Hill
Keith Hill

Reputation: 201622

You can't mix > with -Append. Try this instead:

$Path = "C:\rawfiles"
$files = Get-ChildItem C:\rawfiles\*.001
ForEach ($file in $files) { 
    c:\outputfiles\ldump.exe $file.fullName | Out-File c:\outputfiles\test9.txt -Append
    "=======End of Batch========" | Out-File c:\outputfiles\test9.txt -Append
}

Or:

$Path = "C:\rawfiles"
$files = Get-ChildItem C:\rawfiles\*.001
ForEach ($file in $files) { 
    c:\outputfiles\ldump.exe $file.fullName >> c:\outputfiles\test9.txt
    "=======End of Batch========" >> c:\outputfiles\test9.txt
}

You may want to add a line at the very beginning to delete or empty test9.txt.

Upvotes: 8

Related Questions