首页 > 解决方案 > 使用powershell解压缩文件夹后如何计算每个文件的列数

问题描述

我正在尝试解压缩文件夹并计算文件夹中存在的文件的数量并检查文件计数 = 32 否则发送带有当前文件名的电子邮件并为当前文件执行 infacmds 并等待满足 32 个条件。

在这个脚本中,我想检查现有文件的文件数和列数,如果列数 -neq 280,它必须发送电子邮件错误。

列代码是这​​样的,我必须在主代码中介绍:

$columnCount = ( ( Get-Content "C:\Users\xs15169\Desktop\temp\OEC2_CFLOW.txt" | Select-Object -First 1 ) -Split ',' ).Count
echo "Column count is: $columnCount"

我尝试过的以下代码(#looks 查找具有以下类型格式 .zip 的文件,它将解压缩文件并进行计数):

#unzip commands will be here
#LogWrite "Starting of process"
Function LogWrite
{
   Param ([string]$logstring)

   Add-content $log -value "$(Get-Date) $logstring"
}

  try { 
   LogWrite "Start unzipping $zipfilename"
   Remove-Item $unzipfolderdelete | Where { ! $_.PSIsContainer }
    #Unzip $zipfilename $unzipfolder

    Set-Alias sz "C:\Program Files\7-Zip\7z.exe"

    sz x $zipfilename -o"$unzipfolder" -y
    LogWrite "$zipfilename unzipped to $unzipfolder successfully"
    $filecount= (dir $unzipfolder | measure).Count
    if ($filecount -eq 32)
    {
        #LogWrite "File Count 32


        #LogWrite "Executed informatica scripts for condition filecount equal to 32"
        #LogWrite "End of if"
    }
    elseif ($filecount -ne 0)
    {

        LogWrite "File Count " + $filecount
        $list = Get-ChildItem $unzipfolder
        $incompletefilenames =""
        ForEach($n in $list){

        $incompletefilenames = $incompletefilenames + $n.Name + "<br>`n"

        }

        LogWrite $incompletefilenames
        LogWrite "Sending email for filecount not equal to 32"

            $Params = @{ 
               email setup
            }
            Send-MailMessage @Params
            LogWrite "Successfully emailed available file names"

        #Execute Informatica scripts


        LogWrite "Executed informatica scripts for condition filecount not equal to 32"
         LogWrite "End of elseif"
    }
    else
    {
         LogWrite "File Count " + $filecount
             $Params = @{ 
                email setup
            }
            Send-MailMessage @Params
          LogWrite "End of else"
    }
}

标签: powershellpowershell-3.0powershell-4.0

解决方案


为此,您必须弄清楚一些路径。

尝试这个:

Set-Alias sz "C:\Program Files\7-Zip\7z.exe"

$zipFile = "C:\temp\10 yard Fight.zip"

# Get Name of file without extension or path
$zipFolderToCreate = (gci $zipFile).BaseName

# Get path of zip
$path = (gci $zipFile).Directory.FullName

sz x -o"$path\*" "$zipFile"  -r

$filecount= (dir "$path\$zipFolderToCreate" | measure).Count

推荐阅读