您的位置:首页 > 运维架构 > Shell

【PowerShell 一天一练】 6. 阶段小结

2008-10-05 16:31 225 查看

回忆一下最早的学习PowerShell的目的:

经常在网上下载东西,得到的大都是rar[/i]到压缩包,希望可以让rar[/i]自动解压然后放到以对应日期为名字的文件夹。正好想学习下powershell[/i],决定合二为一,试试看。也不想一口吃成胖子,毕竟每天还有那么多事,就一天学一点,逐步实现吧。[/i]

要实现这个目的,有以下事情要做:

1, 确定压缩文件所在目录(这个目前可以在ps1文件中指定)
2, 将目录中所有压缩文件找出,如果一个压缩文件也没便罢,否则创建以今天日期为名字的文件夹(当然,先要检查该文件夹是否存在)
3, 调用winrar将压缩文件一一解压并放到日期文件夹中

通过前面的学习,目前基本上万事俱备,只欠整合了。
全部的代码如下:

$curDir = "I:/tddownload"

Set-Location $curDir

$zipFiles = Get-ChildItem -path I:/Tddownload -name * -include *.rar

$dateText = get-date

$dateText = $dateText.ToShortDateString()

$file = Get-ChildItem -name * -include $dateText

if ( $file -eq $null )

{

write-host "将创建文件夹 $dateText"

new-item -path $curDir -name $dateText -type directory

}

else

{

write-host "文件夹" + $dateText + "已经存在!"

}

#调用winrar的 unrar.exe 来解压

$valOfWinRAR = gp hklm:/Software/Classes/WinRAR/shell/open/command | Get-Member –membertype

noteproperty

$temp= $valOfWinRAR[0].Definition.Split('"')

$unrarPath = $temp[1].ToUpper().replace("WINRAR.EXE", "UNRAR.EXE")

foreach($filename in $zipFiles)

{

&$unrarPath x -y "$filename" "$file"

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: