Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file DOS CMD批处理:完整路径名错误?(参数扩展~f)_Batch File_Cmd_Path_Pathname - Fatal编程技术网

Batch file DOS CMD批处理:完整路径名错误?(参数扩展~f)

Batch file DOS CMD批处理:完整路径名错误?(参数扩展~f),batch-file,cmd,path,pathname,Batch File,Cmd,Path,Pathname,要列出指定路径中文件的完整路径名,我可以使用: FOR /F "tokens=*" %G IN ('DIR /B "path\*.*"') DO echo %~fG 错误结果:\*.* ss64.com说:“如果没有驱动器号/路径的文件名被展开以显示驱动器号/路径,命令shell将假定该文件位于当前目录中,这通常是错误的。” 这是相当愚蠢的行为。然而,这可能是问题所在,因为DIR在这里返回一个空文件名 有没有办法避免这种错误? 因为它很容易制作 我知道我可以在DIR命令中使用/S选项,这会使结

要列出指定路径中文件的完整路径名,我可以使用:

FOR /F "tokens=*" %G IN ('DIR /B "path\*.*"') DO echo %~fG
错误结果:
\*.*

ss64.com说:“如果没有驱动器号/路径的文件名被展开以显示驱动器号/路径,命令shell将假定该文件位于当前目录中,这通常是错误的。”

这是相当愚蠢的行为。然而,这可能是问题所在,因为DIR在这里返回一个空文件名

有没有办法避免这种错误? 因为它很容易制作

我知道我可以在DIR命令中使用/S选项,这会使结果成为一个完整的路径名,但它也会通过不需要的子文件夹

使用以下语法一切正常,但我不能使用DIR命令的优点:

FOR %G IN ("path\*.*") DO echo %~fG
结果:
\*.*


您有任何关于如何使用DIR和完整路径的提示或技巧吗?

如何使用文件的
呢?这将为您提供所需文件夹的完整路径:

forfiles /p C:\Some\Directory\ /c "cmd /c echo @path"

FORFILES
非常强大,因为它提供了许多选项,如过滤器、搜索子文件夹等。有关更多信息,请查看网站。

如果你真的需要使用
dir
命令

@echo off
setlocal ENABLEDELAYEDEXPANSION

set _subdir=path
set _mask=*.*

call :get_absolute_path _prefix "%CD%\%_subdir%"

rem  Iterate through a list of files, including files in subdirectories
for /f "tokens=*" %%A in ('dir /b /s /a:-d "%_prefix%\%_mask%"') do (
    rem  The current full file path
    set _f=%%A
    rem  Cut the "%CD%\%_subdir%\" prefix from the current file path
    set _f=!_f:%_prefix%\=!
    rem  Test whether the relative file path has a "subdir\" prefix:
    rem    split the relative file path by "\" delimiter and
    rem    pass %%B and %%C tokens (subdir and the remainder) to the loop
    for /f "delims=\ tokens=1*" %%B in ("!_f!") do (
        rem  If the remainder is empty string then print the full file path
        if "%%C"=="" echo %%A
    )
)

endlocal
exit /b 0

:get_absolute_path
set %1=%~f2
exit /b 0

环境变量CD在任何时候都包含当前目录的路径,并且在末尾始终没有反斜杠

因此,您可以使用以下示例:

@echo off
set "DirectoryPath=%CD%\path"
for /F "tokens=*" %%G in ('dir /B "path\*.*"') do echo %DirectoryPath%\%%G
因此,无论何时使用带裸输出格式的DIR而不同时使用
/S
,都必须首先确定目录路径,并在循环体中引用该路径

使用固定绝对路径的示例:

@echo off
for /F "tokens=*" %%G in ('dir /B "C:\Temp\My Folder\*.*"') do echo C:\Temp\My Folder\%%G

不要忘记路径或文件名的双引号,在除echo以外的其他命令中包含空格

另一个相关问题:为什么参数扩展过程
~f
不生成“未找到文件”错误?如果使用
DIR/B/S
会怎么样?我想
DIR/S
也会递归地遍历各个子目录,这是不需要的。ss64.com是一个很棒的站点,我主要看这里。但是,我在XP下运行,
FORFILES
在这里声明只有在XP下才可以使用ResourceKit,我恐怕没有。但我可以试一试。谢谢你,谢谢。不管怎样,还有其他选项可以选择
DIR
?我想要一个按日期排序的文件列表,所以我认为
DIR
是一个简单的方法。不,没有这样的选项。您可以使用
dir…|findstr/r“…”
但是findstr中的regexp是蹩脚的。在完整文件路径中检测subdir并不容易。对不起,我的意思是:“除了
DIR
,还有其他选项吗?”(可能是选项,而不是命令选项)谢谢,我相信这种特殊的方法只解决了路径相对于当前目录的问题,但是,我知道有必要将路径存储在循环之外,然后将其与文件名结合起来。如果在命令DIR上使用了绝对路径,则在不使用命令DIR上的
/S
处理具有完整路径的文件名时,FOR循环内的文件引用也必须使用相同的路径。