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
just remove %~dp0
from each entry (Note: that doesn't work with %%G
metavariables, you have to use a "normal" environment variable):
@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 :
@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=.!
to :_:*%~dp0Files\=!
Alternatively, you could grab the relative paths using the slower, forfiles.exe
utility:
%__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
:
%__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
:
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.
The find
command is used to suppress xcopy
's summary line # File(s)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.