ftp 사이트에서 디렉토리로 지속적으로 가져 오는 파일이 있습니다.
모두 다음 형식으로 이름이 지정됩니다. yyyyMMdd_file1.txt
20160612_file1.txt
20161225_file2.txt
생성 날짜가 45 일 이상 지난 파일을 기본 이름의 날짜를 기준으로 자체 폴더로 이동하려고합니다. 따라서 코드가 실행되면를 가져와 20160612_file1.txt
자동으로 파일을 생성하여라는 폴더로 이동 20160612
해야하지만 다른 파일에는 아무 작업도 수행하지 않습니다.
Get-ChildItem \\myfilepath | Where-Object {!$_.PSIsContainer -and $_.CreationTime.Date -lt (Get-Date).AddDays(-45)} | Foreach-Object{
$dest = Join-Path $_.DirectoryName $_.BaseName.Split('\_')[0]
if(!(Test-Path -Path $dest -PathType Container))
{
$null = md $dest
}
$_ | Move-Item -Destination $dest -Force
}
날짜 비교가 제대로 작동하지 않는 것 같습니다. 이견있는 사람?
추가 정보 :
위의 스크립트는 오류를 반환합니다. 구체적으로 특별히:
PS C:\temp4> .\movefiles.ps1
At C:\temp4\movefiles.ps1:1 char:126
+ ... Object {!$_.PSIsContainer -and $_.CreationTime.AddDays(0) -lt Get-Dat ...
+ ~
You must provide a value expression following the '-lt' operator.
At C:\temp4\movefiles.ps1:1 char:127
+ ... ontainer -and $_.CreationTime.AddDays(0) -lt Get-Date.AddDays(-45)} | ...
+ ~~~~~~~~~~~~~~~~
Unexpected token 'Get-Date.AddDays' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : ExpectedValueExpression
CreationTime.Date와 (get-Date) .AddDays (-45) 값을 비교할 수없는 것 같습니다.
아래 작업
$archivedate = (Get-Date).AddDays(-45)
Get-ChildItem \\filepath | Where-Object {!$_.PSIsContainer -and ($_.LastWriteTime -lt $archivedate)} | Foreach-Object{
$dest = Join-Path $_.DirectoryName $_.BaseName.Split('_')[0]
if(!(Test-Path -Path $dest -PathType Container))
{
md $dest
}
$_ | Move-Item -Destination $dest -Force
}
나는 이것을 사용했고 오류가 없었다. CreationTime.Date가 없습니다.
Get-ChildItem \\myfilepath |where {!$_.PSIsContainer -and ($_.CreationTime -lt (Get-Date).AddDays(-45))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다