Windows script package: substring calculation

I have such a situation. I have a list of urls in a file.

SET str1="http://www.domain.com/dir1/dir2/dir3/dir4/dir5/file1.txt"

In the line above, http://www.domain.com/dir1/dir2/dir3 constant in all URLs. I need to extract the rest of the path in each url.

I mean, I need to get the final line from the above url /dir4/dir5/file1.txt

thanks

+6
source share
2 answers

You need the notation %var:~start,end% . For example, if you run this:

 @SET str1="http://www.domain.com/dir1/dir2/dir3/dir4/dir5/file1.txt" @ECHO %str1:~37,-1% 

This opens /dir4/dir5/file1.txt .

+9
source

Alternatively, you can use the syntax %variable:str_to_delete=% to remove the character string from this variable. Thus, you do not need to rely on the position of the character within the string.

Code example:

 @echo off set str1="http://www.domain.com/dir1/dir2/dir3/dir4/dir5/file1.txt" :: remove the common part of the path set str2=%str1:http://www.domain.com/dir1/dir2/dir3=% :: remove the quotes set str2=%str2:"=% echo.%str2% 

Conclusion:

 /dir4/dir5/file1.txt 
+5
source

Source: https://habr.com/ru/post/890031/


All Articles