结合Get ChildItem和测试路径-将cmd/batch重写到PowerShell
最后,我尝试将一些旧的cmd/批处理脚本移动到PS!目前,我有一个脚本,可以找到所有的.bak文件,并在不存在的情况下创建一个7ziped文件 我当前的脚本如下所示:结合Get ChildItem和测试路径-将cmd/batch重写到PowerShell,powershell,Powershell,最后,我尝试将一些旧的cmd/批处理脚本移动到PS!目前,我有一个脚本,可以找到所有的.bak文件,并在不存在的情况下创建一个7ziped文件 我当前的脚本如下所示: for /R "E:\Backup" %%f in (*.bak) do ( echo %%f if not exist "%%f.7z" ( 7Z a "%%f.7z" "%%f" ) ) $path = &
for /R "E:\Backup" %%f in (*.bak) do (
echo %%f
if not exist "%%f.7z" (
7Z a "%%f.7z" "%%f"
)
)
$path = "E:\Backup"
$7z = "C:\Program Files\7-Zip\7z.exe"
$filter = "*.bak"
# Compress files not already compressed.
Get-ChildItem -Path $path -Filter $filter -File -Recurse |
ForEach-Object {
$archive = $_.FullName + '.7z'
If (!(Test-Path -Path $archive -PathType Leaf)){
& @7z a "$archive" "$($_.FullName)"
}
}
所以我试着用PS重写
这将为我提供所有.back文件的列表,但是如何将.7z添加到文件名并测试它是否存在(测试路径):
在下一步中,我将运行命令创建7z文件:
7z.exe a "file.bak.7z" "file.bak"
我应该将找到的所有文件存储在一个数组中并迭代该数组,还是应该使用“|”来链接命令
谢谢你的建议 无法测试,但类似的方法应该可以工作
Get-ChildItem -Path $path -Filter '*.bak' -File -Recurse |
Foreach-Object {
$zip = $_.FullName + '.7z'
If (!(Test-Path -Path $zip -PathType Leaf)) {
# create the zip file
7z.exe a $zip $_.FullName
}
}
无法测试,但类似的东西应该可以工作
Get-ChildItem -Path $path -Filter '*.bak' -File -Recurse |
Foreach-Object {
$zip = $_.FullName + '.7z'
If (!(Test-Path -Path $zip -PathType Leaf)) {
# create the zip file
7z.exe a $zip $_.FullName
}
}
谢谢@Theo,
我在这里学会了分配
我的最终PS如下所示:
for /R "E:\Backup" %%f in (*.bak) do (
echo %%f
if not exist "%%f.7z" (
7Z a "%%f.7z" "%%f"
)
)
$path = "E:\Backup"
$7z = "C:\Program Files\7-Zip\7z.exe"
$filter = "*.bak"
# Compress files not already compressed.
Get-ChildItem -Path $path -Filter $filter -File -Recurse |
ForEach-Object {
$archive = $_.FullName + '.7z'
If (!(Test-Path -Path $archive -PathType Leaf)){
& @7z a "$archive" "$($_.FullName)"
}
}
谢谢@Theo,
我在这里学会了分配
我的最终PS如下所示:
for /R "E:\Backup" %%f in (*.bak) do (
echo %%f
if not exist "%%f.7z" (
7Z a "%%f.7z" "%%f"
)
)
$path = "E:\Backup"
$7z = "C:\Program Files\7-Zip\7z.exe"
$filter = "*.bak"
# Compress files not already compressed.
Get-ChildItem -Path $path -Filter $filter -File -Recurse |
ForEach-Object {
$archive = $_.FullName + '.7z'
If (!(Test-Path -Path $archive -PathType Leaf)){
& @7z a "$archive" "$($_.FullName)"
}
}