patternshellMinor
Fastest Windows script to move files from one did to a network dir
Viewed 0 times
scriptdiddirmoveonefilesfastestwindowsfromnetwork
Problem
I wrote a script that moves files older than two days over to a network share (via scheduled task). Windows Task Scheduler doesn't seem to like the script so much, but it works well by itself.
-
Is there a faster way than what I have?
-
Is there a way to programmatically make it a scheduled task or service of some type?
Backup.ps1
-
Is there a faster way than what I have?
-
Is there a way to programmatically make it a scheduled task or service of some type?
Backup.ps1
$today = get-date
$tda= $today.addDays(-2) #two days ago
$files = get-childitem d:\source_path | select fullname, lastwritetime | where-object{$_.lastwritetime -lt $tda}
foreach ($file in $files) {
Echo "moving " $file.fullname #just so I know it's still running, get rid of this?
mv $file.fullname 'n:\dest_folder'
Echo 'done'
}Solution
Okay, I'll start by trying to answer your questions first.
-
If there is a lot of files and you should look into using runspaces to run several copy-jobs in separate threads. Just be careful, you are still limited by the bandwidth of the network.
-
We need to see some kind of error to be certain, but I would start by using PowerShell commands instead of old dos commands.
Which leads me to the second part of this answer. The actual code review :)
First thing; if you are going to write a script in PowerShell, use PowerShell commands :) Replace
Second: you don't need
As others have pointed out, you should add some error handling and thinking about what would happen if the destination folder already have a file with the same name. Using
Lastly, if you are new to PowerShell, start commenting your code. You'll thank yourself when you go back to review your scripts 6 months from now :)
Oh, by the way, If you read up on
-
If there is a lot of files and you should look into using runspaces to run several copy-jobs in separate threads. Just be careful, you are still limited by the bandwidth of the network.
-
We need to see some kind of error to be certain, but I would start by using PowerShell commands instead of old dos commands.
Which leads me to the second part of this answer. The actual code review :)
First thing; if you are going to write a script in PowerShell, use PowerShell commands :) Replace
echo with Write-Host and replace mv with Copy-Item.Second: you don't need
| select fullname, lastwritetime. It probably just adds (a little bit of) latency to the execution of the script.As others have pointed out, you should add some error handling and thinking about what would happen if the destination folder already have a file with the same name. Using
-Force is one way, but would overwrite the existing file. So if you don't want this, you will have to handle it differently.Lastly, if you are new to PowerShell, start commenting your code. You'll thank yourself when you go back to review your scripts 6 months from now :)
Oh, by the way, If you read up on
Copy-Item, there is a -Filter parameter that might make the whole Get-ChildItem part of the script redundant.Context
StackExchange Code Review Q#47873, answer score: 5
Revisions (0)
No revisions yet.