0

I play Fallout 4 VR with Mod Organizer 2 (mo2), and most mods require

*Fallout4_VR.esm

at the top of the file plugins.txt, but mo2 keeps removing it.

So I downloaded a batch file which adds that line at the top of the file on execution.

But the problem is that mo2 has this at the top:

# This file was automatically generated by Mod Organizer.
*DLCRobot.esm
*DLCworks...
etc.

This is the code in the batch file:

@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

What do I need to edit so *Fallout4_VR.esm is below the whole line with generated by Mod Organizer instead of top of the file?

The file plugins.txt should be finally:

# This file was automatically generated by Mod Organizer.
*Fallout4_VR.esm
*DLCRobot.esm
*DLCworks...
etc.
Mofi
  • 46,139
  • 17
  • 80
  • 143

1 Answers1

0

The task to add at top the line with *Fallout4_VR.esm to contents of the file plugins.txt below the comment line(s) could be done with the following code:

@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

ATTENTION: The result is only correct if

  • plugins.txt is not a Unicode encoded text file with encoding UTF-16
  • and has DOS/Windows line endings (carriage return + line-feed).

It is advisable to avoid command blocks starting with ( and ending with a matching ) because that makes it possible to do the task without usage of delayed variable expansion and therefore the batch file works also with plugins.txt stored in a directory with a path like C:\Temp\Development & Test(!) 100%. It is of course necessary to use command blocks for the for /F loop processing the lines in the text file to modify.

The outer FOR loop processes the lines in the text file with skipping empty lines and assigning each non-empty line completely to the specified loop variable I. The option usebackq instructs FOR to interpret the string in double quotes as file name of which lines to process and not as string to process. The option delims= defines an empty list of delimiters to prevent splitting the lines up on normal spaces and horizontal tabs. The option eol= defines no character as end of line character. The unusual syntax without " around the three options must be used in this special case which requires escaping the spaces and the equal signs with caret character ^ to get usebackq delims= eol= interpreted as one argument string with the three options for command FOR.

The first IF condition is true as long as the line with *Fallout4_VR.esm is not output. In this case there is first defined the environment variable CommentLine with a value whereby the value itself does not matter.

The inner for /F processes the current line as string with ignoring the line on starting with # after zero or more leading spaces/tabs. So if the current line is a comment line, the environment variable CommentLine is still defined after execution of the inner for /F while this environment variable is deleted on current line is not a comment line.

If the current line is not a comment line, there is output *Fallout4_VR.esm to insert that as line into the temporary file and the environment variable LineInsert is deleted before next is output in any case the current line.

All other lines of the text file are just output after inserting the line with *Fallout4_VR.esm detected by environment variable InsertLine no longer existing.

Everything output during execution of the outer FOR loop is written by cmd.exe into a temporary file in same directory as the text file to update which should work as long as the directory is not write-protected for the user or there is already a directory with the name of the temporary file (very unlikely) or a read-only file with that name (also very unlikely).

The read-only file attribute is removed from the file to update if it would have the read-only file attribute set.

Next the temporary file is moved over the existing file to update which can fail like on write access to file is denied because of the file is currently opened by an application which denies the shared file write access.

The batch file can be started with the option /N to avoid the user prompt with command pause at end of the batch file.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • attrib /?
  • call /? ... for %~dp0 ... drive and path of argument 0 ... batch file path
  • cls /?
  • copy /?
  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143