windows 在批处理脚本中循环文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4663928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loop through file names in a Batch Script
提问by Krayons
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
回答by jeb
The main problem seems to be the space between (%dir1% *.txt)
主要问题似乎是 (%dir1% *.txt) 之间的空格
It could be
它可能是
@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.
引号是为了避免路径中出现空格或其他特殊字符的问题。
EDIT:
The %%~dpnX
is for expanding the filename of %%X
tod
=drive(C:)p
=path(\Users\Family\Desktop\Example)n
=filename(test1) (without extension)
编辑:
该%%~dpnX
是用于扩展的文件名%%X
来d
=驱动器(C :) p
=路径(\ Users \用户家庭\桌面\实施例)n
=文件名(TEST1)(不扩展)
f
=full filename(C:\Users\Family\Desktop\Example\test1.txt).
f
= 完整文件名(C:\Users\Family\Desktop\Example\test1.txt)。
The possible modifiers are explained here FOR /?
这里解释了可能的修饰符FOR /?