1 Answers
Best Answer
ls C:\Windows -File | foreach {
# 截取文件名前缀
$preffixNameLength = 5
if($_.BaseName.length -lt 5){
$preffixNameLength = $_.BaseName.length
}
$subName = $_.BaseName.substring(0,$preffixNameLength)
$targetDir = Join-Path d:\copy-test $subName
# 创建目标文件夹
if(-not (Test-Path $targetDir)){
mkdir $targetDir | Out-Null
}
$newName = Join-Path $targetDir $_.Name
#复制文件
Copy-Item $_.FullName -Destination $targetDir
Write-Host "copy $_ into $newName"
}
