批处理脚本[遍历文件名] [英] Batch Script [Loop through file names]
问题描述
我想一个批处理脚本到一个文件夹中的所有文本文档。
这就是我成功至今
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
我想输出是
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
在此先感谢
凯尔
Thanks in advance Kyle
推荐答案
主要问题似乎是之间的空间(%DIR1%* .TXT)
The main problem seems to be the space between (%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
引号是避免与路径中的空格或其他特殊字符的问题。
The quotes are for avoiding problems with spaces or other special characters in the path.
编辑:结果
在 %%〜dpnX
是扩大 %% X
的文件名结果 D
=驱动器(C :)结果 P
=路径(\\用户\\家庭\\桌面\\例)结果 N
=文件名(test1.txt的)结果˚F
=完整的文件名(C:\\用户\\家庭\\桌面\\实例\\ test1.txt的)
The %%~dpnX
is for expanding the filename of %%X
to
d
=drive(C:)
p
=path(\Users\Family\Desktop\Example)
n
=filename(test1.txt)
f
=full filename(C:\Users\Family\Desktop\Example\test1.txt).
可能的修饰符在此说明的 FOR /?
The possible modifiers are explained here FOR /?
这篇关于批处理脚本[遍历文件名]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!