我想批处理脚本到文件夹中的所有文本文件。 这是我迄今为止所pipe理的:
@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
主要的问题似乎是(%dir1%* .txt)
它可能是
@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 %%X in ("%dir1%\*.txt") DO echo %%~dpnX echo Done pause
引号是为了避免路径中的空格或其他特殊字符的问题。
编辑:
%%~dpnX
用于扩展%%X
的文件名
d
=驱动器(C 🙂
p
=路径(\ Users \ Family \ Desktop \示例)
n
= filename(test1)(没有扩展名)
f
=完整文件名(C:\ Users \ Family \ Desktop \ Example \ test1.txt)。
可能的修饰符在这里解释FOR /?