我想要一个Powershell脚本,该脚本将根据文件的日期将文件移至文件夹,然后根据文件名的前3个字符移至子文件夹。 我已经能够将文件移动到带日期的文件夹,但是不知道如何继续使用powershell创建子文件夹并将文件移动到正确的日期子文件夹。这是我目前正在使用的日期:
Get-ChildItem \\servername\path\path\path\path\New_folder\*.* -Recurse | foreach {
$x = $_.LastWriteTime.ToShortDateString()
$new_folder_name = Get-Date $x -Format yyMMdd
$des_path = "\\servername\path\path\path\path\$new_folder_name"
if (test-path $des_path){
move-item $_.fullname $des_path
} else {
new-item -ItemType directory -Path $des_path
move-item $_.fullname $des_path
}
}
答案 0 :(得分:1)
使用SubString()
方法,您可以提取给定字符串的特定部分:
$SourcePath = '\\servername\path\path\path\path\New_folder'
$DestinationRoot = '\\servername\path\path\path\path'
Get-ChildItem $SourcePath -Recurse -File |
ForEach-Object {
$timeStamp = Get-Date $( $_.LastWriteTime) -Format 'yyMMdd'
$FirstThreeLettersFromFileName = $_.BaseName.SubString(0,3)
$destinationPath = "$DestinationRoot\$timeStamp\$FirstThreeLettersFromFileName"
if (-not (Test-Path -Path $destinationPath)) {
New-Item -ItemType Directory -Path $destinationPath
}
Move-Item -Path $_.fullname -Destination $destinationPath
}