question

Raam4-2038 avatar image
0 Votes"
Raam4-2038 asked RichMatheisen-8856 edited

How do I reverse the order of the lines in multiple files of a folder with a PowerShell script?

This script, saved to a .ps1 file which I run as an admin is not working:-
$x = Get-Content -Path C:\Users\SKY\Desktop\curede*.
[array]::Reverse($x)
$x | Out-File -FilePath C:\Users\SKY\Desktop\curede*.

Please tell me how to reverse the order of the lines in multiple files of a folder with a PowerShell script.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered Raam4-2038 edited

How about this?

 Get-ChildItem c:\junk\123*.txt |
     ForEach-Object{
         $x = Get-Content $_
         [Array]::Reverse($x) 
         $x |
             Out-File $_
     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Terrific @RichMatheisen-8856 , it worked perfectly. I changed the path to C:\Users\SKY\Desktop\curede\ which was obvious.
However, I got this message on the screen in the PowerShell window:-
Cannot find an overload for "Reverse" and the argument count: "1".
At C:\Users\SKY\Desktop\curede\ ReversingLines.ps1:4 char:19
+ [Array]::Reverse <<<< ($x)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Is it a problem? Has everything been done correctly?

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

Are there sub-directories beneath "C:\Users\SKY\Desktop\curede\"? The Get-ChildItem, as written by you, will return not only files but also directories. If you only want files, add the "-File" parameter.

Note that the code I posted had no need for the "-File" parameter, nor does it include any error checking (e.g., what if $x contains a $null value?).

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RichMatheisen-8856 So the "+ CategoryInfo : NotSpecified: (:) [], " was displayed because of a null character and I needn't worry about it right? There were no sub-directories so I figure that that shouldn't be a problem. Should I click on the "Accept answer" with the leading check mark (for what you posted first)?

0 Votes 0 ·

Without you posting the code that you ran that produced the error I'm not doing any more than suggesting possible causes of the error.

Do you know what the "$_" variable contained when the failure occurred? Do you know what was in the "$x" variable at that time? Was the value in $x a single string or was it an array or was it $null?

At a minimum, you should change "$x = Get-Content $" to "[array]$x = Get-Content $" to ensure that you're asking the static "Reverse" method to operate on an array, even if the file contains only a single line. (FYI, both of the "$<underbar>" in this paragraph have been changed by the editor to just "$" -- that's why we ask that code be posted as a "Code Sample" and not text.)

Again, the example code was only an example. It works on the data I used, but I have no idea what your data set is.


0 Votes 0 ·