Ochuse
Ochuse

Reputation: 261

Capture File Paths with Out-File in Powershell

I've created a script that I'm using to clean up some drives at my work. I've been asked to create a log and leave it in the source folder after I move files.

Currently, the process is slow because my script creates a text file of files that meet the parameters I input. Once I have verified it, or edited the text file, I allow the script to read the file and keep doing what it needs to do. I'm creating this original text file with Out-File. Obviously, the path of the files change because I'm moving them from one drive to another. I'd like to log their new path but can't seem to figure out how to do this.

The file the script creates and reads from looks like the following:

C:\This\Is\The\Source\Something.rpt
C:\This\Is\The\Source\Somethingelse.bak
C:\This\Is\The\Source\AnotherFile.jpg

I'm looking to create something that will reflect the new path once the files are moved. In the different ways I've tried I either end up with nothing or just the last file copied, which would tell me Out-File is not appending but overwriting each time it gets a new file path.

And the list will just go on. The following is bit from my script I'm having issue with:

$path = Read-Host "What path should I look at?"

$SourceFolder = $path

$files = Get-ChildItem $path  -Recurse
| Where-Object {$_.lastwritetime.Date -eq $targetdate}
|Where-Object {$_.PSIsContainer -eq $false} 
| ForEach-Object {$_.fullname}
| Out-File $OutFileCopy

$Target =  Read-Host "What is the destination?" 

Write-Host "Please view the text file created." -foregroundcolor "yellow" -backgroundcolor "red"
Invoke-Item $OutFileCopy

$CopyContinueAnswer = Read-Host "Would you like to continue? Y or N?"

If ($CopyContinueAnswer -eq 'Y') {

    $Readfile = Get-Content $outfilecopy

 $ReadFile | Where-Object {$_.PSIsContainer -eq $false}
    foreach ($file in $ReadFile)  {        

    $logfile = "$Sourcefolder\log.txt"
    out-file $logfile

        Write-Host  "The old path is $File"

        $TargetPath = $File.Replace($SourceFolder, $Target) 

        if (!(Test-Path $TargetPath)){

            Write-Host  "This is the new path $TargetPath" -foregroundcolor "Magenta"
            Write-Host               

            Copy-Item  -path $file -Destination $TargetPath                 

            Write-output $TargetPath | out-file $logfile                 

        }

Upvotes: 1

Views: 623

Answers (2)

Ochuse
Ochuse

Reputation: 261

The file that I want to record the new files paths is being created inside the foreach loop. Everytime it stepped to the next file it would recreate the log.txt file and erase what was there previously. Now that I've taken it out of the loop, I don't have the issue of nothing being recorded or only the last file that went through the loop being recorded.

I'll create a portion of the script that looks to see if the log.txt already exists before it tries to create one.

Upvotes: 0

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10809

Out-File by default will overwrite an existing file. If you do not want this to happen, use Out-File -append. I recommend looking at the Microsoft documentation for Out-File; you can find it by typing Get-Help Out-File at any PowerShell prompt, or clicking on the link.

Upvotes: 2

Related Questions