窗口批处理文件 - 从文件路径中删除目录

mic*_*ael 0 windows batch-file

我正在尝试在我的代码库中搜索所有jscript目录,然后使用以下批处理脚本获取这些目录中的相关文件列表...

@echo off

setlocal enableextensions enabledelayedexpansion

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S jscripts') DO (
    CD %%G
    CD dev

    SET "currentDir=!cd!"
    IF NOT "!currentDir:~-1!"=="\" SET "currentDir=!currentDir!\"

    FOR /r %%F IN (*.js) DO (
        SET "relativePath=%%F"
        SET "relativePath=!relativePath:%currentDir%=!"

        ECHO !relativePath!
    )
)
Run Code Online (Sandbox Code Playgroud)

一切都按预期进行,直到...

SET "relativePath=!relativePath:%currentDir%=!"
Run Code Online (Sandbox Code Playgroud)

我可以弄清楚我需要用什么格式来写这个......

c:\dir\jscript\dev\file.js
Run Code Online (Sandbox Code Playgroud)

进入...

file.js
Run Code Online (Sandbox Code Playgroud)

请帮忙!


附加信息

目录设置如下

dir
    jscripts
        dev
            file.js
            file2.js
        live
            file.js
            file2.js


dir2
    jscripts
        dev
            file.js
            file2.js
        live
            file.js
            file2.js
Run Code Online (Sandbox Code Playgroud)

我想找到所有jscripts目录,CD 进入它们,获取相对于 dev目录的所有 JS 文件的列表

Ste*_*ler 5

要从包含完整路径的变量中提取文件名和扩展名,请使用%~nx0如下所示

set G=c:\dir\jscript\file.js
echo %~nxG
Run Code Online (Sandbox Code Playgroud)

引用此答案对类似问题的以下内容

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file

The modifiers can be combined to get compound results:
%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
Run Code Online (Sandbox Code Playgroud)

这是“for /?”的复制粘贴 提示符上的命令。希望能帮助到你。

有关的

十大 DOS 批处理技巧(是的,DOS 批处理...)显示了 batchparams.bat(链接到源作为要点):

C:\Temp>batchparams.bat c:\windows\notepad.exe
%~1     =      c:\windows\notepad.exe
%~f1     =      c:\WINDOWS\NOTEPAD.EXE
%~d1     =      c:
%~p1     =      \WINDOWS\
%~n1     =      NOTEPAD
%~x1     =      .EXE
%~s1     =      c:\WINDOWS\NOTEPAD.EXE
%~a1     =      --a------
%~t1     =      08/25/2005 01:50 AM
%~z1     =      17920
%~$PATHATH:1     =
%~dp1     =      c:\WINDOWS\
%~nx1     =      NOTEPAD.EXE
%~dp$PATH:1     =      c:\WINDOWS\
%~ftza1     =      --a------ 08/25/2005 01:50 AM 17920 c:\WINDOWS\NOTEPAD.EXE
Run Code Online (Sandbox Code Playgroud)