如何在PowerShell中检查文件夹和子文件夹?

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

How can I check a folder and subfolder in PowerShell?

问题

我需要能够检查一个文件夹的内容,例如C:\Temp,如果包含图片,则运行一个计划任务,但忽略C:\Temp内的任何文件夹或其他文件类型,并将输出写入日志文件。

然后,我需要能够检查C:\temp内的子文件夹,即C:\Temp\myfolder。如果文件夹为空,则写入日志文件并不执行任何操作。否则,如果子文件夹包含图片文件,则将它们移到C:\Temp的上一级,运行一个计划任务,并写入日志文件。除了图片文件以外的所有文件类型都应该被忽略。

我已尝试过:

if((Get-ChildItem -Path C:\temp -force | Where-Object Extension -in ('.bmp','.jpg','.jpeg','.png','.tiff') | Measure-Object).Count -ne 0){
    Start-ScheduledTask -TaskName Notepad
}else{
    Write-Output "未找到文件。"
}
英文:

I need to be able to check the contents of a folder, for example, C:\Temp if contains pictures, run a scheduled task, but ignore any folders or other file types within C:\Temp and write the output to a log file.

I then need to be able to check a subfolder within C:\temp i.e. C:\Temp\myfolder. If folder is empty, write to log file and do nothing. Else if subfolder contains picture files, move them one level up to C:\Temp, run a scheduled task and write to a log file. All file types other than picture files should be ignored.

I have tried:

if((Get-ChildItem -Path C:\temp -force | Where-Object Extension -in ('.bmp','.jpg','.jpeg','.png','.tiff') | Measure-Object).Count -ne 0){
    Start-ScheduledTask -TaskName Notepad
}else{
    Write-Output "No files found."
}

答案1

得分: 1

我猜你想要做的是类似以下的操作:

# 将这些变量设置为您的实际环境
$sourceFolder = 'C:\Temp'
$imageTypes = '*.jpg','*.jpeg','*.png','*.bmp','*.gif','*.tiff'  # 等等
$logFile = 'X:\Somewhere\mylog.log'

# 首次运行,仅处理源文件夹
$imageFiles = Get-ChildItem -Path $sourceFolder -File -Include $imageTypes
$filesFound = @($imageFiles).Count
if ($filesFound -eq 0) {
    Add-Content -Path $logFile -Value "在 $sourceFolder 中未找到图像" -PassThru
}
else {
    Add-Content -Path $logFile -Value "$sourceFolder 包含 $filesFound 个图像文件" -PassThru
    # 运行您的定期任务
    Start-ScheduledTask -TaskName Notepad -Verbose
}

# 接下来查看子文件夹中是否有图像文件
$subFolder = Join-Path -Path $sourceFolder -ChildPath 'MyFolder'
$imageFiles = Get-ChildItem -Path $subFolder -File -Include $imageTypes
$filesFound = @($imageFiles).Count
if ($filesFound -eq 0) {
    Add-Content -Path $logFile -Value "在 $subFolder 中未找到图像" -PassThru
}
else {
    Add-Content -Path $logFile -Value "$subFolder 包含 $filesFound 个图像文件" -PassThru
    # 将文件移动到源文件夹
    $imageFiles | ForEach-Object {
        $_ | Move-Item -Destination $sourceFolder
    }
    # 运行您的定期任务
    Start-ScheduledTask -TaskName Notepad -Verbose
}

请注意,这只是代码的翻译部分,不包括问题的回答。

英文:

I guess what you want to do is something like below:

# set these variables to your actual environment
$sourceFolder = 'C:\Temp'
$imageTypes   = '*.jpg','*.jpeg','*.png','*.bmp','*.gif','*.tiff'  # etc.
$logFile      = 'X:\Somewhere\mylog.log'

# first run, just the sourcefolder
$imageFiles = Get-ChildItem -Path $sourceFolder -File -Include $imageTypes
$filesFound = @($imageFiles).Count
if ($filesFound -eq 0) {
    Add-Content -Path $logFile -Value "No images found in $sourceFolder" -PassThru
}
else {
    Add-Content -Path $logFile -Value "$sourceFolder contains $filesFound image files" -PassThru
    # run your scheduled task
    Start-ScheduledTask -TaskName Notepad -Verbose
}

# next see if there are image files in the subfolder
$subFolder  = Join-Path -Path $sourceFolder -ChildPath 'MyFolder'
$imageFiles = Get-ChildItem -Path $subFolder -File -Include $imageTypes
$filesFound = @($imageFiles).Count
if ($filesFound -eq 0) {
    Add-Content -Path $logFile -Value "No images found in $subFolder" -PassThru
}
else {
    Add-Content -Path $logFile -Value "$subFolder contains $filesFound image files" -PassThru
    # move the files to the source folder
    $imageFiles| ForEach-Object {
        $_ | Move-Item -Destination $sourceFolder
    }
    # run your scheduled task
    Start-ScheduledTask -TaskName Notepad -Verbose
}

huangapple
  • 本文由 发表于 2023年6月26日 22:29:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557654.html
匿名

发表评论

匿名网友

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

确定