简体   繁体   English

批处理文件 - 使用 dir /s /b %~dp0 获取文件列表并从 output 中删除 %~dp0 的路径

[英]batch file - use dir /s /b %~dp0 to get list of files and remove %~dp0's path from output

ECHO ===FILES TO TRANSFER===
FOR /f "usebackq tokens=*" %%G IN (`DIR /B /S "%~dp0Files"`) DO @ECHO %%G

The output is the full path of the file/dir but I want to make it simpler by removing %~dp0's path from the output output 是文件/目录的完整路径,但我想通过从 output 中删除 %~dp0 的路径来使其更简单

just remove %~dp0 from each entry (Note: that doesn't work with %%G metavariables, you have to use a "normal" environment variable):只需从每个条目中删除%~dp0 (注意:这不适用于%%G元变量,您必须使用“正常”环境变量):

@echo off
setlocal enabledelayedexpansion
ECHO ===FILES TO TRANSFER===
FOR /f "usebackq tokens=*" %%G IN (`DIR /B /S "%~dp0"`) DO (
  set "file=%%G"
  echo !file:%~dp0=!
)

This is the methodology I'd suggest you incorporate, which protects filenames which may include !这是我建议您合并的方法,它可以保护可能包含的文件名! characters and limits the output to files, as per your stated requirement :字符并将 output 限制为文件,根据您的要求

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^ EOL^= %%G In ('Dir /B/S/A-D "%~dp0Files" 2^>NUL')Do (
    Set "_=%%G" & SetLocal EnableDelayedExpansion
    Echo(!_:*%~dp0Files=.! & EndLocal)
Pause

If you'd prefer not to have a relative path type output then change :_.*%~dp0Files=.!如果您不想使用相对路径类型 output 则更改:_.*%~dp0Files=.! to :_:*%~dp0Files\=!:_:*%~dp0Files\=!


Alternatively, you could grab the relative paths using the slower, forfiles.exe utility:或者,您可以使用较慢的forfiles.exe实用程序获取相对路径:

%__AppDir__%forfiles.exe /P "%~dp0Files" /S /C "%__AppDir__%cmd.exe /D/Q/C If @IsDir==FALSE Echo @RelPath"

If you prefer it without doublequotes then this modification should do that:如果您更喜欢没有双引号的情况,那么此修改应该这样做:

%__AppDir__%forfiles.exe /P "%~dp0Files" /S /C "%__AppDir__%cmd.exe /D/Q/C If @IsDir==FALSE For %%G In (@RelPath)Do Echo %%~G"

And if you wanted it without the leading .\ then perhaps:如果你想要它没有前导.\那么也许:

%__AppDir__%forfiles.exe /P "%~dp0Files" /S /C "%__AppDir__%cmd.exe /D/Q/C If @IsDir==FALSE For /F 0x22Tokens=1*Delims=\0x22 %%G In (@RelPath)Do Echo %%H"

You could also do this by leveraging powershell.exe :您也可以通过利用powershell.exe来做到这一点:

%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile Get-ChildItem -Path "%~dp0Files" -File -Force -Name -Recurse

which could possibly be done, (not recommended) , in as short a line as:这可以做到, (不推荐) ,在短短的一行:

powershell -NoP ls "%~dp0Files" -File -Fo -Na -Rec

To retrieve the relative path to a given root without string manipulation , you could use the xcopy command with its /L option, which lists relative paths to files it would copy without /L :要在不进行字符串操作的情况下检索给定根目录的相对路径,您可以使用带有/L选项的xcopy命令,该命令列出了在不使用/L的情况下将复制的文件的相对路径:

pushd "%~dp0" && (
    for /F "delims= eol=|" %%G in ('xcopy /L /S /Y /I "Files" "%TEMP%" ^| find "\"') do (
        echo(%%G
    )
    popd
)

pushd and popd are used to change into and return from the root directory, respectively. pushdpopd分别用于切换到根目录和从根目录返回。

The find command is used to suppress xcopy 's summary line # File(s) . find命令用于抑制xcopy的摘要行# File(s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从具有%〜dp0%的路径复制文件 - Copying files from a path with %~dp0% does not work 我们如何在批处理文件中使用带有“ren”命令的当前目录路径(%~dp0)? - How can we use the current directory path(%~dp0) with 'ren' command in a batch file? 如何将%〜dp0写入由批处理文件创建的批处理文件? - How to write %~dp0 into a batch file created by a batch file? “%〜dp0”和“。\\”之间的区别? - Difference between “%~dp0” and “.\”? 最后一个参数中的正斜杠导致更改批处理文件目录(“%〜dp0”)的路径 - Forward slash in last argument causes path to directory of batch file (“%~dp0”) to change 改变目录停止 %~dp0 工作 - changing directory stops %~dp0 from working %〜dp0不返回包含正在运行的脚本的文件夹 - %~dp0 not returning folder containing the running script 使用REG ADD将%〜DP0写入注册表 - Writing %~DP0 to Registry using REG ADD 在Powershell中等效于%〜dp0(使用Expand-Archive cmdlet) - %~dp0 equivalent in powershell (using Expand-Archive cmdlet) Windows CLI列表要求:dir / n / s列表,但每个文件dir / b / s一行,并且具有内联完整路径 - Windows CLI list requirement: dir /n/s list, but one line per file dir /b/s and with inline full path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM