简体   繁体   中英

Windows Batch matching a string

In the end I want to match a string and use that value.

I have a lot of folders called TEST123456 with different numbers in different places on my computer. Using dir TEST?????? /s /b /a:d > folders.txt dir TEST?????? /s /b /a:d > folders.txt I can get a file which looks something like:

C:/folder1/folder2/TEST123456
C:/folder1/folder2/TEST654321
C:/folder2/TEST246810

Now this list will be very large so I want it sorted by the number. I want to change this file to:

TEST123456C:/folder1/folder2/TEST123456
TEST654321C:/folder1/folder2/TEST654321
TEST246810C:/folder2/TEST246810

and it would be preferable but not neccesary to already do this when inserting the lines in the text file.

Now I tried using findstr TEST...... folders.txt but it prints the whole line and I only want the matching string. I'm very beginner at Windows Batch so I think it should be relatively easy to create what I want.

You can use FOR /D to loop through folders:

@echo off
>folders.txt (
for /D /R %%A in (test??????) do echo %%~nxA%%A
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM