简体   繁体   English

根据文件夹和文件名输出

[英]Output on the basis of folder and file name

I have some directories structure like below, where the last folder name will be the current date changing everyday, as below: 我有一些目录结构,如下所示,其中最后一个文件夹名称将是每天更改的当前日期,如下所示:

D:\data\Backup\WINDOWSDATA\18-03-2015
D:\data\Backup\LINUXDATA\18-03-2015
D:\data\Backup\UBUNTUDATA\18-03-2015

Under each date folder (18-03-2015) there will be maximum four .dat files having different time stamps in their names, as given below: 在每个日期文件夹(18-03-2015)下, maximum four .dat files的名称带有不同的时间戳,如下所示:

BKP_DS_FETCHER_6AM.dat
BKP_DS_FETCHER_10AM.dat
BKP_DS_FETCHER_2PM.dat
BKP_DS_FETCHER_6PM.dat

I am trying to generate following results in an output.txt file on the basis of simple logic that is if .dat file is there for a particular time, there should come Success otherwise Failed in output.txt for example as below: 我正在尝试基于简单逻辑在output.txt文件中生成以下结果,即如果.dat文件存在特定时间,则应该Success否则在output.txt Failed ,例如如下所示:

output.txt: output.txt:

FOLDER_NAME       6AM       10AM     2PM       6PM
WINDOWSDATA       Success   Failed   Success   Success
LINUXDATA         Success   Success  Failed    Success
UBUNTUDATA        Failed    Success  Success   Success

Please can somebody help me show the way to achieve it (in Batch or Powershell) ? 请有人可以帮助我展示实现它的方法(批处理或Powershell)吗?

Here is a way to do it in PowerShell: 这是在PowerShell中执行此操作的方法:

$date = Get-Date -Format 'yyyy-MM-dd'

$basePath = 'D:\data\Backup\'
$outputPath = 'D:\data\Backup\output_' + $date + '.txt'

$baseFileName = 'BKP_DS_FETCHER_[HOUR].dat'
$hours = @( '6AM', '10AM', '2PM', '6PM' )


function Check-Folder( $folderName )
{
    $resultLine = New-Object -TypeName System.Object 
    $resultLine | Add-Member -MemberType NoteProperty -Name 'FOLDER_NAME' -Value $folderName

    foreach( $h in $script:hours )
    {
        $path = $script:basePath + $folderName + '\' + $script:date + '\' + $script:baseFileName.Replace('[HOUR]',$h)
        #Write-Host $path

        if( Test-Path -Path $path )
        {
            $resultLine | Add-Member -MemberType NoteProperty -Name $h -Value 'Success'
        }
        else
        {
            $resultLine | Add-Member -MemberType NoteProperty -Name $h -Value 'Failed'
        }
    }

    return $resultLine
}

$results = @()

$results += Check-Folder 'WINDOWSDATA'
$results += Check-Folder 'LINUXDATA'
$results += Check-Folder 'UBUNTUDATA'

$results | Format-Table -AutoSize | Out-File -FilePath $outputPath

Output will look like this: 输出将如下所示:

FOLDER_NAME 6AM     10AM    2PM     6PM   
----------- ---     ----    ---     ---   
WINDOWSDATA Success Failed  Success Success
LINUXDATA   Failed  Failed  Failed  Failed
UBUNTUDATA  Failed  Success Failed  Failed
Push-Location D:\data\Backup
$todayDir = Get-Item (get-date -Format 'dd-MM-yyyy') -ErrorAction SilentlyContinue
if($todayDir)
{
Push-Location $todayDir
$logResult = @{}
dir -Directory | foreach {
$logResult.($_.Name) = $_.Name | Get-ChildItem -Filter '*.dat' |
foreach {
           $isMatch = $_.Name -match 'BKP_DS_FETCHER_(?<hour>.*).dat$'
           if($isMatch) {
            $Matches.hour
           }
        }
    }

$hourProperties = ($logResult.Values | foreach {$_}) |  Sort-Object | Get-Unique 

$logResult.Keys | foreach {
$obj = New-Object pscustomobject
$obj | Add-Member -MemberType NoteProperty -Name 'FOLDER_NAME' -Value $_
foreach($p in $hourProperties) {
        $v=$null
        if($logResult[$_] -contains $p) { 
            $v = 'Success'
        }
        else {
            $v = 'Failed' 
        }
        $obj | Add-Member -MemberType NoteProperty -Name $p -Value $v
    }

    $obj
    } | Format-Table 

    Pop-Location

}
Pop-Location

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 批处理文件以计算文件夹中的所有文件和子文件夹文件,并使用批处理文件以文件夹名称写入输出文件 - batch file to count all files in a folder and subfolder files and write output file in folder name using batch file 使用powershell输出多个.csv文件附加.csv文件名作为源文件夹名称 - Output multiple .csv files appending the .csv file name as the source folder name with powershell 如何将批处理文件的结果输出到与输入文件同名的文件夹中? - How do I output the results of a batch file into a folder with the same name as the input file? 查找.bat文件的文件夹名称 - Finding the folder name of .bat file 获取文件夹中文件的全名 - Get the full name of file in a Folder Powershell:如何在一行中输出文件夹名称,lastwritetime和文件夹大小? - Powershell: How to output folder name, lastwritetime and folder size in one line? 为每个文件名创建文件夹并移动文件 - Create folder for each file name and move the file 在文件夹中的每个文件上运行命令,然后保存输出 - Running a command on every file in a folder, then saving output Windows文件夹名称转换为excel输出文件 - Windows folder names to excel output file 获取文件夹中文件详细信息的格式化输出 - Get formatted output of file details in a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM