简体   繁体   English

根据文件名时间戳Powershell将文件移动到年/月文件夹

[英]Move files into year/month folders based on file name timestamp powershell

I have thousands of files spanning 5 years which I would like to move into year/month folders. 我有成千上万个跨5年的文件,希望将其移入年/月文件夹。 The file names all end with 文件名均以“。”结尾

_yyyy_mm_dd_wxyz.dat _yyyy_mm_dd_wxyz.dat

I'm looking for ideas on how I can generate such file folders and move the files into the appropriate folders yyyy/mm using the windows command shell. 我正在寻找有关如何生成此类文件夹并将文件使用Windows命令外壳程序yyyy / mm移至相应文件夹的想法。

You'll need a Regular Expression with (capture groups) to extract year/month from the filename. 您将需要一个带有(捕获组)的正则表达式,以从文件名中提取年/月。
Assuming the year/month folder should be placed directly in files parent location. 假设年/月文件夹应直接放置在文件的父位置。

un tested with -Version 2 未经 测试-Version 2

## Q:\Test\2018\07\23\SO_51485727.ps1
Push-Location 'x:\folder\to\start'
Get-ChildItem *_*_*_*_*.dat |
  Where-Object {$_.BaseName -match '_(\d{4})_(\d{2})_\d{2}_[a-z]+$'} | ForEach-Object {
    $TargetDir = "{0}\{1}" -f $Matches[1],$Matches[2]
    if (!(Test-Path $TargetDir)){MD $TargetDir | Out-Null}
    $_ | Move -Destination $TargetDir 
}

Sample tree /f after running the script on my ramdriive: 在我的ramdriive上运行脚本后,示例树/ f:

PS A:\> tree /F
A:.
├───2017
│   └───07
│           test_2017_07_24_xyz.dat
└───2018
    └───07
            test_2018_07_24_xyz.dat

I have created this little quick and dirty script. 我已经创建了这个快速而又肮脏的脚本。 Things have been put in more variables than strictly needed, they could be combined in a single line, but I feel this adds clarity which I hope help you understand what happens. 事情已经放入了比严格需要更多的变量,可以将它们组合在一行中,但是我认为这可以增加清晰度,希望可以帮助您了解会发生什么。

As a note, I have used the date the item was last written to (created or edited). 请注意,我使用了该项目的最后写入日期(创建或编辑)。 If you want only the date the file was created and not the time the file was last edited, you could change LastWriteTime to CreationTime 如果只希望创建文件的日期,而不想要文件的上次编辑时间,则可以将LastWriteTime更改为CreationTime。

#Load all files from the folder you wish to move on
$items = Get-ChildItem -Path "C:\SomeFolder\RestofPathToYourFiles"

foreach($item in $items) {
    #Creates variables for year, month and day
    $FolderYear = "$($item.LastWriteTime.Year)"
    $FolderMonth = "$($item.LastWriteTime.Month)"
    $FolderDay = "$($item.LastWriteTime.Day)"

    #create variable with the new directory path
    $NewPath = $item.Directory.FullName + "\" + $FolderYear + "\" + $FolderMonth + "\" + $FolderDay

    #create variable with the new full path of the file
    $NewFullPath = $NewPath + "\" + $item.Name

    #test if the folder already is created, if not, create it
    if((Test-Path -Path $NewPath) -eq $false) {
        New-Item -Force -path $NewPath -Type Directory
    }

    #move the item to the new folder
    Move-Item -Path $item.FullName -Destination $NewFullPath -Force
}

At the simplest, I'd do something like the following: 最简单地说,我会执行以下操作:

  1. Determine the year and month related to a file 确定与文件相关的年份和月份
  2. See if a folder exists already. 查看文件夹是否已经存在。 If not, create it 如果没有,请创建它
  3. Move the file 移动文件

Example... 例...

foreach ($file in $(ls .\stuff.txt)) {
    $m = $file.LastWriteTime.Month.ToString("00")
    $y = $file.LastWriteTime.Year
    $dir = "{0}-{1}" -f $y, $m
    New-Item -Name $dir -ItemType directory -ErrorAction SilentlyContinue | Out-Null
    Move-Item -Path $file.Fullname -Destination $dir
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell脚本,用于根据创建时间戳将文件移动到年/月文件夹中 - Powershell script to move files into year/month folders based on creation timestamp powershell根据文件名的一部分移动文件 - powershell to move files based on part of file name Powershell:提取文件名的一部分以创建文件夹并将文件移动到文件夹中 - Powershell: Extracting parts of file name to create folders and move files into the folder 根据年月LastWriteTime持久化子文件夹将文件移动到子文件夹 - Move files to sub folders based upon year and month LastWriteTime persevering subfolder 如何根据文件名中的搜索字符串创建文件夹并将文件移动到其中? - How to create folders based on search string in a file name and move files into it? 批量执行 Regex 或 powershell 脚本以生成文件夹并在按文件名中的关键字符串排序的相对文件夹中移动文件 - Implement Regex in batch or powershell script to generate folders and move files in relative folders ordered by key string in file name Powershell根据文件名的初始部分移动Excel文件 - Powershell to move Excel files based on Initial part of the file name Powershell 移动基于 x 天以前的文件和文件夹 - Powershell move files and folders based on older than x days Powershell:根据月份和年份创建文件夹结构并相应地复制文件 - Powershell: creating folder structure based on month and year and copying file accordingly 如何根据每个文件名中固定分隔符字符串的左侧字符串将文件移动到文件夹? - How to move files to folders based on string left to a fixed separator string in each file name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM