【发布时间】:2019-10-14 23:37:15
【问题描述】:
我正在尝试创建一个执行以下操作的脚本:
1)检查 MASTERFOLDER 内的所有子文件夹/文件
2)所有文件扩展名相同,同一天修改
Script to Create a Monthly folder and move files to it every month.
代码基于上面的问题:
# Get the files which should be moved, without folders
$files = Get-ChildItem '\\test\d$\Reports\client *' -Recurse |
where {!$_.PsIsContainer}
# List Files and names which will be moved
$files.names
# Target Folder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = '\\test\d$\Reports\client\'
foreach ($file in $files)
{
# Get year and Month of the file
$year = $file.LastWriteTime.Year.ToString()
$month = $file.LastWriteTime.Month.ToString("00")
$monthname = (Get-Culture).DateTimeFormat.GetAbbreviatedMonthName($month)
LastWriteTime 不起作用 - 必须根据包含日期的名称对文件进行排序 - 例如: ClientReportX 20191014 file1.csv
也许每年使用 2015 - 2019 + 月份的 -match
$filesstructure| foreach-object (file in files) {
if($file.name-match '2019') {
#MOVE TO FOLDER 2019 - > MONTH OF FILE
ELSE($filename -match '2018' {
#MOVE TO FOLDER 2018 -> MONTH OF FILE
.....
其余代码
# Set Directory Path
$Directory = $targetPath + "\" + $month + $monthname
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
$file | Move-Item -Destination $Directory
}
目标:主文件夹 > 2019 - 2015 年每年的子文件夹列表。每个文件夹年份的内部 > 1 月 - 12 月。
感谢任何帮助
【问题讨论】:
-
this >>>
$Year, $Month, $Day = 'ClientReportX 20191014 file1.csv'.Split(' ')[1] -split '(....)(..)(..)' | Where-Object {$_}
标签: powershell