Reputation: 341
I have the following code:
$SchoolFolder = "C:\Users\MyUser\Desktop\School Folder\$StudentName\$Month. $MonthWrite\$Day. $DayWrite"
$MP4Lenght = (Get-ChildItem -Path $RenderFolder).Length -ne "0"
$MP4existsToCopy = Test-Path -Path "$RenderFolder\*.mp4"
If (($MP4existsToCopy -eq $True) -and ($MP4Lenght -eq $True)) {
Get-ChildItem $MyFolder |
Where-Object { $_.Length -gt 0KB} |
Move-Item -Destination (new-item -type directory -force ($SchoolFolder + $newSub)) -force -ea 0
Write-Host "Done!"
}
I would like to know how do I make all correspondence in $MP4Lenght
be printed in the console with the format $MP4Lenght + "was moved"
, because that way I can know which files were moved.
Upvotes: 0
Views: 182
Reputation: 341
Final script:
$StudentName = Tyler
$RenderFolder = "C:\Users\MyUser\Desktop\Render"
$MP4existsToCopy = Get-ChildItem $RenderFolder -File | where-object {$_.Length -ne 0}
$SchoolFolder = "C:\Users\MyUser\Desktop\School Folder\$StudentName\$Month. $MonthWrite\$Day. $DayWrite"
foreach ($file in $MP4existsToCopy) {
Move-Item $file.FullName -Destination (new-item -type directory -force ($SchoolFolder)) # new-item - Serves to create the folder if it does not exist
if (-not(Test-Path $file.FullName) -and (test-path (Join-Path -Path $SchoolFolder -ChildPath $file.Name))) {
Write-Host "$($file.name) was moved!"
}
Upvotes: 0
Reputation: 16096
Why not just use -verbose
?
Move-Item -Destination (new-item -type directory -force ($SchoolFolder + $newSub)) -force -ea 0 -Verbose
Update as per your comment.
Try it this way...
$source = 'C:\Users\myuser\playground\powershell\Source\'
$destination = 'C:\Users\myuser\playground\powershell\Destination'
Get-ChildItem $source -File |
where-object {$PSItem.Length -ne 0} |
ForEach-Object{
Move-Item $PSItem.FullName -Destination '.\Destination'
if (-not(Test-Path $PSItem.FullName) -and (test-path (Join-Path -Path $destination -ChildPath $PSItem.Name))) {
"$($PSItem.name) has moved"
}
}
Upvotes: 1
Reputation: 186
Your "exist to copy" logic isn't really required, because if the file doesn't exist then get-childitem is not going to find it. Similarly with the check if MP4lenght is true.
The following will check if the file does not exist in the source and does exist in the destination and if that is true then write to the host that the file has moved:
$source = 'C:\Users\myuser\playground\powershell\Source\'
$destination = 'C:\Users\myuser\playground\powershell\Destination'
$files = Get-ChildItem $source -File | where-object {$_.Length -ne 0}
foreach ($file in $files) {
Move-Item $file.FullName -Destination .\Destination
if (-not(Test-Path $file.FullName) -and (test-path (Join-Path -Path $destination -ChildPath $file.Name))) {
Write-Host "$($file.name) has moved"
}
}
Upvotes: 1