Hsu Amanda
Hsu Amanda

Reputation: 31

How to filter a string in batch file?

I want to handle the string such as project_Name_-_sofeware_Name.txt into project_Name.txt, so I wrote a script like following to run:

set temp1=%~n1
echo %temp1:-*=%

But the output of the bat still been project_Name_-_sofeware_Name.txt.

I have also written another bat file to get the folder name:

:GetFolderName
set temp2=%temp1%
set temp1=%temp2:*\=% 
If Not %temp1%==%temp2% Goto GetFolderName

And the output string is in line with expectations by far.

It also is weird. In my opinion, it should be FolderName after running temp1=%temp2:*\=%.

Upvotes: 0

Views: 1402

Answers (1)

Aacini
Aacini

Reputation: 67216

You may do that in a very simple way:

@echo off

set "temp1=project_Name_-_sofeware_Name.txt"

set "dummy=%temp1:_-_=" & set "temp2=%"

echo %temp2%

Further details at Split string with string as delimiter. Pay attention to the comment at such an answer...

Upvotes: 2

Related Questions