以下是我的代码:
Function ZipOnebyOne{
$extension = Get-ChildItem $filePath
foreach ($file in $extension) {
$name = $file.name
$directory = $file.DirectoryName
$zipfile = $name.Replace($fileExtension,".7z")
sz a -t7z "$directory\$zipfile" "$directory\$name"
}
}
我想要的只是在$ filePath中单独压缩文件而不管它们的扩展名。上述方法不起作用。
答案 0 :(得分:0)
正如@KenWhite所说,你没有清楚地说明问题是什么,但是,我带你去抽样并解决了我能找到的所有问题。 也许,这对您有帮助。
Function ZipOnebyOne{
param(
[Parameter(Mandatory=$true)]
[string]
$filepath
)
$extension = Get-ChildItem $filePath -File -Recurse
foreach ($file in $extension) {
$name = $file.name
$directory = $file.DirectoryName
$fileExtension = $file.Extension
$zipfile = $name.Replace($fileExtension, '.zip')
Compress-Archive -Path (Join-Path $directory $name) -DestinationPath (Join-path $directory $zipfile) -Update
}
}
Function ZipOnebyOne{
param(
[Parameter(Mandatory=$true)]
[string]
$filepath
)
$7z = 'C:\Program Files\7-Zip\7z.exe'
$extension = Get-ChildItem $filePath -File -Recurse
foreach ($file in $extension) {
$name = $file.name
$directory = $file.DirectoryName
$fileExtension = $file.Extension
$zipfile = $name.Replace($fileExtension, '.7z')
$null = & $7z -t7z a (Join-path $directory $zipfile) (Join-Path $directory $name)
}
}