循环遍历批处理脚本中的文件名
我想要一个文件夹中所有文本文档的批处理脚本。 这是我到目前为止所管理的:
@ECHO off
title Test
set dir1=C:\Users\Family\Desktop\Example
:Start
cls
echo 1. test loop
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto test
if %choice%==2 exit
:test
cls
echo running loop test
FOR %%n in (%dir1% *.txt) DO echo %dir1%\%%n
echo Done
pause
我想要输出的是:
running loop test
C:\Users\Family\Desktop\Example\doc 1.txt
C:\Users\Family\Desktop\Example\doc 2.txt
Done
但是我得到了这个:
running loop test
C:\Users\Family\Desktop\Example\C:\Users\Family\Desktop\Example
C:\Users\Family\Desktop\Example\doc 1.txt
C:\Users\Family\Desktop\Example\doc 2.txt
Done
I would like a batch script to all the text documents in a folder.
This is what I have managed so far:
@ECHO off
title Test
set dir1=C:\Users\Family\Desktop\Example
:Start
cls
echo 1. test loop
echo 2. Quit
set /p choice=I choose (1,2):
if %choice%==1 goto test
if %choice%==2 exit
:test
cls
echo running loop test
FOR %%n in (%dir1% *.txt) DO echo %dir1%\%%n
echo Done
pause
What I would like outputted is:
running loop test
C:\Users\Family\Desktop\Example\doc 1.txt
C:\Users\Family\Desktop\Example\doc 2.txt
Done
But I Get this:
running loop test
C:\Users\Family\Desktop\Example\C:\Users\Family\Desktop\Example
C:\Users\Family\Desktop\Example\doc 1.txt
C:\Users\Family\Desktop\Example\doc 2.txt
Done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要问题似乎是 (%dir1% *.txt) 之间的空格,
引号可能是
为了避免路径中空格或其他特殊字符的问题。
编辑:
%%~dpnX
用于将%%X
的文件名扩展为d
=驱动器(C:)p
=路径(\Users\Family\Desktop\Example)n
=文件名(test1)(不带扩展名)f
=完整文件名(C:\Users\Family\Desktop\Example\test1.txt)。此处解释了可能的修饰符FOR /?
The main problem seems to be the space between (%dir1% *.txt)
It could be
The quotes are for avoiding problems with spaces or other special characters in the path.
EDIT:
The
%%~dpnX
is for expanding the filename of%%X
tod
=drive(C:)p
=path(\Users\Family\Desktop\Example)n
=filename(test1) (without extension)f
=full filename(C:\Users\Family\Desktop\Example\test1.txt).The possible modifiers are explained here FOR /?