如何获取相对于工作目录的路径?

编程入门 行业动态 更新时间:2024-10-27 13:21:11
本文介绍了如何获取相对于工作目录的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个脚本,该脚本以递归方式遍历工作目录和子目录中的所有 .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 ) )

更多推荐

如何获取相对于工作目录的路径?

本文发布于:2023-05-30 09:21:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/365363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相对于   路径   目录   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!