[英]Windows batch file: converting all files in subfolders
I need to write a batch file to convert every file in every subfolder. 我需要编写一个批处理文件以转换每个子文件夹中的每个文件。 I came up to the following solution:
我想到了以下解决方案:
set INDIR=<some path>
set OUTDIR=<some path>
mkdir "%OUTDIR%"
for /f "tokens=1*delims=." %%f in ('dir %INDIR% /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one . %%f %%g
)
)
for /f "delims==" %%d in ('dir %INDIR% /ad /b') do (
rem %%d is relative path
mkdir %OUTDIR%\%%d
for /f "tokens=1*delims=." %%f in ('dir %INDIR%\%%d /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one %%d %%f %%g
)
)
The problem is that it iterates through the first level subfolders only. 问题是仅迭代第一级子文件夹。 When I add /s key to the dir command it returns full pathes instead of relative.
当我在dir命令中添加/ s键时,它将返回完整路径,而不是相对路径。
How can I improve the script to process all levels subfolders? 如何改进脚本以处理所有级别的子文件夹?
PS: I need separate values for relative path , file name and extension . PS:我需要相对路径 , 文件名和扩展 名的单独值。
You don't show us convert_one
- but here's a few clues... 您没有向我们显示
convert_one
但是有一些提示...
for /f "delims=" %%f in ('dir %INDIR% /b /s /a-d') do (
rem %%~dpnf is file name and path
rem %%~dpf is file absolute path
rem %%~nf is file name-part
rem %%~xf is extension
call convert_one "%%~dpf" "%%nf" %%~xf
)
)
See for /?|more
from the prompt for more... 从提示中查看
for /?|more
,以了解更多...
(within your subroutine, %~n for n=1..9 strips quotes if needed - applying quotes means that "strings containing spaces"
are regarded as a single string. - maybe make your destination directory there...?) (在子例程中,如果需要,n〜= 1..9的%〜n会删除引号-应用引号意味着
"strings containing spaces"
被视为单个字符串。-也许将目标目录放在其中...?)
@echo off
setlocal enableextensions disabledelayedexpansion
set "INDIR=%cd%"
set "OUTDIR=..\out"
subst :: /d >nul 2>&1 & subst :: "%INDIR%"
for /r "::\." %%a in (*) do (
if not exist "%OUTDIR%%%~pa" md "%OUTDIR%%%~pa"
call convert_one ".%%~pa" "%%~na" "%%~xa"
)
subst :: /d
This operates creating a subst drive, so the root of the drive point to the in folder. 这将创建一个辅助驱动器,因此驱动器的根指向in文件夹。 Then iterates over the folder structure recreating the structure in the output folder and calling the subroutine with the indicated parameters
然后遍历文件夹结构,在输出文件夹中重新创建结构,并使用指示的参数调用子例程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.