0

The purpose of this code is to transfer files from one location to another and to log whether the transfer was a success or a failure.

Everything works except I am having issues with the log. I want the log to be in CSV format and there to be 3 columns: success/failure, from location, and to location. This is outputting the results all into rows with one column.

I've tried the Export-Csv option but that looks for objects/properties so only displays the length(I have strings too). Add-content works but there is only one column. Any suggestions?

#LOCATION OF CSV
$csv = Import-Csv C:\test2.csv
#SPECIFY DATE     (EXAMPLE-DELETE FILES > 7 YEARS.  7 YEARS=2555 DAYS SO YOU WOULD ENTER "-2555" BELOW)
$Daysback = "-1"
#FILE DESTINATION
$storagedestination = "C:\Users\mark\Documents\Test2"
#LOG LOCATION
$loglocation = "C:\Users\mark\Documents\filetransferlog.csv"
$s = "SUCCESS"
$f = "FAIL"


$CurrentDate = Get-Date
foreach ($line in $csv) {
    $Path = $line | Select-Object -ExpandProperty FullName
    $DatetoDelete = $CurrentDate.AddDays($DaysBack)

    $objects = Get-ChildItem $Path -Recurse | Select-Object FullName, CreationTime, LastWriteTime, LastAccessTime | Where-Object { $_.LastWriteTime -lt $DatetoDelete } 

    foreach ($object in $objects) {
        try 
        {
            $sourceRoot = $object | Select-Object -ExpandProperty FullName
            Copy-Item -Path $sourceRoot -Recurse -Destination $storagedestination 
            Remove-Item -Path $sourceRoot -Force -Recurse
            $temp = $s, $sourceRoot, $storagedestination
            $temp | add-content $loglocation
        }
        catch
        {
            $temp2 = $f, $sourceRoot, $storagedestination
            $temp2 | add-content $loglocation
        }
    }
}

1 Answer 1

2

All your | Select-Object -ExpandProperty are superfluous, simply attach the property name to the variable name => $Path = $line.FullName
Why calculate $DatetoDelete inside the foreach every time?
Output the success/fail to a [PSCustomObject] and gather them in a variable assigned directly to the foreach.

Untested:

$csv = Import-Csv C:\test2.csv  
$Daysback = "-1"                
$destination = "C:\Users\mark\Documents\Test2"
$loglocation = "C:\Users\mark\Documents\filetransferlog.csv"
$s = "SUCCESS"
$f = "FAIL"

$CurrentDate  = Get-Date
$DatetoDelete = $CurrentDate.Date.AddDays($DaysBack)

$Log = foreach ($line in $csv) {
    $objects = Get-ChildItem $line.FullName -Rec | 
        Where-Object LastWriteTime -lt $DatetoDelete
    foreach ($object in $objects) {
        $Result = $s
        $sourceRoot = $object.FullName
        try {
            Copy-Item   -Path $sourceRoot -Recurse -Destination $destination 
            Remove-Item -Path $sourceRoot -Recurse -Force
        } catch {
            $Result = $f
        }
        [PSCustomObject]@{
            'Success/Fail' = $Result
            Source         = $sourceRoot
            Destination    = $destination
        }
    }
}
$Log | Export-Csv $loglocation -NoTypeInformation
Sign up to request clarification or add additional context in comments.

1 Comment

I see what you did. This worked perfectly! The custom object is exactly what I was missing and you're right about the efficiency of the code. P.S. I did see your edit for the result. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.