batch file string character split

2019-09-17 10:06发布

I want to write a batch file to store a character from a file name into a variable.

For example if my file name is helloworld or how_are_you?, I want to store fourth character from right into variable x (which in the above cases would be o and _).
File names do not have spaces in my case.

1条回答
Explosion°爆炸
2楼-- · 2019-09-17 10:40

Assuming the file name is stored in a variable called filename, you can do this:

set "rstr=%filename:~-4%"
set "x=%rstr:~0,1%"

P.S
If you need filename to be passed as a command-line argument (for example, the first), use this before evaluating rstr and x:

set "filename=%1"
查看更多
登录 后发表回答