[英]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
output 是文件/目錄的完整路徑,但我想通過從 output 中刪除 %~dp0 的路徑來使其更簡單
只需從每個條目中刪除%~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=!
)
這是我建議您合並的方法,它可以保護可能包含的文件名!
字符並將 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
如果您不想使用相對路徑類型 output 則更改:_.*%~dp0Files=.!
到:_:*%~dp0Files\=!
或者,您可以使用較慢的forfiles.exe
實用程序獲取相對路徑:
%__AppDir__%forfiles.exe /P "%~dp0Files" /S /C "%__AppDir__%cmd.exe /D/Q/C If @IsDir==FALSE Echo @RelPath"
如果您更喜歡沒有雙引號的情況,那么此修改應該這樣做:
%__AppDir__%forfiles.exe /P "%~dp0Files" /S /C "%__AppDir__%cmd.exe /D/Q/C If @IsDir==FALSE For %%G In (@RelPath)Do Echo %%~G"
如果你想要它沒有前導.\
那么也許:
%__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"
您也可以通過利用powershell.exe
來做到這一點:
%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile Get-ChildItem -Path "%~dp0Files" -File -Force -Name -Recurse
這可以做到, (不推薦) ,在短短的一行:
powershell -NoP ls "%~dp0Files" -File -Fo -Na -Rec
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.