如何在powershell中管理从一年到另一年的过渡

如何在powershell中管理从一年到另一年的过渡,powershell,Powershell,我要压缩以下目录列表: 2019_40 2019_41 2019_42 2019_43 2019_44 2019_45 2019_46 2019_47 2019_48 2019_49 2019_50 2019_51 2019_52 2020_01 2020_02 2020_03 2020_04 2020_05 这是我的代码来压缩它们。我将2020年设定为模拟下一年将发生什么: ### Set 7Zip excutabe as variable $7Zip = gci c:\Program*

我要压缩以下目录列表:

2019_40
2019_41
2019_42
2019_43
2019_44
2019_45
2019_46
2019_47
2019_48
2019_49
2019_50
2019_51
2019_52
2020_01
2020_02
2020_03
2020_04
2020_05
这是我的代码来压缩它们。我将2020年设定为模拟下一年将发生什么:

### Set 7Zip excutabe as variable 
$7Zip = gci c:\Program* -include '7z.exe' -recurse -ea 4|select -first 1 -expand fullname

# Date 
$Year = get-date -UFormat %Y
$WeekNb = get-date -UFormat %V
#Simulation the future
$Year = "2020"
$WeekNb = "02"

$Dir_To_Zip = Get-ChildItem -path "C:\Temp\LOG Script"
Foreach ($element in $Dir_To_Zip ) 
{  
$DirName =  $element.Name  
$DirNameY = $DirName.Substring(0, $DirName.Length -3 )

$DirNameW = $DirName.Substring($DirName.Length -2) 

if ( $DirNameW -lt $WeekNb - 4 )
 { 
& $7zip a "$element.zip" "$element" }
    }
在这种情况下,我的脚本是只对2020年的目录进行ziping,而不是2019年的目录。 如果我更改此行:

if ( $DirNameW -lt $WeekNb - 4 )
通过这个

if (( $DirNameY -le $Year -1 ) -or  ($DirNameW -lt $WeekNb -4 ))

所有目录都是压缩的

使用
-或
条件,
$DirNameY
不被视为字符串编号,因此我更正如下:

#Set 7Zip excutabe as variable
$7Zip = gci c:\Program* -include '7z.exe' -recurse -ea 4|select -first 1 -expand fullname

# Date 
$Year = get-date -UFormat %Y
$WeekNb = get-date -UFormat %V
# DEBUG pour forcer l annee et le numero de seamine
$Year = "2020"
$WeekNb = "05"

# List  files to  Zip
$Dir_To_Zip = Get-ChildItem -path "C:\Temp\LOG Script"
Foreach ($element in $Dir_To_Zip ) 
{  
$DirName =  $element.Name  
$DirNameY = $DirName.Substring(0, $DirName.Length -3 )

$DirNameW = $DirName.Substring($DirName.Length -2) 
$DirNameW = $DirNameW.trimstart('0') 
if (( $DirNameY -lt $Year -1 ) -or  ($DirNameW -lt $WeekNb -4 )) 
 { 
& $7zip a "$element.zip" "$element" }
    }