Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 向压缩文件名添加路径_Powershell_Zip - Fatal编程技术网

Powershell 向压缩文件名添加路径

Powershell 向压缩文件名添加路径,powershell,zip,Powershell,Zip,我需要在文件名中添加文件的位置。 如下所示:C\user\someuser\folder\folder\u日志 压缩文件的名称应该是2019_Jan_folder_logs 我让它一直工作到日期,无法添加名称 $Zip = $target_path + "" + "{0:yyyy}_{0:MMM}" -f $_.LastWriteTime & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 $Zip $_.Ful

我需要在文件名中添加文件的位置。 如下所示:C\user\someuser\folder\folder\u日志

压缩文件的名称应该是2019_Jan_folder_logs

我让它一直工作到日期,无法添加名称

    $Zip = $target_path + "" + "{0:yyyy}_{0:MMM}" -f $_.LastWriteTime
    & "C:\Program Files\7-Zip\7z.exe" u -mx9 -t7z -m0=lzma2 $Zip $_.FullName | Out-Null
    if ($LastExitCode -eq 0) {
        Remove-Item $_.FullName
    }
}

根据所需的zip名称,您只希望名称中包含路径的最后一部分。 例如,如果当前文件$的LastWriteTime为2019年4月1日,则

$targetpath = 'C:\user\someuser\folder\folder_logs'
$Zip = '{0:yyyy_MMM}_{1}' -f $_.LastWriteTime, (Split-Path -Path $targetpath -Leaf)
将导致

2019_Apr_folder_logs
但是,在代码块中,可以颠倒顺序,将$targetpath放在新名称前面。 在这里也这样做:

$Zip = '{0}_{1:yyyy_MMM}' -f (Split-Path -Path $targetpath -Leaf), $_.LastWriteTime
结果:

folder_logs_2019_Apr
当然,如果需要,您也可以将实际文件名$\ux.BaseName附加到该文件中