将文件移动到新创建的子文件夹

时间:2017-10-05 17:32:47

标签: powershell

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

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

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$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

答案 1 :(得分:0)

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

$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
}
相关问题