我正在尝试对目录中的所有文件(及其子目录)运行命令,但我很难为我找到的文件生成相对路径。
我的代码是......
@echo off
set currentDir=%~dp0
for /r %%f in (*.js) do (
echo %currentDir%
echo %%f
set relativePath=%%%f:%currentDir%=%
echo %relativePath%
)
基本上如果我在 C:\ somedir 中运行脚本并找到 C:\ somedir \ anotherdir \ file.js ,我需要%relativePath%才能包含 anotherdir \ file.js
请帮忙!
答案 0 :(得分:2)
已编辑 - 以适应所需的输出。
@echo off
setlocal enableextensions enabledelayedexpansion
rem to take batch file directory as current directory
set "currentDir=%~dp0"
rem OR to take current directory as current directory
set "currentDir=%cd%"
rem Select only one of the above
rem add trailing backslash if not present
if not "%currentDir:~-1%"=="\" set "currentDir=%currentDir%\"
rem Do recurse directory
for /r %%f in (*.js) do (
set "relativePath=%%f"
set "relativePath=!relativepath:%currentDir%=!"
echo %relativePath%
)