首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按月将文件移动到文件结构中

按月将文件移动到文件结构中
EN

Stack Overflow用户
提问于 2014-11-12 18:04:46
回答 2查看 297关注 0票数 0

大家好,我正在尝试将每个月的文件移动到一个文件结构中,结构是这样的:

代码语言:javascript
运行
复制
Year >> Quarter >> Month >> FileType

日志文件会自动放到一个日志文件夹中--我想把这些文件中的任何一个放入上面的结构中。

我尝试了以下几种方法:

代码语言:javascript
运行
复制
function moveFiles{
# The three parameters. 
param([string]$sourceDir, [string]$type, [string]$destinationDir) 
# Move the files
Get-ChildItem -Path $sourceDir -Include $type | Move-Item -Destination $destinationDir -  Force
}

然而,每个月它都会将每个文件移动到新的月份中,我知道这与递归有关,但我删除了它,但没有文件被移动。

任何帮助都将不胜感激。

我如何调用函数:

代码语言:javascript
运行
复制
$sourceDir = "C:\Logs"
$destinationExcelDir = ($monthFolder + "\Excel Files");
#Moving all Excel files from logs folder to Archive
moveFiles $sourceDir "*.xls" $destinationExcelDir

使用write-host,我可以确认两个路径都是正确的,而且也是不同的,但是我尝试将文件复制到同一主目录下的子文件夹中。

例如,$destinationExcelDir为"C:\Logs\2014\quarter 4\11.11\Excel“

EN

Stack Overflow用户

发布于 2014-11-13 13:30:21

假设您希望按照文件的上次写入时间对C:\logs中的文件进行排序,下面的函数修订版应该会有所帮助。您只需为路径提供一个通配符,其中包含扩展名和路径名中的最后一个文件夹名称作为函数的参数。

示例用法: moveFiles C:\Logs*.xls "Excel文件“

代码语言:javascript
运行
复制
function moveFiles {
    param(
    [string]$source,
    [string]$destinationDir
    )

Get-ChildItem $source -file | foreach {
    $year = $_.LastWriteTime.Year

    # Switch statement to get the quarter name
    switch -regex ($_.LastWriteTime.Month) {
        "[1-3]" {$quarter = 'Quarter 1'}
        "[4-6]" {$quarter = 'Quarter 2'}
        "[7-9]" {$quarter = 'Quarter 3'}
        "[10-12]" {$quarter = 'Quarter 4'}

    }
    # Switch statement to get the month name
    switch ($_.LastWriteTime.Month) {
        '1' {$month = 'January'}
        '2' {$month = 'Feburary'}
        '3' {$month = 'March'}
        '4' {$month = 'April'}
        '5' {$month = 'May'}
        '6' {$month = 'June'}
        '7' {$month = 'July'}
        '8' {$month = 'August'}
        '9' {$month = 'September'}
        '10' {$month = 'October'}
        '11' {$month = 'November'}
        '12' {$month = 'December'}
    }

    # Create the destination folder if it doesn't exist
    if (!(test-path "C:\Logs\$year\$quarter\$month\$destinationDir")) {
        New-Item -ItemType Directory -Path "C:\Logs\$year\$quarter\$month\$destinationDir"
    }
    # Move the files into the correct folder
    move-item $_ "C:\Logs\$year\$quarter\$month\$destinationDir"
}

}

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26884179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档