Dos批处理脚本和AutoHotKey脚本的编写

现要在windows下批量解压缩带密码的zip文件,并依据zip文件名重命名解压后的文件

如果只是解压,在命令行窗口中输入:7z e -pinfected *.zip就可以了,但是解压的文件名都一样,所以需要编写批处理的脚本灵活处理


先下载安装7-zip,其安装目录下的命令7z支持命令操作,将目录添加到系统环境变量

我的文件密码均为"infected",则对应解压命令为:

7z e -pinfected %Filename%

e表示解压,-p接解压密码,没有空格

首先想到的是写dos脚本,这种方式最快了

dos命令的说明可参考:http://blog.csdn.net/tjhd1989/article/details/7516961

批量解压缩的dos脚本unzip.bat如下:

::Unzip all the zip files in directory dirpath, the unziping password is "infected"
@echo off
set dirpath="E:\Samples\offensivecomputing" 
cd /d %dirpath%
setlocal enabledelayedexpansion
for %%i in (*.zip) do (
echo Unzip %%i 
set newname=%%i
set newname=!newname:zip=exe! ;;
7z e -pinfected %%i
ren malware.exe !newname!
)
endlocal
pause

脚本的功能是遍历dirpath目录(非递归),对每一zip文件调用7z解压,将加压后的文件malware.exe(所有解压后的文件都是这个)重名为zip文件改后缀为exe的文件名。

----------------分割线-----------------

由于最近研究一些自动化的操作用到了AutoHotKey,顺便也写了个实现相同功能的脚本upzip.ahk:

;Automatically unzip the files in the folder dirpath
dirpath = E:\Samples\netsky\offensivecomputing

;open a cmd window
IfWinExist C:\Windows\system32\cmd.exe
{
    WinActivate
}
else
{
    Run cmd
    WinWait C:\Windows\system32\cmd.exe
    WinActivate
}

send @echo off{enter}
send cd /d %dirpath%{enter}

SetWorkingDir, %dirpath%

Loop, %dirpath%\*.zip, , 0  ; not recurse into subfolders.
	If ( A_Index = 1 )
		FileList := A_LoopFileName
	Else FileList .= "`n" A_LoopFileName

Loop, parse, FileList, `n
{
   FileName:=A_LoopField
   send echo %Filename%{enter}
   send 7z e -pinfected %Filename%{enter} 	;unzip current file by command 7z
   StringTrimRight, OutputVar, Filename, 3 	;trim the suffix "zip"
   send echo %errorlevel%{enter} ;or sleep a while, must add this line to waiting for "OutputVar" getting the value
   FileMove malware.exe,%OutputVar%exe 		;all the unzip file is malware.exe, we need to rename it with *.exe
 } 
AutoHotKey的脚本中实际上还是用的dos命令,所以不如dos脚本来得直接,运行起来比较慢。

但是AutoHotKey的方便之处体现在其他方面,如窗口的操作,键盘和鼠标的操作。

AutoHotKey参考:

http://www.autohotkey.com/docs/(官方文档最全面了)

http://blog.csdn.net/redraiment/article/details/6183040

http://jixiuf.github.io/autohotkey/AutoHotKey_1.html

http://www.autohotkey.com/board/topic/67034-recursive-loop-issue-with-filemove/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值