-2

How can I batch process files with identical names, minus a single letter difference (c or t)?

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

Using sox, this code lowers volume on one microphone file then merges both into a new file:

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

I need to output to a subfolder using the original file names, or worse case just overwrite the original c files.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    What have you tried? This is not a code request site. You need to help us, help you. – Gerhard Jan 30 '19 at 07:58
  • 1
    honestly, I have no idea, what you're talking about. Your example even doesn't match your file names. – Stephan Jan 30 '19 at 09:15
  • 1
    What do you want the name of the subfolder to be? Edit the question and give an example. Would `BML201_solohorn_longs_legato_ff_RR1_G4` be OK? What do you want the name of the output file to be? Would `BML201_solohorn_longs_legato_ff_RR1_G4\BML201_solohorn_longs_legato_ff_RR1_G4.wav` be OK? – AlexP Jan 30 '19 at 09:35
  • @Stephan I apologize. Hopefully this might help clarify: `sox -v 0.43 *_c_*.wav -m *_t_*.wav output.wav` I have thousands of files in one folder. Half have `_c_` and the other half have `_t_`. I'm trying to merge what are otherwise identical file names into a "merged" subdirectory. If it helps to understand, the `_t_` and `_c_` represent different microphones from the same recording session. I'm lowering the volume of the "tree" files then merging (or mixing) with the "close" files. This is for a sampled instrument, a virtual french horn. I'm sorry if I'm still being too vague. – film composer Jan 30 '19 at 14:12

3 Answers3

1

@AlexP @Stephan

It's working beautifully! :)

For whatever reason it wasn't working with the custom file name out. I didn't want to remove the delimiter anyway (long story why), so I just used the same file name and now it works. Here's the code if anyone else could use it.

@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

Sorry to be that annoying pest. I usually hate when people don't help themselves in another language I semi-know. So I get it. I just had no idea where to start and reading up on Batch was murdering my brain. Anyway, thanks for the help to all and thanks to AlexP for the solution! :) -Sean

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
AlexP
  • 4,370
  • 15
  • 15
  • Thank you for helping. All files can go into the same subfolder called "merged". Both files will always be found. I tried editing your code but nothing happens when I run it. I know one obscure scripting language so I apologize, this is very raw to me. I am trying to wrap my head around it, compare to other code, and edit at least. I'm just clueless. I'll post my attempt in another answer. – film composer Jan 30 '19 at 13:56
0

This is my edit of AlexP's code. It doesn't do anything to the files (likely my fault). Am I correct in using "merged\" if I just want to put all files in the same subdirectory at least?

@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

Sorry for the lack of clarity in the original question. I replied to the first answer with some clarifying remarks. I hope this helps. I'm happy to clarify more if needed. -Sean

AlexP
  • 4,370
  • 15
  • 15
  • `Am I correct in using "merged\" if I just want to put all files in the same subdirectory at least?` depends whether `sox` accepts it - just try. PS: this seems to be an additional question, not an answer and is likely to be deleted. Please include the code and additional question into the question itself (making it actually on-topic) – Stephan Jan 30 '19 at 14:39
  • You edited too much. I have edited your answer. If you run this code in the directory where the original files are you should see at least `- Looking at BML201_solohorn_longs_legato_ff_RR1_c_G4.wav`, `Found BML201_solohorn_longs_legato_ff_RR1_t_G4.wav` and whatever messages `sox` displays. – AlexP Jan 30 '19 at 15:01