移动文件使用 Powershell。

huangapple go评论44阅读模式
英文:

Moving files with Powershell

问题

以下是您的代码的翻译部分:

# 设置变量
$sourceFolder = "C:\mysource\folder"
$destinationFolder = "C:\mydestination\folder"
$extensionFilter = "*.hdf"
$zipFileName = "New_Drawings_$(Get-Date -Format 'yyyy-MM-dd').zip"
$processedFilesFile = "C:\mydestination\folder\myProcessedFiles.txt"

# 检查已处理文件的文件是否存在,如果不存在则创建
if (!(Test-Path $processedFilesFile)) {
    New-Item -Path $processedFilesFile -ItemType File
}

# 获取与源文件夹及其子文件夹中的扩展名过滤器匹配且尚未处理过的文件列表
$fileList = Get-ChildItem -Path $sourceFolder -Recurse -Filter $extensionFilter | Where-Object {!($_.PSIsContainer) -and $_.LastWriteTime.Date -eq (Get-Date).AddDays(-1).Date -and (Get-Content $processedFilesFile) -notcontains $_.Name}

# 检查是否找到任何文件
if ($fileList.Count -gt 0) {
    # 创建一个带有昨天日期的新 zip 文件
    $zipFileName = "New_Drawings_$(Get-Date).AddDays(-1).ToString('yyyy-MM-dd').zip"
    $zipFilePath = Join-Path $destinationFolder $zipFileName
    Compress-Archive -Path $fileList.FullName -DestinationPath $zipFilePath

    # 将 zip 文件移动到源文件夹
    $zipFile = Get-Item -Path $zipFilePath
    $destinationPath = Join-Path $sourceFolder $zipFileName
    Move-Item -Path $zipFile.FullName -Destination $destinationPath

    # 将已处理文件的名称添加到已处理文件的文件中
    $fileList.Name | Out-File $processedFilesFile -Append
}

关于您的代码问题或权限问题的询问,可能涉及权限问题。您需要确保脚本运行的用户具有足够的权限来访问源文件夹和目标文件夹,以及执行文件操作。您可以尝试以管理员身份运行脚本或确保脚本运行的用户具有适当的权限。此外,您还可以查看脚本生成的文本文件是否正确地记录了处理的文件,以便排除其他潜在问题。

英文:

I have the powershell script below.

The intent is for the script to look for a specific drawing file in my source folder and move them to another folder.

The script is also designed to check if file have already been copied and skip over those files.

the script runs it generates a text file for the list of files but is empty and does not copy any files either.

Eventually I want this to run in task scheduler

# Set variables
$sourceFolder = "C:\mysource\folder"
$destinationFolder = "C:\mydestination\folder"
$extensionFilter = "*.hdf"
$zipFileName = "New_Drawings_$(Get-Date -Format 'yyyy-MM-dd').zip"
$processedFilesFile = "C:\mydestination\folder\myProcessedFiles.txt"

# Check if the processed files file exists and create it if it doesn't
if (!(Test-Path $processedFilesFile)) {
    New-Item -Path $processedFilesFile -ItemType File
}

# Get the list of files that match the extension filter in the source folder and its        subfolders and haven't been processed before
$fileList = Get-ChildItem -Path $sourceFolder -Recurse -Filter $extensionFilter | Where-Object {!($_.PSIsContainer) -and $_.LastWriteTime.Date -eq (Get-Date).AddDays(-1).Date -and (Get-Content $processedFilesFile) -notcontains $_.Name}

# Check if any files were found
if ($fileList.Count -gt 0) {
    # Create a new zip file with yesterday's date in the name
    $zipFileName = "New_Drawings_$(Get-Date).AddDays(-1).ToString('yyyy-MM-dd').zip"
    $zipFilePath = Join-Path $destinationFolder $zipFileName
    Compress-Archive -Path $fileList.FullName -DestinationPath $zipFilePath

    # Move the zip file to the source folder
    $zipFile = Get-Item -Path $zipFilePath
    $destinationPath = Join-Path $sourceFolder $zipFileName
    Move-Item -Path $zipFile.FullName -Destination $destinationPath

    # Add the names of the processed files to the processed files file
    $fileList.Name | Out-File $processedFilesFile -Append
}

is there a problem with my code or is it likely to be associated with a permissions issues? There are lots of files that meet the criteria and I have also tried with other file extensions and still doesn't work

thanks in advance for any help

答案1

得分: 2

问题出在日期比较上,具体在这一行:

$_.LastWriteTime.Date -eq (Get-Date).AddDays(-1).Date

因为 Get-Date 部分会返回:

10/05/2023 00:00:00

而 LastWriteTime.Date 行会返回文件修改日期的日期和时间。

所以,你需要使用 -ge 来捕捉选择日期午夜之后的所有时间。像这样:

$fileList = Get-ChildItem -Path $sourceFolder -Recurse -Filter $extensionFilter | Where-Object {!($_.PSIsContainer) -and $_.LastWriteTime.Date -ge (Get-Date).AddDays(-1).Date -and (Get-Content $processedFilesFile) -notcontains $_.Name}

这就是为什么如果你尝试输出 $filelist,你会发现它是空的,因为没有文件符合这些条件。

英文:

The issue is with your date comparison, specifically where you have

$_.LastWriteTime.Date -eq (Get-Date).AddDays(-1).Date

as while the Get-Date part will return :

10/05/2023 00:00:00

the LastWriteTime.Date line will return both the date and time of the file's modified date.

So rather then -eq you need -ge to catch all times AFTER midnight of the chosen day. So like this

$fileList = Get-ChildItem -Path $sourceFolder -Recurse -Filter $extensionFilter | Where-Object {!($_.PSIsContainer) -and $_.LastWriteTime.Date -ge (Get-Date).AddDays(-1).Date -and (Get-Content $processedFilesFile) -notcontains $_.Name}

That's why if you'd tried outputting $filelist you'd have found it was empty, as none of the files were matching those conditions.

huangapple
  • 本文由 发表于 2023年5月11日 06:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定