I have a bat file to process all file under a directory, and output to another directory
this is the codes in the bat file
@set dir1=mulit-simp\
@set dir2=mulit-trad\
@cd /d %~dp0
@if not exist %dir1% md %dir1%
@if not exist %dir2% md %dir2%
@for /f "delims=" %%i in ('dir %dir1%*.* /b') do @opencc.exe --input="%dir1%%%i" --output="%dir2%%%i"
@echo.
@echo Done!
@echo.
@pause
but this code can’t process files in the sub directory
How could I process the sub directory files, and output them with the same directory structure to another directory?
thx for help 🙂
The
DIR /Soption would give you the entire folder hierarchy. And you also want the/A-Doption to suppress folders from the output. TypeHELP DIRorDIR /?for more info.However, instead of using
FOR /FwithDIR, I recommend usingFOR /R. TypeHELP FORorFOR /?for more info. Also see http://judago.webs.com/batchforloops.htm for easier to understand explanation of theFORcommand.Either way, you also need to create all of the destination folders. The easiest way to do that is to use
XCOPY /T /Eto create the destination folders prior to processing the files.Some additional tips:
@echo offat the top instead of prefixing every command with@ECHO.has been widely used for years, but it can actually fail to work properly. Safer to useECHO(instead.SETLOCALat the top will make the variables temporary and they will exist only until the batch file finishes, or untilENDLOCALis executed. Given that you are usingPAUSEat the end, I suspect you are running the batch file by double clicking on the file or a shortcut. If so, then the command window closes at the end, and the variables disappear along with it. ThenSETLOCALis not needed, but it is still probably a good practice to get into anyway.Here is the finished code with all of the suggestions. EDIT – The original code did not work. Altered to address tiance’s comments