windows PowerShell -在控制台中打印移动项

qcuzuvrc  于 2022-12-05  发布在  Windows
关注(0)|答案(3)|浏览(212)

下面的代码:

$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!"
        }

我想知道如何使所有$MP4Lenght的信件在控制台中以$MP4Lenght + "was moved"打印,因为这样我就可以知道哪些文件被移动了。

jdgnovmf

jdgnovmf1#

你的“exist to copy”逻辑并不是真正需要的,因为如果文件不存在,get-childitem就不会找到它。
下面的代码将检查文件是否存在于源中而存在于目标中,如果是,则写入到文件已移动的主机:

$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"
}

}

nbewdwxp

nbewdwxp2#

为什么不直接使用-verbose呢?

Move-Item -Destination (new-item -type directory -force ($SchoolFolder + $newSub)) -force -ea 0 -Verbose

根据您的意见更新。

这样试试...

$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"
    }
}
xxslljrj

xxslljrj3#

最终脚本:

$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!"
    }

相关问题