#Powershell script that runs robocopy and writes output to a text box in real time

7 messages · Page 1 of 1 (latest)

visual wraith
#

I currently have the script running robocopy but I cant figure out how to write the output text to a text box in real time. All of the solutions I have found so far all write the output text to a file or text box after the robocopy has finished. This makes the script seem like its frozen while robocopy is running. I want to mimic the behavior of running robocopy in a batch file.

hushed eagle
#

Why use robocopy? But either way the answer to your problem is to run the command in a separate runspace, session, job, or on another thread.

visual wraith
#

I used robocopy because this script was an attempt to improve on a batch file which I was previously using and I didn’t know of any other solutions. If there are better ways of copying and getting the output in real time I’d be interested in that

#

Also do you have anything I could refer to for an example of running robocopy on a separate thread?

hushed eagle
#

Ill get back to you its late here. I will send over some stuff tomorrow.

past valve
#

That's why UI's can be a bit of a pain in powershell, blocking IO
One option could be use the robocopy arguments for logging to a file
Your app can read the file

vapid fern
#

Robocopy can output to the Console, to a Log File, to Both and has options to control the content of that output.

Given that Robocopy outputs to the Console (cmd.exe) it's running in, you will need to figure how to capture the Console (cmd) output in powershell real-time.
Another thought is to how launch cmd in your PowerShell App and display the cmd console as a window or frame so you have that real-time console output and have RoboCopy also output to a log file (if you need to display that after the console / RoboCopy terminates.

Here is some code I use to create a catalog (text file) of every folder and file on disk starting with the C: Drive

$Src = "C:"
$Dst = "D:\Tmp"
$logFile = "C:\Temp\RoboLog_$DateStamp.txt"
$outFile = "C:\Temp\RoboLog_$DateStamp.csv"
$Timespan = Measure-Command -Expression {
$RobocopyResult = Start-Process -FilePath robocopy.exe -ArgumentList "$Src", "$Dst", ". /E /L /SL /NP /W:0 /R:0 /XJ /TS /FP /BYTES /TEE /UNILOG:"$logFile"" -Wait -PassThru
}
$RobocopyResult.ExitCode
$Timespan

I further process the logfile into a '|' delimited CSV file