我和Mod组织者2 (mo2)一起玩Fallout 4vr,大多数mods都需要
*Fallout4_VR.esm在文件plugins.txt的顶部,但是mo2一直在删除它。
所以我下载了一个批处理文件,它在执行时将该行添加到文件的顶部。
但问题是,mo2的顶部是这样的:
# This file was automatically generated by Mod Organizer.
*DLCRobot.esm
*DLCworks...
etc.这是批处理文件中的代码:
@echo off
cls
setlocal enabledelayedexpansion
TITLE FO4VR Launch Codes
REM Find plugins.txt
set "file=C:\Modding\MO2\profiles\Default\plugins.txt"
if not exist "%file%" (
echo ERROR - Could not find %file%
echo.
goto FAIL
) else (
findstr /b /l /i /n "*Fallout4_VR.esm" %file%
if !errorlevel! == 0 (
echo VR ESM entry already exists. Good to go.
echo.
) else (
REM needs to add
(echo *Fallout4_VR.esm) >plugins.txt.new
type %file% >>plugins.txt.new
move /y plugins.txt.new %file%
echo VR ESM entry prepended to %file%.
echo.
)
)
echo.
pause我需要编辑什么,以便*Fallout4_VR.esm在整个行下使用generated by Mod Organizer,而不是文件的顶部?
最后,文件plugins.txt应该是:
# This file was automatically generated by Mod Organizer.
*Fallout4_VR.esm
*DLCRobot.esm
*DLCworks...
etc.发布于 2022-05-24 18:06:37
在*Fallout4_VR.esm行的顶部添加到注释行下面的文件plugins.txt的内容的任务可以使用以下代码完成:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
title FO4VR Launch Codes
rem Find plugins.txt
set "PluginsFile=C:\Modding\MO2\profiles\Default\plugins.txt"
rem Use the line below instead of the line above if the file
rem plugins.txt is always in the same directory as the batch file.
rem set "PluginsFile=%~dp0plugins.txt"
if not exist "%PluginsFile%" echo ERROR: Could not find: "%PluginsFile%"& goto EndBatch
%SystemRoot%\System32\findstr.exe /B /I /L /C:"*Fallout4_VR.esm" "%PluginsFile%" >nul
if not errorlevel 1 echo VR ESM entry already exists. Good to go.& goto EndBatch
set "LineInsert=1"
(for /F usebackq^ delims^=^ eol^= %%I in ("%PluginsFile%") do (
if defined LineInsert (
set "CommentLine=1"
for /F "eol=#" %%J in ("%%~I") do set "CommentLine="
if not defined CommentLine (
echo *Fallout4_VR.esm
set "LineInsert="
)
echo(%%I
) else echo(%%I
))>"%PluginsFile%.tmp"
if not exist "%PluginsFile%.tmp" echo ERROR: Could not create temporary file: "%PluginsFile%.tmp"& goto EndBatch
%SystemRoot%\System32\attrib.exe -r "%PluginsFile%"
move /Y "%PluginsFile%.tmp" "%PluginsFile%" >nul 2>nul
if not errorlevel 1 echo VR ESM entry added to: "%PluginsFile%"& goto EndBatch
del "%PluginsFile%.tmp"
echo ERROR: Could not update: "%PluginsFile%"
:EndBatch
echo/
if /I not "%~1" == "/N" pause
endlocal注意:结果只有在以下情况下才正确
plugins.txt是而不是,是编码UTF-16的Unicode编码文本文件。建议避免以(开头的命令块,并以匹配的)结尾,因为这样可以在不使用延迟变量展开的情况下完成任务,因此批处理文件也适用于存储在路径为C:\Temp\Development & Test(!) 100%的目录中的plugins.txt。当然,对于处理文本文件中要修改的行的for /F循环,需要使用命令块。
外部FOR循环处理文本文件中的行,跳过空行并将每个非空行完全分配给指定的循环变量I。选项usebackq指示FOR将双引号中的新字符串解释为要处理的行的文件名,而不是要处理的字符串。选项delims=定义了一个空的分隔符列表,以防止在普通空格和水平制表符上分割行。选项eol=不将任何字符定义为行字符的末尾。在这种特殊情况下,必须使用在这三个选项周围没有"的异常语法,这需要转义空格和带有插入字符^的等号,以便将usebackq delims= eol=解释为一个参数字符串,并使用命令 FOR的三个选项。
第一个,如果条件为真,只要没有输出带有*Fallout4_VR.esm的行。在这种情况下,首先使用一个值定义环境变量CommentLine,因此该值本身并不重要。
内部for /F将当前行处理为字符串,在零或多个前导空格/制表符之后,忽略以#开头的行。因此,如果当前行是注释行,则环境变量CommentLine仍然是在执行内部for /F之后定义的,而当前行上删除的该环境变量不是注释行。
如果当前行不是注释行,则将输出*Fallout4_VR.esm插入临时文件中,并在输出当前行之前删除环境变量LineInsert。
文本文件的所有其他行都是在插入环境变量InsertLine检测到的带有InsertLine的行之后输出的。
执行外部 for 循环期间输出的所有内容都由cmd.exe写入与要更新的文本文件位于同一个目录中的临时文件中,只要该目录不为用户所保护,或者已经有一个具有临时文件名的目录(非常不可能)或具有该名称的只读文件(也非常不可能)。
如果要设置只读文件属性,则从文件中删除只读文件属性以进行更新。
接下来,将临时文件移动到现有文件上,以更新可能在写入时失败的文件,因为当前由拒绝共享文件写入访问的应用程序打开该文件。
可以使用选项/N启动批处理文件,以避免在批处理文件末尾使用命令pause的用户提示符。
要了解所使用的命令及其工作方式,请打开一个命令提示符窗口,在那里执行以下命令,并非常仔细地读取为每个命令显示的所有帮助页。
attrib /?call /? ...对于%~dp0 ..。驱动和路径0 ..。批处理文件路径cls /?copy /?del /?echo /?endlocal /?findstr /?for /?goto /?if /?move /?pause /?rem /?set /?setlocal /?title /?另请参阅:
https://stackoverflow.com/questions/72363155
复制相似问题