批处理文件:列出具有相对路径的目录中的所有文件 [英] Batch files: List all files in a directory with relative paths

查看:19
本文介绍了批处理文件:列出具有相对路径的目录中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Windows批处理文件:有没有办法列出某个目录及其子目录中的所有文件(或所有特定类型),包括相对于列表中当前(或搜索)目录的路径?

Concerning Windows batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list?

例如,如果我想要当前目录和子目录中的所有 .txt 文件及其完整路径,我可以这样做

For example, if I want all the .txt files in the current directory and subdirectories with their full paths, I can do

for /r . %%g in (*.txt) do echo %%g >> C:	emp	est.txt

dir *.txt /b /s >> C:	emp	est.txt

我会得到类似的东西

C:	estDoc1.txt
C:	estsubdirDoc2.txt
C:	estsubdirDoc3.txt

如果我这样做

for /r . %%g in (*.txt) do echo %%~nxg >> C:	emp	est.txt

我会得到类似的东西

Doc1.txt
Doc2.txt
Doc3.txt

但我真正想要的是:

Doc1.txt
subdirDoc2.txt
subdirDoc3.txt

有可能吗?

如果我的帖子太混乱:我基本上想要在 Linux CLI 中使用相对于当前目录的路径递归列出文件,但仅适用于 Windows.

If my post is too confusing: I basically want List files recursively in Linux CLI with path relative to the current directory, but just for Windows.

推荐答案

您可以简单地获取当前目录的字符长度,并将它们从您的绝对列表

You could simply get the character length of the current directory, and remove them from your absolute list

setlocal EnableDelayedExpansion
for /L %%n in (1 1 500) do if "!__cd__:~%%n,1!" neq "" set /a "len=%%n+1"
setlocal DisableDelayedExpansion
for /r . %%g in (*.log) do (
  set "absPath=%%g"
  setlocal EnableDelayedExpansion
  set "relPath=!absPath:~%len%!"
  echo(!relPath!
  endlocal
)

这篇关于批处理文件:列出具有相对路径的目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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