简体   繁体   English

如何根据文件名的前3个字符继续ps创建文件夹

[英]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.

相关问题 无法基于文件名创建文件夹 - Can't create a folder, based on file name 如何用字符串替换文件名中的第一个字符? - How to replace first characters in a file name with a string? 根据文件名复制基于指定文件夹的文件。 创建文件夹(如果不存在) - Copy file based a specified folder based on file name. Create folder if it doesn't exist PS如何在文件名中间加符号 - How to add a symbol in a middle of a file name in PS 如何在Powershell中基于文件扩展名创建文件夹 - how to create a folder based on a file extenstion in powershell 在文件夹名称中支持 Unicode 字符的情况下,如何将文件夹移动到具有文件夹第一个字符名称的子文件夹中? - How to move folders into subfolders with name of first character of the folder to move with support for Unicode characters in folder names? 如何使用基于Powershell中当前日期的名称创建文件夹? - How to create a folder using a name based on the current date in Powershell? Powershell 命令/脚本根据日期将文件移动到子目录(在文件名的前 8 个字符中) - Powershell command / script to move files to subdirectories based on date (in the first 8 characters of the file name) 根据部分文件名创建文件夹名称 - create folder name base on partial file name 我将如何告诉 PS 读取文件名并使用它? - How would I tell PS to read a file name and use it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM