[英]Windows batch file: converting all files in subfolders
我需要编写一个批处理文件以转换每个子文件夹中的每个文件。 我想到了以下解决方案:
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
)
)
问题是仅迭代第一级子文件夹。 当我在dir命令中添加/ s键时,它将返回完整路径,而不是相对路径。
如何改进脚本以处理所有级别的子文件夹?
PS:我需要相对路径 , 文件名和扩展 名的单独值。
您没有向我们显示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
)
)
从提示中查看for /?|more
,以了解更多...
(在子例程中,如果需要,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
这将创建一个辅助驱动器,因此驱动器的根指向in文件夹。 然后遍历文件夹结构,在输出文件夹中重新创建结构,并使用指示的参数调用子例程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.