希望创建一个能够编辑文本文件中的一行(或一个单词)的批处理文件

希望创建一个能够编辑文本文件中的一行(或一个单词)的批处理文件

我有一个文本文件(...\config.txt),包含两行,如下所示:

Preamp: -4 dB
Include: surround.txt

我需要将配置文件中的“surround”改为“headphones”。我确实需要制作 2 个批处理文件。一个用于“headphones”,一个用于“surround”。

有问题的配置文件是一个与 EqualizerAPO 相关的文件,它调用一个单独的文件,其中包含特定收听环境的 EQ 设置。在上面的例子中,是“环绕”。但是,可能还有许多其他设置。保存时,该配置文件会自动触发它在“include”行下调用的 EQ 设置。我需要这个的原因是,如果我需要将收听环境更改为其他环境,我需要浏览到目录,打开 config.txt 文件,根据需要编辑“Include”部分,然后保存。当我必须在需要时更改它时,这会变得很乏味。我真的需要一个一键式解决方案。任何帮助都将不胜感激。谢谢!

编辑:

目录(包含前面提到的“config.txt”文件):

C:\Program Files\EqualizerAPO\config\

包含 2 种类型的文本文件:EQ 设置文件(例如耳机、环绕声、剧院等)。基本上,用户通过名为 Room EQ Wizard 的软件创建并以 TXT 格式导出。我们将它们称为“参考”文件。)另一个是 EqualizerAPO 用于确定需要使用哪个“参考”文件并相应地调整其 EQ 设置的实际单个配置文件。EqualizerAPO 将自身作为音频处理对象附加到系统效果基础架构。我需要的只是一个简单的解决方案,它允许我将“Include:round.txt”行更改为我当时需要使用的任何其他 EQ 文件。我经常在 EQ 之间切换,因此我来到这里。如果可能的话,我希望看到一个选择列表。

例如:

Select from the following equalizers:

1. Headphones
2. Surround
3. Theater
4. ***
5. ***

Enter the number:_

然后在选择号码后退出。

理想情况下,最好在任务栏中运行某种非常小的应用程序,以便实现一键式解决方案,从而完全消除批处理文件的使用。但是,这不是必需的。目前,批处理文件就足够了。

答案1

看看这是否有帮助。当我需要对 XML 文件执行搜索和替换时,我就遇到了这种情况。只要将字符串括在双引号中,它们就可以包含特殊字符,例如大于和小于(用作脚本重定向,此处忽略)。如果您不喜欢使用变量来定义要修改的文件,则可以将它们变成可以传递给脚本的参数。但是,除非您明确说明“参数 1 是要更改的文件,参数 2 是要查找的字符串,等等……”,否则您必须更改逻辑来移动参数。

希望这对某些人有帮助。需要 Windows XP 或更高版本。

::Find and Replace script allows the user to 
::define a file path, file name and a string 
::to find and replace so as to create a new file.
::
::Original file is backed up with an added extension of *.bak, in case
::the user finds the need to go back to the original.

@echo off
::Use the path from whence the script was executed as
::the Current Working Directory
set CWD=%~dp0

::***BEGIN MODIFY BLOCK***
::The variables below should be modified to the
::files to be changed and the strings to find/replace
::Include trailing backslash in _FilePath
set _FilePath=Path\To\File\
set _FileName=FileNameToModify
::_WrkFile is the file on which the script will make
::modifications.
set _WrkFile=BackupFileName
set OldStr="The string to be found and replaced, enclosed in double-quotes"
set NewStr="The new string to replace the value of OldStr, enclosed in double-quotes"
::***END MODIFY BLOCK***

::Set a variable which is used by the
::search and replace section to let us
::know if the string to be modified was
::found or not.
set _Found=Not found

SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION

if not exist "%_FilePath%%_FileName%" goto :NotFound

::If a backup file exists, delete it
if exist "%_FilePath%%_WrkFile%" (
    echo Deleting "%_FilePath%%_WrkFile%" 
    del "%_FilePath%%_WrkFile%" >nul 2>&1
    )

echo.
echo Backing up "%_FilePath%%_FileName%"...
copy "%_FilePath%%_FileName%" "%_FilePath%%_WrkFile%" /v

::Delete the original file. No worries, we got a backup.
if exist "%_FilePath%%_FileName%" del "%_FilePath%%_FileName%"
echo.
echo Searching for %OldStr% string...
echo.
for /f "usebackq tokens=*" %%a in ("%_FilePath%%_WrkFile%") do (
    set _LineChk=%%a
    if "!_LineChk!"==%OldStr% (
        SET _Found=Found 
        SET NewStr=!NewStr:^"=! 
        echo !NewStr!
        ) else (echo %%a)
        )>>"%_FilePath%%_FileName%" 2>&1

::If we didn't find the string, rename the backup file to the original file name
::Otherwise, delete the _WorkFile as we re-created the original file when the
::string was found and replaced.
if /i "!_Found!"=="Not found" (echo !_Found! && del "%_FilePath%%_FileName%" && ren "%_FilePath%%_WrkFile%" %_FileName%) else (echo !_Found! && del "%_FilePath%%_WrkFile%")
goto :exit

:NotFound
echo.
echo File "%_FilePath%%_FileName%" missing. 
echo Cannot continue...
echo.
:: Pause script for approx. 10 seconds...
PING 127.0.0.1 -n 11 > NUL 2>&1
goto :Exit

:Exit
exit /b

答案2

这应该可以解决问题:

$filepath = "C:\Program Files\EqualizerAPO\config\config.txt";
$equalizer = "";
$equalizers = @("Headphones","Surround","Theater");  # Add all options here.

if( -not (test-path $filepath)) 
{
    Write-Host "File: `"$filepath`" not found";
    exit(1);
}


if($args.count -gt 0)
{
    if($args[0] -ge 0 -and $args[0] -lt $equalizers.length)
    {
        $equalizer = $equalizers[[int]$args[0]];
    }
    elseif($equalizers -contains $args[0])
    {
        $equalizer = $args[0];
    }
    else
    {
        Write-Host "Invalid argument";
        exit(1);
    }
}
else
{
    Write-Host "Select from the following equalizers:";

    $i=0;

    foreach( $option in $equalizers)
    {
        Write-Host "$i. $option";
        $i++;
    }
    $choice = Read-Host "Enter choice:";

    if($choice -ge 0 -and $choice -lt $equalizers.length)
    {
        $equalizer = $equalizers[[int]$choice];
    }
    else
    {
        Write-Host "Invalid choice";
        exit(1);
    }
}

Write-Host "Changing config file to use $equalizer";

(Get-Content $filepath) | %{

    $_ -replace '^Include: .*$', ("Include: $equalizer.txt")

} | Set-Content $filepath

exit(0);

另存为 filename.ps1 并创建一个名为 filename.cmd 的 bat 文件,并将它们都放在 PATH 中的某个位置,这样您就可以通过在 Windows 开始菜单或运行中键入“filename”来运行脚本。

将以下内容粘贴到 bat 文件中:

@echo off
if [%1]==[] goto none
powershell "& '%~dp0\filename.ps1' '%1'"
goto end
:none
powershell "& '%~dp0\filename.ps1'"
:end

PS:如果之前还没有启用 powershell 脚本的运行,请确保启用它:

powershell "set-executionpolicy remotesigned"

相关内容