如何获取相对于工作目录的路径? [英] How to get path relative to the working directory?

查看:66
本文介绍了如何获取相对于工作目录的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本以递归方式遍历工作目录和子目录中的所有 .txt 文件,并对这些文件执行某些操作.现在,我想排除 exclude.txt 文件中列出的某些子目录中的所有文件:

I have a script that recursively loops through all .txt files in the working directory and subdirectories and does something with that files. Now I would like to exclude all files in certain subdirectories that are listed in a exclude.txt file:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

for /r %%f in (*.txt) do (
    CALL:processFile %%f %%~df%%~pf
)
GOTO:EOF

:processFile
    SET file_=%~1
    SET path_=%~2                 <- %%~df%%~pf is the full path :(
    find "!path_!" exclude.txt
    IF !ERRORLEVEL! EQU 1 (
        REM do something here
    )
    GOTO:EOF

但是, %%/〜df %%〜pf 会扩展到绝对路径.如何获取相对于工作目录的路径?我只想列出 exclude.txt 中的子目录,而不是完整路径.

However, %%/~df%%~pf expands to the absolute path. How can I get the path relative to the workingdirectory? I want to list only the subdirectories in exclude.txt and not the full paths.

PS:我当然可以从 exclude.txt 中读取相对路径,附加%cd%并将它们写入某些 exclude.temp 然后搜索此临时文件,但我希望有更好的方法.

PS: I could of course read the relative paths from exclude.txt, append %cd% and write them to some exclude.temp and then search in this temporary file, but I hope there is a nicer way.

推荐答案

尝试一下.

@ECHO OFF
SETLOCAL EnableDelayedExpansion

for /r %%F in (*.txt) do (
    echo %%F|findstr /I /G:exclude.txt >nul 2>&1
    IF NOT "!ERRORLEVEL!"=="0" (
        REM do something here
    )
)

这篇关于如何获取相对于工作目录的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