[英]Batch File modify text file for NSIS installer
As part of an NSIS script builder I have a list of filenames that are currently in a file called files.txt. 作为NSIS脚本生成器的一部分,我有一个文件名列表,这些文件名当前位于一个名为files.txt的文件中。 An example of the content is below... 内容示例如下...
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
I am trying to process this list of files using a batch file so that they end up looking like this... 我正在尝试使用批处理文件来处理此文件列表,以使它们最终看起来像这样……
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"
I have tried a few things but haven't really been able to find a solution. 我已经尝试了一些方法,但实际上并没有找到解决方案。 Can anyone point me in the right direction? 谁能指出我正确的方向?
UPDATE 更新
Ok, so going on from Aacini's post I have modified the batch file slightly as below... 好的,所以从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
I made a couple of changes mainly with quotes to allow spaces to be used in the path names, and I also set the path to allow everything to work correctly when being called from NSIS. 我进行了几处更改,主要是使用引号来允许在路径名中使用空格,并且我还将路径设置为允许从NSIS调用时一切都能正常工作。 The problem I am facing now is that instead of.... 我现在面临的问题是...
SetOutPath "$INSTDIR\assets"
File "c:\my folder\assets\startup.xml"
I get.... 我知道...
SetOutPath "$INSTDIR\c:\my folder\assets"
File "c:\my folder\assets\startup.xml"
I imaging it is a real easy fix to resolve but I am pulling my hair out trying to change it! 我认为这是一个真正容易解决的解决方案,但我正在拔头发试图进行更改! What am I doing wrong? 我究竟做错了什么?
I have slightly modified your batch: 我对您的批次进行了一些修改:
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
NSIS can include a directory tree with File /r "c:\\myfolder\\"
etc but if you really want to parse a text file you can try something like this: 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
This Batch file do what you need: 该批处理文件可以满足您的需求:
@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
First test it and check the result.txt file. 首先对其进行测试,然后检查result.txt文件。 If everything is OK, remove the REM part in the last command. 如果一切正常,请在最后一个命令中删除REM部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.