1

I have been looking for help on this issue for a few hours with no luck. My script monitors a folder for files older than 12 minutes ending with '.tmp'. It then removes '.tmp'. This portion is working correctly.

I would like the script to output a text file containing the file name of the files that were renamed. With the script below, the text documents are empty.

#Computer name from Description
$ComputerName = Get-WmiObject -Class Win32_OperatingSystem | Select Description
$ComputerName = "$ComputerName".split("}")
$ComputerName = "$ComputerName".split("=") | Select-Object -Last 1
$ComputerName = "$ComputerName".split(" ") | Select-Object -First 1

#$ComputerName = $env:computername
Write-Host "This is the computer name: $ComputerName"

$c1Output = 'E:\' + $ComputerName + '\Capture One Session\Output\Market\'

get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) }
    out-file \\server\Report.txt

This should be easy to figure out, however I am still new to PowerShell.

Thanks!

Garrett
  • 617
  • 12
  • 30

1 Answers1

5

Your problem is here:

get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) }
    out-file \\server\Report.txt

You need to make two changes: add the -Passthru switch to your call to Rename-Item, and pipe the result to your Out-File:

get-childitem -Path "$c1Output" -Filter *.tmp |
    where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} | 
    rename-item -newname { $_.name.substring(0,$_.name.length-4) } -Passthru |
    out-file \\server\Report.txt
Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Thanks @Jeff Zeitlin! – Garrett Aug 08 '17 at 20:34
  • Hey @Jeff Zeitlin , When using this code as you recommended, the reports are constantly outputting. I wanted to only get the text file when a file's name was changed. Any advice on that? – Garrett Aug 15 '17 at 14:54
  • The code will _always_ change the filename for any file that meets the `Where-Object` condition. Files that do not meet that clause will not pass through that filter, and will therefore not get renamed, and will therefore not show up in the log. You need to make sure that your `Where-Object` is only passing the files you actually want; all I did was correct the error that left you with an empty log. – Jeff Zeitlin Aug 16 '17 at 11:55
  • Thanks again @Jeff Zeitlin. We remedied the issue with the following: `$childItem = get-childitem -Path "$c1Output" | where-object {$_.LastWriteTime -lt (get-date).AddMinutes(-12)} $childItem = $childItem.Name foreach ($child in $childItem) {if ($child -like "*.tmp"){ rename-item -newname { $_.name.substring(0,$_.name.length-4) } -PassThru | out-file \\ant\fc\Dept\Photo\SDF8\Studio\Tech\scripts\tmp_fix\Reports\$computerName'___'$time.txt} }` – Garrett Aug 16 '17 at 12:15