批处理两个WAV文件,一个字母之间有差异?

时间:2019-01-30 07:56:17

标签: batch-file cmd filenames sox

如何批处理具有相同名称的文件,减去一个字母差异(ct)?

BML201_solohorn_longs_legato_ff_RR1_c_G4.wav
BML201_solohorn_longs_legato_ff_RR1_t_G4.wav
BML201_solohorn_shorts_staccato_ff_RR2_c_C#3.wav
BML201_solohorn_shorts_staccato_ff_RR2_t_C#3.wav

使用sox,此代码可降低一个麦克风文件的音量,然后将两者合并为一个新文件:

sox -v 0.43 tree.wav -m close.wav output.wav

我需要使用原始文件名输出到子文件夹,或者更糟的情况是覆盖原始c文件。

3 个答案:

答案 0 :(得分:1)

@AlexP @Stephan

工作得很漂亮! :)

无论出于何种原因,它都无法使用自定义文件名。无论如何,我都不想删除定界符(长话说为什么),所以我只使用了相同的文件名,现在它可以工作了。这是其他人可以使用的代码。

@echo off
  setlocal enableextensions disabledelayedexpansion
  for %%f in (*_c_*.wav) do call :processOneFile "%%~f"
  exit /b

:processOneFile

  setlocal
  echo;- Looking at "%~nx1"
  cd /d "%~dp1"
  set "fileNameC=%~nx1"

  set "fileNameT=%fileNameC:_c_=_t_%"
  echo;  Found "%fileNameT%"

  mkdir mixed >nul 2>&1

  echo;  Running sox -v 0.43 "%fileNameC%" -m "%fileNameT%" "mixed\%fileNameOut%"
  sox -v 0.43 "%fileNameT%" -m "%fileNameC%" "mixed\%fileNameC%"
  echo;
  exit /b

很抱歉令人讨厌的害虫。我通常讨厌别人不使用我不知道的另一种语言来帮助自己。所以我明白了。我只是不知道从哪里开始,而阅读Batch却在谋杀我的大脑。无论如何,感谢大家的帮助,也感谢AlexP提供的解决方案! :) -Sean

答案 1 :(得分:0)

    @echo off
    setlocal enableextensions disabledelayedexpansion
    rem
    rem Run this script in the directory where the _c_ and _t_ files reside.
    rem
    for %%f in (*_c_*.wav) do call :processOneFile "%%~f"
    exit /b

:processOneFile

    setlocal
    echo;- Looking at "%~nx1"
    cd /d "%~dp1"
    set "fileNameC=%~nx1"
    rem
    rem Now %fileNameC% is the name of the file with _c_. Calculate the name
    rem of the corresponding file with _t_ and check that it actually exists.
    rem
    set "fileNameT=%fileNameC:_c_=_t_%"
    if not exist "%fileNameT%" (
      echo;  Cannot find "%fileNameT%"
      echo;
      exit /b
    )
    echo;  Found "%fileNameT%"
    rem
    rem Calculate the name of the output file and the name of the subdirectory.
    rem This example code makes the name of the output file by replacing
    rem _c_ with _ in the name of %fileNameC%, and makes the name of the
    rem subdirectory by deleting the extension .wav from the name of the output
    rem file. Adjust to suit your needs.
    rem
    set "fileNameOut=%fileNameC:_c_=_%"
    for %%d in ("%fileNameOut%") do set "subDirName=%%~nd"
    rem
    rem Create the subdirectory %subDirName% if it does not exist. If the
    rem subdirectory already contains a file named %fileNameOut% delete it.
    rem
    mkdir "%subDirName%" >nul 2>nul
    del "%subDirName%\%fileNameOut%" >nul 2>&1
    rem
    rem Do something with "%fileNameC%" and "%fileNameT%" producing an output
    rem file %subDirName%\%fileNameOut%.
    rem I don't know what you want to do with them, maybe
    rem sox -v 0.43 "%fileNameC%" -m "%fileNameT%" "%subDirName%\%fileNameOut%"
    rem
    rem Insert your command here.
    rem
    echo;
    rem
    rem All done with these two files, return to the main loop to process the
    rem next two.
    rem
    exit /b

答案 2 :(得分:0)

这是我对AlexP代码的编辑。它对文件没有任何作用(可能是我的错)。如果我只想将所有文件至少放在同一个子目录中,我使用“ merged \”是否正确?

@echo off
  setlocal enableextensions disabledelayedexpansion
  for %%f in (*_c_*.wav) do call :processOneFile "%%~f"
  exit /b

:processOneFile

  setlocal
  echo;- Looking at "%~nx1"
  cd /d "%~dp1"
  set "fileNameC=%~nx1"

  set "fileNameT=%fileNameC:_c_=_t_%"
  echo;  Found "%fileNameT%"

  mkdir mixed >nul 2>&1

  set "fileNameOut=%fileNameC:_c_=_%"
  echo;  Running sox -v 0.43 "%fileNameC%" -m "%fileNameT%" "mixed\%fileNameOut%"
  sox -v 0.43 "%fileNameC%" -m "%fileNameT%" "mixed\%fileNameOut%"
  echo;
  exit /b

很抱歉,原始问题不够清楚。我回复了第一个答案,并做了一些澄清的说明。我希望这有帮助。如果需要,我很高兴澄清更多。 -Sean