批处理脚本反转文本文件的内容更改加载顺序并再次反转

时间:2016-09-19 15:30:44

标签: batch-file cmd

set startpath=%cd%
del mods.txt
del mod.txt
del prioritys.txt
del desiredmodname.txt
type NUL > prioritys.txt
cls
set /p w3folder=<w3folder.txt
if not exist "%w3folder%\content\content0" (goto choosew3folder) else goto       askmodkitfolder
:choosew3folder
OpenFolderBox.exe C:\ "Appears only on first start or path edit - Please choose your The Witcher 3 Wild Hunt Directory" > w3folder.txt
set /p w3folder=<w3folder.txt
:askmodkitfolder
set /p modkitfolder=<modkitfolder.txt
if not exist "%modkitfolder%\bin\config\base" (goto choosemodkitfolder) else goto askbasefolder
:choosemodkitfolder
OpenFolderBox.exe C:\ "Appears only on first start or path edit - Please choose your Official ModKit Directory" > modkitfolder.txt
set /p modkitfolder=<modkitfolder.txt
:askbasefolder
set /p w3basefolder=<w3basefolder.txt
if not exist "%w3basefolder%\gameplay\community" (goto choosebasefolder) else goto :selectmods
:choosebasefolder
MessageBox.exe "In the next Window you have to select your uncooked base game folder - click on cancel if u dont have it or if u dont know what im talking about :D" "First start/Changed base folder" "OK" "Information" "Button1" "None" "30"
OpenFolderBox.exe C:\ "choose game base folder-cancel to uncook base game" > w3basefolder.txt
set /p w3basefolder=<w3basefolder.txt
Call notEmpty.bat w3basefolder.txt
If %errorlevel% EQU 0 (goto selectmods) Else (goto uncookbase)
:uncookbase
MKDIR "%startpath%\base"
cd "%w3folder%\xTcTools\bin\x64\"
call wcc_lite.exe uncook -indir="%w3folder%\content" -outdir="%startpath%\base"
call wcc_lite.exe uncook -indir="%w3folder%\DLC" -outdir="%startpath%\base"
set w3basefolder="%startpath%\base"
:selectmods
cd "%startpath%"
del mod.txt
del modname.txt
type NUL > mod.txt
type NUL > modname.txt
call OpenFolderBox.exe "%w3folder%\mods" "Choose next mod - press cancel to uncook selected mods" >> mod.txt
Call notEmpty.bat mod.txt
If %errorlevel% EQU 0 (goto findrepl) Else (goto uncookmods)
:findrepl
set /p mod=< mod.txt
call modname.bat "%mod%"
cd "%startpath%"
echo %modname% >> prioritys.txt
timeout 1
type "mod.txt"|findrepl /i /o:1:1 >>"mods.txt"
goto selectmods
:uncookmods
set /p uncookmod=< mods.txt
call modname.bat "%uncookmod%"
cd "%modkitfolder%\bin\x64\"
call wcc_lite.exe uncook -indir="%uncookmod%" -outdir="%startpath%\mods\%modname%"
if not exist "%startpath%\%modname%" (goto quickbms) else goto askmod
:quickbms
cd "%startpath%"
call quickbms.exe -d -o -Y "%startpath%\witcher3.bms" "%uncookmod%\content\blob0.bundle" "%startpath%\%modname%"
call quickbms.exe -d -o -Y "%startpath%\witcher3.bms" "%uncookmod%\content\blob1.bundle" "%startpath%\%modname%"
call quickbms.exe -d -o -Y "%startpath%\witcher3.bms" "%uncookmod%\content\blob2.bundle" "%startpath%\%modname%"
call quickbms.exe -d -o -Y "%startpath%\witcher3.bms" "%uncookmod%\content\buffers0.bundle" "%startpath%\%modname%"
call quickbms.exe -d -o -Y "%startpath%\witcher3.bms" "%uncookmod%\content\buffers1.bundle" "%startpath%\%modname%"
call quickbms.exe -d -o -Y "%startpath%\witcher3.bms" "%uncookmod%\content\buffers2.bundle" "%startpath%\%modname%"
Robocopy.exe /MOVE /S /E "%startpath%\%modname%\blob0.bundle" "%startpath%\mods\%modname%"
Robocopy.exe /MOVE /S /E "%startpath%\%modname%\blob1.bundle" "%startpath%\mods\%modname%"
Robocopy.exe /MOVE /S /E "%startpath%\%modname%\blob2.bundle" "%startpath%\mods\%modname%"
Robocopy.exe /MOVE /S /E "%startpath%\%modname%\buffers0.bundle" "%startpath%\mods\%modname%"
Robocopy.exe /MOVE /S /E "%startpath%\%modname%\buffers1.bundle" "%startpath%\mods\%modname%"
Robocopy.exe /MOVE /S /E "%startpath%\%modname%\buffers2.bundle" "%startpath%\mods\%modname%"
rd /s /q "%startpath%\%modname%"
:askmod
MKDIR "%w3folder%\mods\00_MergedMods"
move "%uncookmod%" "%w3folder%\mods\00_MergedMods"
cd "%startpath%"
timeout 1
type "mods.txt"|findrepl /v /o:1:1 >"mods.txt"
Call notEmpty.bat mods.txt
If %errorlevel% EQU 0 (goto uncookmods) Else (goto editfiles)
:editfiles
DROPDOWNBOX.exe "Edit Load Order;Edit Files;Build Mod" "What you like to do?" "Option" /RI /C:23 >NUL
SET ReturnCode=%ErrorLevel%
IF "%ReturnCode%"=="0" goto editfiles
IF "%ReturnCode%"=="1" cmd /c "prioritys.txt"
IF "%ReturnCode%"=="2" %SystemRoot%\explorer.exe "%startpath%\mods"
IF "%ReturnCode%"=="3" goto robocopy
goto editfiles
:robocopy
set /p modtocopy=< prioritys.txt
echo %modtocopy% >> IncludedMods.txt
Robocopy.exe /MOVE /S /E "%startpath%\mods\%modtocopy%" "%startpath%\uncook"
timeout 1
type "prioritys.txt"|findrepl /v /o:1:1 >"prioritys.txt"
Call notEmpty.bat prioritys.txt
If %errorlevel% EQU 0 (goto robocopy) Else (goto build)
:build
rd /s /q "%startpath%\mods"
InputBox.exe "Enter Name to start building Mod" "Enter Name and Build Mod" "mod" > desiredmodname.txt
set /p desiredmodname=< desiredmodname.txt
MKDIR "%startpath%\%desiredmodname%\content"
MKDIR "%startpath%\cooked"
cd "%modkitfolder%\bin\x64\"
call wcc_lite.exe cook -platform=pc -mod="%startpath%\uncook" -basedir="D:\Spiele\Witcher3Mods\Base" -outdir="%startpath%\cooked"
call wcc_lite.exe buildcache textures -basedir="%startpath%\uncook" -platform=pc -db="%startpath%\cooked\cook.db" -out="%startpath%\%desiredmodname%\content\texture.cache"
call wcc_lite.exe pack -dir="%startpath%\cooked" -outdir="%startpath%\%desiredmodname%\content"
call wcc_lite.exe metadatastore -path="%startpath%\%desiredmodname%\content"
cd "%startpath%"
rd /s /q "%startpath%\cooked"
rd /s /q "%startpath%\uncook"
move "IncludedMods.txt" "%startpath%\%desiredmodname%"
del mods.txt
del mod.txt
del modname.txt
del prioritys.txt
del desiredmodname.txt
move "%startpath%\%desiredmodname%" "%w3folder%\mods"
MessageBox.exe "All Done enjoy Witcher 3 Wild Hunt now!" "Mod Build Success" "OK" "Information" "Button1" "None" "10"
exit

