[英]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: 最简单地说,我会执行以下操作:
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.