我有一个脚本,该脚本以递归方式遍历工作目录和子目录中的所有 .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 ) )更多推荐
如何获取相对于工作目录的路径?
发布评论