0

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:*\=%.

Hsu Amanda
  • 31
  • 1
  • 1
  • 9
  • This is interesting. Where do you get the `%temp1:-*=%` construct from? Well, it does _not_ work in Batch files... **`:/`** – Aacini Feb 27 '19 at 12:32
  • @GerhardBarnard: Are you suggesting to use a 1000-lines Batch-JScript hybrid script or an entirely different language to perform something that can be achieved in a very simple and short Batch line? I don't think it is a good advice... **`:(`** – Aacini Feb 27 '19 at 12:48
  • 1
    `for /F "tokens=2 delims=$" %%a in ("%temp1:_-_=$%") do echo %%a` – Aacini Feb 27 '19 at 12:56
  • @Aacini, misread the question, thought it was about replacing `=` :) – Gerhard Feb 27 '19 at 13:43
  • I'd never use string manipulation to get components of a path; rather I'd use the [`~` modifiers](http://ss64.com/nt/syntax-args.html) of [`for` loop](http://ss64.com/nt/for.html) variables... – aschipfl Feb 27 '19 at 16:01

1 Answers1

2

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...

Aacini
  • 65,180
  • 12
  • 72
  • 108