Powershell: Save multiple files with the same name after modify

Nicu F 61 Reputation points
2022-11-04T09:43:00.79+00:00

hello. I want to understand how to do this. I want to modify each .txt files from a folder, but keeping the original name on save.

I believe I made something wrong. Can you help me, please?

((Get-Content -Path 'C:\Folder1' -Filter '*.txt' -Raw -Encoding UTF8) -split "\s+" |   
    Sort-Object {Get-Random} ) -join ' ' |  
        Out-File -FilePath "$newfilename" -force  

I get this error:

Get-Content : Could not find a part of the path 'C:\Folder1\'.  
At line:1 char:3  
+ ((Get-Content -Path 'C:\Folder1\' -Filter '*.txt' -Raw -Encodi ...  
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : ObjectNotFound: (C:\Folder1\:String) [Get-Content], DirectoryNotFoundException  
    + FullyQualifiedErrorId : GetContentReaderDirectoryNotFoundError,Microsoft.PowerShell.Commands.GetContentCommand  
  
Out-File : Cannot bind argument to parameter 'FilePath' because it is an empty string.  
At line:3 char:28  
+         Out-File -FilePath "$newfilename" -force  
+                            ~~~~~~~~~~~~~~  
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException  
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.OutF  
   ileCommand  
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-11-08T16:29:59.837+00:00

    This may be what you're looking for:

    $files = Get-Item -Path 'C:\Folder1\*' -Filter '*.txt'  
    ForEach ($file in $files){  
        ((Get-Content -Path $file.FullName -Raw -Encoding UTF8) -split "\s+" |   
            Sort-Object {Get-Random} ) -join ' ' |  
                Out-File -FilePath $file.FullName -force  
    }  
    

3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-11-04T10:09:38.857+00:00

    Hi @Nicu F ,

    you need to get the file object (filename) first before you can use the Get-Content cmd.

    Maybe this helps to get started:

    Get-ChildItem C:\Folder1 -Filter *.txt -File | Get-Content  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Nicu F 61 Reputation points
    2022-11-04T13:46:10.11+00:00
    Get-ChildItem C:\Folder1 -Filter *.txt -File | Get-Content  
    

    thanks. This is the first part. As to read de files, isnt't it?

    But the second part is the problem... how can I save in the same file?

    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2022-11-04T15:41:41.383+00:00

    If you use the -Filter parameter you can't use an unqualified directory name. See Example 7 of the Get-Content help. E.g., add "*" to the end of the path.

    You can't save the file to the same name while the file is in use. In your example, you've placed the Get-Content inside parentheses so the file should be okay to overwrite because the reading of the file should be completed before you overwrite it. BUT, how do you know what file name to use? The Get-Content produces only a string object. There's no information about the file.

    You need to use a Get-Item to select the files to modify and use the FullName property in a ForeEach loop. Save the file name for reuse in the Out-File cmdlet.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.