OK, eu configurei o mesmo conjunto de arquivos de teste:
X:\>dir /s /b
X:\work
X:\work\src
X:\work\src\A
X:\work\src\B
X:\work\src\A\D1
X:\work\src\A\D2
X:\work\src\A\D1\foo.txt
X:\work\src\A\D2\foo.txt
X:\work\src\B\foo.txt
Então, eu tenho um arquivo de lote. Digamos que ele seja salvo como: "D:\batchscript.cmd"
:
@echo off
rem get the "unique" part of the source and dest paths: "\src\", "\dst\"
set "zusource=\src\"
set "zudest=\dst\"
rem From within the current directory, and all subdirectories,
rem process each file named "foo.txt"
for /f "usebackq delims=" %%f in ('dir /s /b "foo.txt"') do call :work "%%~f"
rem We are done. Go "exit" the script.
goto :EOF
:work
rem get the full name of the current "source" file: "X:\work\src\A\D1\foo.txt"
set "zsourcefq=%~1"
rem substitute the "unique" parts:
rem get "X:\work\dst\A\D1\foo.txt" from "X:\work\src\A\D1\foo.txt"
for /f "usebackq delims=" %%g in ('echo set "zdestfq=%%zsourcefq:%zusource%=%zudest%%%"') do %%g
rem make the destination directory if necessaary: "X:\work\dst\A\D1"
rem two ways, first relies on your specific file "name", second is "universal"
rem md "%zdestfq:foo.txt=%" 2>nul
for %%g in ("%zdestfq%") do md "%%~dpg" 2>nul
echo Processing: "%zsourcefq%" --^> "%zdestfq%"
rem do your "sed" command here
echo sed -r "s/whatever/whateverelse/g" "%zsourcefq%" ^> "%zdestfq%"
goto :EOF
Para usar o scirpt, primeiro mude para o diretório "source":
X:\>CD /D "X:\work\src"
X:\work\src>
Em seguida, execute o script:
X:\work\src>"D:\batchscript.cmd"
Processing: "X:\work\src\A\D1\foo.txt" --> "X:\work\dst\A\D1\foo.txt"
sed -r "s/whatever/whateverelse/g" "X:\work\src\A\D1\foo.txt" > "X:\work\dst\A\D1\foo.txt"
Processing: "X:\work\src\A\D2\foo.txt" --> "X:\work\dst\A\D2\foo.txt"
sed -r "s/whatever/whateverelse/g" "X:\work\src\A\D2\foo.txt" > "X:\work\dst\A\D2\foo.txt"
Processing: "X:\work\src\B\foo.txt" --> "X:\work\dst\B\foo.txt"
sed -r "s/whatever/whateverelse/g" "X:\work\src\B\foo.txt" > "X:\work\dst\B\foo.txt"
Altere a linha que começa: "echo sed -r..."
para qualquer comando sed que você precisar.