2012-03-25 54 views
1

作爲NSIS腳本生成器的一部分,我有一個當前位於名爲files.txt的文件中的文件名列表。內容的下面是一個例子...批處理文件修改NSIS安裝程序的文本文件

c:\myfolder\assets\startup.xml 
c:\myfolder\assets\begin.exe 
c:\myfolder\assets\begin.swf 
c:\myfolder\assets\resources\assets\text\help.pdf 
c:\myfolder\assets\resources\assets\text\license.rtf 
c:\myfolder\assets\resources\assets\swf\piano.swf 
c:\myfolder\assets\resources\assets\swf\numbers.swf 
c:\myfolder\assets\resources\section1\resource1\item1.jpg 
c:\myfolder\assets\resources\section1\resource1\item2.jpg 
c:\myfolder\assets\resources\section4\resource1\item7.jpg 
c:\myfolder\assets\resources\section4\resource1\item8.jpg 

我嘗試過程中使用批處理文件,使他們最終這樣看此文件列表...

SetOutPath "$INSTDIR" 
File "c:\myfolder\assets\startup.xml" 
SetOutPath "$INSTDIR" 
File "c:\myfolder\assets\begin.exe" 
SetOutPath "$INSTDIR" 
File "c:\myfolder\assets\begin.swf" 
SetOutPath "$INSTDIR\resources\assets\text" 
File "c:\myfolder\assets\resources\assets\text\help.pdf" 
SetOutPath "$INSTDIR\resources\assets\text" 
File "c:\myfolder\assets\resources\assets\text\license.rtf" 
SetOutPath "$INSTDIR\resources\assets\swf" 
File "c:\myfolder\assets\resources\assets\swf\piano.swf" 
SetOutPath "$INSTDIR\resources\assets\swf" 
File "c:\myfolder\assets\resources\assets\swf\numbers.swf" 
SetOutPath "$INSTDIR\resources\section1\resource1" 
File "c:\myfolder\assets\resources\section1\resource1\item1.jpg" 
SetOutPath "$INSTDIR\resources\section1\resource1" 
File "c:\myfolder\assets\resources\section1\resource1\item2.jpg" 
SetOutPath "$INSTDIR\resources\section4\resource1" 
File "c:\myfolder\assets\resources\section4\resource1\item7.jpg" 
SetOutPath "$INSTDIR\resources\section4\resource1" 
File "c:\myfolder\assets\resources\section4\resource1\item8.jpg" 

我有嘗試了一些東西,但還沒有真正找到解決方案。任何人都可以將我指向正確的方向嗎?

UPDATE

好了,所以從Aacini的帖子怎麼回事我已經修改爲略低於該批處理文件...

dir "c:\n\assets" /b /s > "c:\n\original.txt" 
type "c:\n\original.txt" | findstr \. > "c:\n\filelist.txt" 
@echo off 
setlocal EnableDelayedExpansion 
SET PATH=C:\n 
set INSTDIR="c:\n\assets\" 
(for /F "delims=" %%a in (filelist.txt) do (
    set "filePath=%%~DPa" 
    set "outPath=!filePath:%INSTDIR%=!" 
    if defined outPath set "outPath=\!outPath:~0,-1!" 
    echo SetOutPath "$INSTDIR!outPath!" 
    echo File "%%a" 
) 
) > "c:\n\result.txt" 
REM move /Y c:\result.txt files.txt 

我做了一些改動,主要是加上引號,可讓空間用於路徑名稱中,並且我還設置路徑以允許從NSIS調用所有內容時正常工作。我現在面臨的問題是,不是....

SetOutPath "$INSTDIR\assets" 
File "c:\my folder\assets\startup.xml" 

我得到....

SetOutPath "$INSTDIR\c:\my folder\assets" 
File "c:\my folder\assets\startup.xml" 

我拍攝它是一個真正簡單的辦法來解決,但我拉我的頭髮試圖改變它!我究竟做錯了什麼?

回答

2

NSIS可以包括目錄樹File /r "c:\myfolder\"等,但如果你真的想分析的文本文件,你可以嘗試這樣的事:

;create a file list for this example 
!delfile "$%temp%\testfilelist.txt" 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\begin.swf$\n 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\resources\assets\text\help.pdf$\n 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\resources\assets\text\license.rtf$\n 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\resources\assets\swf\piano.swf$\n 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\resources\assets\swf\numbers.swf$\n 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\resources\section1\resource1\item1.jpg$\n 
!appendfile "$%temp%\testfilelist.txt" c:\myfolder\assets\resources\section1\resource1\item2.jpg$\n 

