2

Suppose I have file name text.txt and contains several lines. Each line starts with dayname and finished with a "/" char then the path is added. I just need the file names on the other hand for each line not the path. Thus I need something to convert the file from this way:

mon/C:\stt\test_file.abc
sat/C:\Documents and Settings\alguri\Desktop\test_file2.txt
fri/C:\sat\new_folder2\file3.jpg

to

mon/test_file.abc
sat/test_file2.txt
fri/file3.jpg

I need this code at dos command. I tried some code but can not parse the string in different ways. Because path name can have more than one "\" char.

For istance if I know that there is only two "\" chars in a line. I can do something like:

for /f "tokens=1,4 delims=/\" %%a in (text.txt) do (
echo %%a
    )

This will give the right answer only for line 1 which is "test_file.abc" However all other lines have different number of "\" char. How do you think I can handle this?

One solution in my mind is to count number of "\" and set the tokens second parameter to decide which part I need. But I could do that. Besides tokens value might not be set dynamically as I thought.

user1517831
  • 21
  • 1
  • 3

1 Answers1

3

You can use the parameter modifiers.

for /f "tokens=1,* delims=/" %%A in (test.txt) do (
    echo %%A/%%~nxB
)

For more infos about the modifiers you could read What does %~dp0 mean, and how does it work?

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225