0

我正在尝试对目录(及其子目录)中的所有文件运行命令,但正在努力为我找到的文件生成相对路径。

我的代码是...

@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

请帮忙!

4

1 回答 1

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%
    )
于 2013-11-06T09:01:19.513 回答