outfile "$%temp%\test.exe" 
section 
!define srcroot "c:\myfolder\assets" ;batch file needs to generate a relative path for $instdir 
!define filelist "$%temp%\testfilelist.txt" 
!tempfile BATCH 
!tempfile NSH 
!appendfile "${BATCH}.cmd" '@echo off&setlocal&goto main$\n' 
!appendfile "${BATCH}.cmd" ':StrLen$\n' 
!appendfile "${BATCH}.cmd" 'set #=%~2%&set length=0$\n' 
!appendfile "${BATCH}.cmd" ':stringLengthLoop$\n' 
!appendfile "${BATCH}.cmd" 'if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)$\n' 
!appendfile "${BATCH}.cmd" 'set "%~1=%length%"&GOTO :EOF$\n' 
!appendfile "${BATCH}.cmd" ':writ$\n' 
!appendfile "${BATCH}.cmd" 'setlocal&set "d=%~2"$\n' 
!appendfile "${BATCH}.cmd" 'setlocal enabledelayedexpansion$\n' 
!appendfile "${BATCH}.cmd" 'echo SetOutPath "$INSTDIR!d:~%rlen%!" >>"%~3"$\n' 
!appendfile "${BATCH}.cmd" 'echo File "%~1" >>"%~3"$\n' 
!appendfile "${BATCH}.cmd" 'endlocal&endlocal&GOTO :EOF$\n' 
!appendfile "${BATCH}.cmd" ':main$\n' 
!appendfile "${BATCH}.cmd" 'call :StrLen rlen "%~2"$\n' 
!appendfile "${BATCH}.cmd" 'for /F "usebackq tokens=*" %%A in ("%~1") do call :writ "%%~A" "%%~dpA" "%~3"$\n' 
!system '"${BATCH}.cmd" "${filelist}" "${srcroot}" "${NSH}"' = 0 
!delfile "${BATCH}.cmd" 
!delfile "${BATCH}" 
!undef BATCH 
!include "${NSH}" 
!delfile "${NSH}" 
!undef NSH 
sectionend 
+0

這工程確定爲我的例子,但我想的過程是自動的爲文件列表的變化。我意識到我可以直接從NSIS生成文件列表,但使用文本文件中的文件更容易處理我的情況。 – fightstarr20 2012-03-27 12:25:30

+0

@ fightstarr20:這是不是自動的? (除了srcroot部分) – Anders 2012-03-27 17:58:03

+0

當然,行!appendfile「$%temp%\ testfilelist.txt」c:\ myfolder \ assets \ begin.swf $ \ n將不得不爲每個我需要的新文件組更改加? – fightstarr20 2012-03-27 18:21:43

2

此批處理文件做你的需要:

@echo off 
setlocal EnableDelayedExpansion 
set INSTDIR=c:\myfolder\assets\ 
(for /F "delims=" %%a in (files.txt) do (
    set "filePath=%%~DPa" 
    set "outPath=!filePath:%INSTDIR%=!" 
    if defined outPath set "outPath=\!outPath:~0,-1!" 
    echo SetOutPath "$INSTDIR!outPath!" 
    echo File "%%a" 
) 
) > result.txt 
REM move /Y result.txt files.txt 

首先測試它並檢查result.txt文件。如果一切正常,請刪除最後一條命令中的REM部分。

+0

這個批處理文件對我來說工作正常,如果我直接運行它,但試圖使用Excec從NSIS啓動它,它無法生成result.txt文件 – fightstarr20 2012-03-27 12:19:41

2

我稍微修改您的批處理:

dir "c:\n\assets" /b /s > "c:\n\original.txt" 
FOR /F "tokens=3,4 delims=\" %%G IN (original.txt) DO @echo %%G\%%H| tee -a filelist.txt 
@echo off 
setlocal EnableDelayedExpansion 
SET PATH=C:\n\ 
set INSTDIR=C:\n\assets\ 
(for /F "delims=" %%a in (filelist.txt) do (
    set "filePath=%%~DPa" 
    set "outPath=!filePath:%INSTDIR%=!" 
    if defined outPath set "outPath=\!outPath:~0,-1!" 
    echo SetOutPath "$INSTDIR\%%a" 
    echo File "!Path!%%a" 
) 
) > "c:\n\result.txt" 
REM move /Y c:\result.txt files.txt 
相關問題