如何使用批处理脚本读取带空格的行?(How to read a line with spaces using batch script?)

编程入门 行业动态 更新时间:2024-10-22 19:38:58
如何使用批处理脚本读取带空格的行?(How to read a line with spaces using batch script?)

我有一个名为file.txt的文件,其中包含几行

1.vishnu 2.ram 3.gopal 4.suresh 5.chandra sekar 6.prem kumar 7.nandhu

我编写了一个批处理脚本来读取每一行并创建一个文件夹并将这些行指定为该文件夹的名称。

FOR /f "tokens=* delims=" %a in (file.txt) DO @md %a

但是,创建的文件夹为

vishnu ram gopal suresh chandra sekar prem kumar nandhu

这里的chandra和sekar被分隔为两个文件夹,但我想将chandra sekar作为单个文件夹名称,如何实现这一点?

I have a file called file.txt in which it contains few lines as

1.vishnu 2.ram 3.gopal 4.suresh 5.chandra sekar 6.prem kumar 7.nandhu

I have written a batch script to read each line and to create a folder and assign the lines as name to that folder.

FOR /f "tokens=* delims=" %a in (file.txt) DO @md %a

but, the folder created as

vishnu ram gopal suresh chandra sekar prem kumar nandhu

Here the chandra and sekar are seperated as two folders, but i want the chandra sekar as a single folder name, how to acheive this?

最满意答案

尝试这个:

FOR /F "tokens=2 delims=." %%A IN (file.txt) DO @md "%%A"

说明: FOR -loop变量使用双倍百分比%% 。 此外,您需要将%%A中包含的文件名括在双引号中,以便将其解释为单个目录名,即使它包含空格。 最后,如果使用"tokens=* delims=" ,则创建的目录名称也将包含前导编号,即1.vishnu , 2.ram等。

Try this:

FOR /F "tokens=2 delims=." %%A IN (file.txt) DO @md "%%A"

Explanation: FOR-loop variables use a double-percent %%. Also, you need to enclose the filename contained in %%A in double quotes, so that it is interpreted as a single directory name, even if it contains spaces. Finally, if you use "tokens=* delims=", the directory names that are created will contain the leading numbering as well, i.e. 1.vishnu, 2.ram and so on.

更多推荐

本文发布于:2023-08-07 06:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:批处理   空格   如何使用   脚本   read

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!