[英]How to continue ps to create folder based upon first 3 characters of file name
I would like a powershell script that would move files into a folder based upon the date of the file and then move into subfolders based upon the first 3 characters of the file names.我想要一个 powershell 脚本,它会根据文件的日期将文件移动到一个文件夹中,然后根据文件名的前 3 个字符移动到子文件夹中。 I have been able to move the files to a dated folder, but do not know how to continue with powershell to create subfolders and move files to correct date subfolder.我已经能够将文件移动到过时的文件夹,但不知道如何继续使用 powershell 创建子文件夹并将文件移动到正确的日期子文件夹。 This is what I have and is working for the date:这是我所拥有的并且正在为日期工作:
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
}
}
With the SubString()
method you can extract a particular part of a given string:使用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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.