cmd - 将 1 个文件名导出到 txt

cmd - export 1 file name to txt

我是脚本新手,所以这可能很简单。

如何将目录中的一个文件的名称导出到文本文件?我知道 dir > text_file.txt 将输出目录中所有文件的列表,但是我需要它来创建一个循环,它将单独打印每个文件名。

提前致谢。

稍微模糊的问题回答有点模糊。

for /r %%G in (*) do echo %%G

让我们说 .txt 个文件,使用:

for /r %%G in (*.txt) do echo %%G

如果您想添加更多命令,格式会更好:

for /r %%G in (*) do (
    echo Full path : %%G
    echo Name/Ext  : %%~nxG
)

编辑:

这里有一种方法可以执行您在评论中提到的操作,将当前目录中的所有文件回显到一个文本文件中:

rem first delete existing output files to clear them.
del full_path.txt >nul
del name_and_ext.txt >nul

for /r %%G in (*) do (
    echo %%G >> full_path.txt
    echo %%~nxG >> name_and_ext.txt
)

随意编辑。

注意;

rem this will override all content in file.txt with !variable!
echo !variable! > file.txt

rem this will add the content of !variable! to new lines accordingly
echo !variable! >> file.txt