如何用正则替换来实现功能?(已解决)
本帖最后由 gzh888666 于 2014-12-19 10:09 编辑以前学的点正则全还回去了,重新学习,请教要实现替换扩展名,并且根据相应的扩展名替换文件名的第一位,再根据文件的第一位还原扩展名。
au3.au3.rmvb
替换为
1u3.au3.rar
然后
1u3.au3.rar
再替换回
1u3.au3.rmvb
#region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_Outfile_type=a3x
#AutoIt3Wrapper_icon=C:\Windows\system32\SHELL32.dll|-24
#AutoIt3Wrapper_Outfile=E:\moveFild.a3x
#endregion ;**** 参数创建于 ACNWrapper_GUI ****
;视频保存转换
#include <Array.au3>
#include <File.au3>
Dim $jiance ;用于检测是否是压缩文件,0为是,1为不是
$lujing = "E:\360驱动大师目录\驱动备份\"
$wenjian = _FileListToArray($lujing, "*", 1)
;_ArrayDisplay($wenjian)
If _TiQuKuoZhanMing($wenjian) <> "rar" Then
For $n = 1 To UBound($wenjian) - 1
$kuozhanming = _TiQuKuoZhanMing($wenjian[$n])
$ChangDu = StringLen($kuozhanming)
$xinlujing = StringTrimRight($wenjian[$n], $ChangDu) & "rar"
If _kuozhanming_xuliehao_huhuan($kuozhanming) <> "" Then
$xinlujing = _kuozhanming_xuliehao_huhuan($kuozhanming) & StringTrimLeft($xinlujing, 1)
FileMove($lujing & $wenjian[$n], $lujing & $xinlujing)
Else
MsgBox(0, "", "("& $wenjian[$n] & ")的扩展名未添加到源码!")
EndIf
$jiance = 1
Next
Else
For $n = 1 To UBound($wenjian) - 1
$diyiwei = StringLeft($wenjian[$n], 1)
If _kuozhanming_xuliehao_huhuan($diyiwei) <> "" Then
$xinlujing = StringTrimRight($wenjian[$n], 3) & _kuozhanming_xuliehao_huhuan($diyiwei)
FileMove($lujing & $wenjian[$n], $lujing & $xinlujing)
Else
MsgBox(0, "", "("& $wenjian[$n] & ")的扩展名丢失!请手动更换尝试!")
EndIf
$jiance = 0
Next
EndIf
If $jiance = 0 Then
If MsgBox(1, "", "你确定要打开文件夹吗?") = 1 Then _ExplorerDir($lujing)
ElseIf $jiance = 1 Then
MsgBox(0, "", "现在是压缩文件", 3)
EndIf
Func _kuozhanming_xuliehao_huhuan($qian);返回扩展名对应的序列号互换结果
Local $zhuanhuanhou = ""
Switch $qian
Case "rmvb"
$zhuanhuanhou = "1"
Case "mp4"
$zhuanhuanhou = "2"
Case "avi"
$zhuanhuanhou = "3"
Case "wmv"
$zhuanhuanhou = "4"
Case "1"
$zhuanhuanhou = "rmvb"
Case "2"
$zhuanhuanhou = "mp4"
Case "3"
$zhuanhuanhou = "avi"
Case "4"
$zhuanhuanhou = "wmv"
EndSwitch
If $zhuanhuanhou <> "" Then
Return $zhuanhuanhou
Else
Return ""
EndIf
EndFunc ;==>_kuozhanming_xuliehao_huhuan
Func _TiQuKuoZhanMing($str)
$Test = StringRegExp($str, '\.([^\.]*)$', 3)
Return $Test
EndFunc ;==>_TiQuKuoZhanMing
Func _ExplorerDir($Dir, $Parameter = 0)
Switch $Parameter
Case 0
ShellExecuteWait("Explorer", "/n, " & $Dir)
Case 1
ShellExecuteWait("Explorer", "/e, " & $Dir)
Case 2
ShellExecuteWait("Explorer", "/root, " & $Dir)
Case 3
ShellExecuteWait("Explorer", "/select, " & $Dir)
Case Else
MsgBox(262160, "提示", " 参数错误!")
EndSwitch
EndFunc ;==>_ExplorerDir
回复 1# gzh888666
#include <Array.au3>
#include <File.au3>
Local $aFiles = ['au3.au3.rmvb', 'au3.au3.mp4', 'au3.au3.avi', 'au3.au3.wmv']
Local $aFiles_B = $aFiles
Local $sMsg1, $sMsg2
For $i = 0 To UBound($aFiles)-1
$aFiles_B[$i] = _rename_file($aFiles[$i])
Next
$sMsg1 = _ArrayToString($aFiles, @TAB) & @CRLF & _ArrayToString($aFiles_B, @TAB)
For $i = 0 To UBound($aFiles_B)-1
$aFiles[$i] = _rename_file($aFiles_B[$i])
Next
$sMsg2 = _ArrayToString($aFiles_B, @TAB) & @CRLF & _ArrayToString($aFiles, @TAB)
MsgBox(0, '', '"备份":' & @CRLF & $sMsg1 & @CRLF & @CRLF & '"恢复":' & @CRLF & $sMsg2)
Func _rename_file($sFile)
Local $suffix, $Code, $sNewN
If StringRegExp($sFile, '(?i)\.rar$') Then
$suffix = _str_convert(StringLeft($sFile, 1))
$sNewN =StringRegExpReplace($sFile, '^(.+\.).+$', '\1') & $suffix
Else
$suffix = 'rar'
$Code = _str_convert(_get_postfix($sFile))
$sNewN = $Code & StringRegExpReplace(StringTrimLeft($sFile,1), '^(.+\.).+$', '\1') & $suffix
EndIf
Return $sNewN
EndFunc
Func _get_postfix($sFile)
$aRe = StringRegExp($sFile, '\.([^\.]+)$', 3)
If Not @error Then Return $aRe
EndFunc
Func _str_convert($str);返回扩展名对应的序列号互换结果
Local $sRe = ""
Switch $str
Case "rmvb"
$sRe = "1"
Case "mp4"
$sRe = "2"
Case "avi"
$sRe = "3"
Case "wmv"
$sRe = "4"
Case "1"
$sRe = "rmvb"
Case "2"
$sRe = "mp4"
Case "3"
$sRe = "avi"
Case "4"
$sRe = "wmv"
Case Else
Return SetError(1)
EndSwitch
Return $sRe
EndFunc
老兄,对你的变量及函数取名方式实在不敢认同.
太长了,很不好记也不好认!!! 回复 3# user3000
我也不怎么认同,无奈英语都还给老师了。现在做的工作也用不到,唉!年龄大了以后就没有上进心了。
感谢你的代码,学习.........
页:
[1]