[英]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. pushd
和popd
分别用于切换到根目录和从根目录返回。
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.