所以这是我的代码。一切正常。现在在editlines部分,我可以选择编辑如下所示的加载顺序文件:

modWEATHER  
modInventoryBackground  
modTest1
modTest2
modTest3
modTest4
modTest5

有没有办法批量转换这个文件(因为我希望优先级正常,如1,2,3,4,5,6,7等,但后来脚本需要反向,因为进程本身需要它后来反转,因为最后加载的文件可以替换第一个加载的文件。(坏英语抱歉)

那么有没有办法扭转这个loadorder.txt文件给用户让他改变一些东西并在之后再次反转?

并且在第84行中,我将此loadorder文件传递给用户,但是用他的标准文本编辑器打开了它。是否有一个小工具让用户更改此文本文件的加载顺序? 我希望你明白。

3 个答案:

答案 0 :(得分:1)

另一种基于sort命令的方法:

@echo off
setlocal

set "n=2000000"
for /F "usebackq tokens=1*" %%a in (`
   (echo off ^& for /F "usebackq delims=" %%c in ("%~1"^) do (
   set /A "n-=1" ^& echo %%n%% %%c^)^) ^| sort`) do echo %%b

以下修改允许包含空行:

@echo off
setlocal

set "n=2000000"
for /F "usebackq tokens=1* delims=:" %%a in (`
   (echo off ^& for /F "delims=" %%c in ('findstr /N "^" "%~1"'^) do (
   set /A "n-=1" ^& echo %%n%% %%c^)^) ^| sort`) do echo(%%b

要生成输出文件,只需将整个 for /F命令括在括号中,然后将输出重定向到文件。之后,您可以使用输出替换原始文件:

(for /F "usebackq tokens=1*" %%a in (`... sort`) do echo %%b) > output.txt
move /Y output.txt "%~1"

答案 1 :(得分:0)

我写了一个批处理文件来反转文件。 它使用递归和其他一些丑陋的技巧,但似乎有效:

文件:Reverse.bat

@echo off 
set LineCount=0
for /F %%a in (%1) do (set /A LineCount+=1)
call :ReverseFile %1
exit /B

:ReverseFile [%1|Filename] [%2|LineCount]
    SetLocal
    set /A LineCount-=1
    if %LineCount%==-1 exit /B
    set SKIP_CMD="skip=%LineCount%"
    if %LineCount%==0 set SKIP_CMD=
    for /F %SKIP_CMD% %%a in (%1) do (set Line=%%a & echo(%%a & call :ReverseFile %1 %LineCount% & exit /B)
exit /B

示例用法

文件:TestData.txt

Four
score
and
seven
years
ago
our
fathers
brought
forth
on
this
continent


C:\BatchFiles> Reverse TestData.txt
continent
this
on
forth
brought
fathers
our
ago
years
seven
and
score
Four

答案 2 :(得分:0)

这是一个以2个文件名作为参数的子程序,将第一个文件反转为第二个文件:

:reverse
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET /a line=9999
FOR  /F "usebackq delims=" %%a In ("%~1") DO SET "$!line!=%%a"&SET /a line-=1
(
 FOR  /F "tokens=1*delims==" %%a In ('set $ 2^>Nul') DO ECHO(%%b
)>"%~2"
GOTO :eof

它的工作原理是将文件内容构建为变量$9999$9998等,然后将它们列入新文件。至关重要的是,line的初始设置足够大,从初始设置中减去文件中的行数不会改变值中的位数(此处为4,因此可以使用理论上最多8,999行,因为9999-8999 = 1000;另外1行,它是999,这是3位数)

如果需要,可以设置为单独的批次。

毫无疑问,可以调用

Notepad来编辑文件。

notepad "newfilename"

将调用记事本并等待用户保存所有更改,然后再继续。