0

I have made a script using xcopy that generates a csv log file with the date to check that my copies are done.

I would like to be able to display only when it has copied files, and not display anything when there are 0 files copied.

How can I do this? if I didn't make myself clear let me know

Thanks :)

Here is my code:

$Logfile = "C:\Users\Name\Documents\Power\" 

Function LogWrite
{
   Param ([string]$logstring)

   Add-content $Logfile -value $logstring and 
}

function Get-TimeStamp 
{
   return "[{0:dd/MM/yy} {0:HH:mm:ss}]" -f (Get-Date)     
}


xcopy /s /f /y /b /d C:\Users\Name\Documents\SourceBack C:\Users\Name\Documents\DestBack 
>> xcopy.csv 

Write-Output "last copied file(s) on $(Get-TimeStamp)" | Out-file 
C:\Users\Name\Documents\Power\xcopy.csv -append 
Caeko
  • 13
  • 1
  • 3

1 Answers1

0

I think this is what you're looking for.

The output of xcopy is redirected to a temporary file. If that file contains only a single line (e.g. 0 File(s) copied), no further action is taken.

Otherwise, the output and an additional line with the timestamp is added to your CSV file.

At the end, regardless of the outcome, the temporary file is removed.

$Logfile = "C:\Users\Name\Documents\Power\xcopy.csv" 

$TempFile = New-TemporaryFile

try {
    Start-Process xcopy `
        -ArgumentList '/s','/f','/y','/b','/d','C:\Users\Name\Documents\SourceBack','C:\Users\Name\Documents\DestBack' `
        -RedirectStandardOutput $TempFile.FullName `
        -Wait

    $ProcessOutput = @(Get-Content $TempFile)

    if ($ProcessOutput.Length -gt 1) {
        ($ProcessOutput + "last copied file(s) on $(Get-Date -Format '[dd/MM/yy HH:mm:ss]')") -join "`n" | Add-Content $Logfile
    }
} finally {
    Remove-Item $TempFile -Force
}

Remarks:

  • I removed the Get-TimeStamp function, as it was only used once and doesn't add a lot of benefit in that case.

  • I also removed the LogWrite function as it isn't used in your sample code, and it contains a syntax error (stray and).

  • You're appending a line to a CSV file (last copied file(s) …), which is bound to cause issues when you try to parse that file later on.

Update

You're never too old to learn, and it seems that Start-Process isn't necessary at all. For more information, see here and here.

$Logfile = "C:\Users\Name\Documents\Power\xcopy.csv" 

[string[]] $Output = & xcopy /s /f /y /b /d C:\Users\Name\Documents\SourceBack C:\Users\Name\Documents\DestBack 2>&1

if ($ProcessOutput.Length -gt 1) {
    ($ProcessOutput + "last copied file(s) on $(Get-Date -Format '[dd/MM/yy HH:mm:ss]')") -join "`n" | Add-Content $Logfile
}
DocZerø
  • 8,037
  • 11
  • 38
  • 66
  • 1
    Hi DocZerø ! I really appreciate your response and the time you took to find a solution. I will get back to you as soon as I am back in the office. have a great day or evening depending on where you are on the globe ;) – Caeko Mar 16 '22 at 13:11
  • Hi, finally I saw with my partner that it was not serious if it also displayed the "0 files copied" when there was no copy. But I appreciate the time you took to help me. Thanks again ! – Caeko Mar 17 '22 at 10:36