如何用相对路径替换这个程序中的绝对路径
本帖最后由 laotongbao 于 2009-5-3 14:12 编辑这个脚本有两个急需解决的问题需大家帮助,
1、如何用相对路径替换这个程序中的绝对路径,如脚本中的才“c:\补丁\”,我想把这个脚本跟补丁文件放U盘,不同机子可能盘符不一,希望试用“当前目录\补丁\”这种形式。
2、如果程序安装时更改了目录,即不是在C盘,如何获取到程序安装路径?不然就不能复制补丁到正确位置了
#include <GUIConstants.au3>
#include <GuiConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
If FileExists("C:\Program Files\TopDomain\e-Learning Class V6 2007 Professional\TeacherMain.exe") Then
ProgressCopy("c:\补丁\", "C:\Program Files\TopDomain\e-Learning Class V6 2007 Professional\",1);要复制的文件
ElseIf FileExists("C:\Program Files\TopDomain\e-Learning Class V4 2007 Professional\TeacherMain.exe") Then
ProgressCopy("c:\补丁\", "C:\Program Files\TopDomain\e-Learning Class V4 2007 Professional\",1);要复制的文件
EndIf
Func ProgressCopy($current, $destination, $UseMultiColour=0, $attrib = "-R", $overwrite = 1 ,$Run1 = 0 )
;FirstTimeRun Get original DirSize and set up Gui
If $Run1 = 0 Then
Global $OverallQty, $Overall, $source, $overallpercent, $Progress0Text, $progressbar1, $Progress1Text, $progressbar2, $Progress2Text, $LocalPercent
DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", 0)
If not FileExists ($Destination) then DirCreate ($Destination); This is why it was failing, the dir did not exist
$source = $current
If StringRight($current, 1) = '\' Then $current = StringTrimRight($current, 1)
If StringRight($destination, 1) <> '\' Then $destination = $destination & "\"
$tosearch = $current
$Overall = DirGetSize($tosearch, 1)
$OverallQty = $Overall
Global Const $PrCopyGui = GUICreate("安装极域电子教室补丁包", 420, 100, -1, -1, -1, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
$Progress0Text = GUICtrlCreateLabel("Please Wait", 10, 5, 400, 20, $SS_LEFTNOWORDWRAP)
$progressbar1 = GUICtrlCreateProgress(10, 20, 400, 20)
GUICtrlSetColor(-1, 32250)
$Progress1Text = GUICtrlCreateLabel("", 10, 44, 400, 20, $SS_LEFTNOWORDWRAP)
$progressbar2 = GUICtrlCreateProgress(10, 60, 400, 20, $PBS_SMOOTH)
$Progress2Text = GUICtrlCreateLabel("", 10, 82, 400, 20, $SS_LEFTNOWORDWRAP)
GUISetFont(10, 600)
;$Progress2Text2 = GUICtrlCreateLabel("", 150, 62, 400, 20)
GUICtrlSetColor(-1, 32250); not working with Windows XP Style if not using windows classic style or dllcall above
GUISetState(@SW_SHOW)
GUICtrlSetData($Progress1Text, "Working Directory " & $tosearch)
$Run1 = 1
EndIf
$Size = DirGetSize($current, 3)
$Qty = $Size
Local $search = FileFindFirstFile($current & "\*.*")
While 1
Dim $file = FileFindNextFile($search)
If @error Or StringLen($file) < 1 Then ExitLoop
If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
$Qty -= 1
$LocalPercent = 100 - (($Qty / $Size) * 100)
$OverallQty -= 1
$overallpercent = 100 - (($OverallQty / $Overall) * 100)
GUICtrlSetData($Progress0Text, "Total Progress " & Int($overallpercent) & "% completed")
GUICtrlSetData($progressbar1, $overallpercent)
GUICtrlSetData($progressbar2, $LocalPercent)
GUICtrlSetData($Progress2Text, "正在复制 " & $file)
If $useMultiColour then
GUICtrlSetColor($Progressbar2, _ChangeColour($LocalPercent))
GUICtrlSetColor($Progressbar1, _ChangeColour($OverallPercent))
EndIf
FileCopy($current & "\" & $file, $destination & StringTrimLeft($current, StringLen($source)) & "\" & $file,$overwrite)
FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
EndIf
If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
DirCreate($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file)
FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
GUICtrlSetData($Progress1Text, $current & "\" & $file)
ProgressCopy($current & "\" & $file, $destination, $UseMultiColour, $attrib, $overwrite,1)
EndIf
WEnd
FileClose($search)
;when overall percent = 100 set end gui text, delete gui and reset run1 to 0
If $overallpercent = 100 Then
GUICtrlSetData($Progress0Text, "Total Progress 100% completed")
GUICtrlSetData($progressbar1, 100)
GUICtrlSetData($progressbar2, 100)
GUICtrlSetData($Progress2Text, "补丁安装结束")
Sleep(5000)
GUIDelete($PRCopyGui)
$Run1 = 0
EndIf
EndFunc ;==>ProgressCopy
Func _ChangeColour($start)
$Redness = Int(255 - ($start / 100 * 512))
If $Redness < 0 Then $Redness = 0
$Greeness = Int(($start / 100 * 512) - 257)
If $Greeness < 0 Then $Greeness = 0
$Blueness = Int(255 - ($Redness + $Greeness))
Return ($Redness * 256 * 256) + ($Greeness * 256) + $Blueness
EndFunc ProgressCopy(@ScriptDir,"C:\Program Files\TopDomain\e-Learning Class V6 2007 Professional\",1);
@ScriptDir脚本所在目录 如果用@ScriptDir,会去复制程序自身,我希望复制“@ScriptDir\补丁\”这个目录里面的所有文件 @ScriptDir&"补丁"
前面获取程序的路径,然后加上补丁这个目录 抱歉,补丁二字前面还要加上\,漏掉了
页:
[1]