开发者

Batch FOR space problem

开发者 https://www.devze.com 2023-03-15 03:56 出处:网络
I tried to list all files and directories on a directory by using this format dir1:::dir2:::file1:::file2:::

I tried to list all files and directories on a directory by using this format

dir1:::dir2:::file1:::file2:::

To achieve this, I wrote a batch script. Take a look at it :

    @echo off
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET M=
    FOR %%d IN ('dir /B') DO SET M=!M!%%d:::
    ECHO %M%

Well, it works for directories/files that doesn't contain spaces, but for those th开发者_如何学Pythonat contained it, it will show just the first word. For example, suppose the files are "Blue hills.jpg" and "Sunset.jpg".

The expected result is of course

Blue hills.jpg:::Sunset.jpg:::

But what appears instead is

Blue:::Sunset.jpg

FYI, I use WinXP. *Is that matter? I've tried to put quotes in "%%d" but it doesn't work. How can I fix this? Thanks for the help! And sorry for my bad english, I really have to improve it..


You need to run your for loop for file names containing any text (spaces included) "tokens=*". The /f switch is to search for text (filename text).

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET M=
FOR /f "tokens=*" %%d IN ('dir /B') DO SET M=!M!%%d:::

ECHO %M%

Works for files and directories with spaces.

If you use tokens=1 then you get the first word of each file name (using a space as separator). So you would see

Blue:::Sunset.jpg:::

If you use tokens=2 then you get the second word:

hills.jpg:::
0

精彩评论

暂无评论...
验证码 换一张
取 消