【问题标题】:Move file to newly created subfolder将文件移动到新创建的子文件夹
【发布时间】:2017-10-06 06:07:59
【问题描述】:

我必须每天将一个文件移动到一个文件夹,该文件夹是一个月文件夹。当新的月份到来时,脚本应该识别新创建的文件夹(我将使用不同的脚本创建)并开始将文件推送到该新文件夹。现在,当年份更改时,脚本应该识别带有新年的文件夹以及该月的子文件夹,并将文件推送到新的子文件夹。

对不起,如果这听起来令人困惑。需要帮助我知道如何使用 PowerShell 将文件移动到另一个文件夹,但进入该层次结构是我无法完成的。

【问题讨论】:

  • 请展示您到目前为止所尝试的内容。您可以使用 Get-Date 命令来完成此任务,并且有很好的文档记录。

标签: powershell


【解决方案1】:

试试这样的:

$CurrentDate=get-date
$year, $month= $CurrentDate.Year, $CurrentDate.Month
$storagedir="c:\temp\$year\$month"

if (! (Test-Path $storagedir))
{
    New-Item -ItemType Directory $storagedir
}

Move-Item "C:\temp\dbhash.csv" $storagedir -Force

【讨论】:

  • 非常感谢。关于如何获取月份名称的任何建议我都尝试过 $year, $month= $CurrentDate.Year, $CurrentDate.MonthNames 但没有奏效。
  • 试试这个:(Get-Culture).DateTimeFormat.GetMonthName($month)
  • 谢谢我已经用其他方式做到了。但问题是,当我再次运行此脚本以将其他文件移动到同一个子文件夹中时,它不起作用,因为子文件夹将在整个月内保持不变,它应该能够在该月每天移动同一子文件夹中的文件.
【解决方案2】:

没关系 明白了。下面是最适合我的最终脚本

$CurrentDate=get-date
$year = $CurrentDate.Year
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh") 
$storagedir="C:\Users\$year\$folderName"
if (! (Test-Path $storagedir))
{
    New-Item -ItemType Directory $storagedir
    }
if (Test-Path $storagedir)
{

Move-Item "C:\Users" $storagedir -Force
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    相关资源
    最近更新 更多