Replace tab characters with commas in a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

In a windows batch file, there is a string str="H:\ I:\" (the delimiter
is a tab character). And I want to change the tab character in the string to
a comma, that is, changing the value of str to "H:\,I:\". I have tried using
the following command but it does not work:

set str=%str:^t=,%

Can anyone tell me what to do?
 
In a windows batch file, there is a string str="H:\ I:\" (the delimiter
is a tab character). And I want to change the tab character in the string to
a comma, that is, changing the value of str to "H:\,I:\". I have tried using
the following command but it does not work:

set str=%str:^t=,%

Can anyone tell me what to do?

Use a literal TAB in place of the ^t

Some editors will swap tabs for spaces, so use an editor that deosn't have
that behaviour, like notepad.
 
So I used

set str=%str:TAB=,%.

But then str becomes ",=," instead of "H:\,I:\". Any idea how that happens?
 
Michelle said:
So I used

set str=%str:TAB=,%.

But then str becomes ",=," instead of "H:\,I:\". Any idea how that
happens?

Please show your code with actual usage. I suspect you are trying to change
a variable within a FOR loop which may need delayed expansion enabled but
need to see your code implementation to verify that is the case.
 
You are right, I am using the statement in the following FOR loop:

for /f "usebackq tokens=*" %%j in (`hares -value %2 Owned_paths`) do (
set MOUNT=%%j
set MOUNT=%MOUNT:TAB=,%
)

I have tried creating a value DelayedExpansion in
HKEY_CURRENT_USER\Software\Microsoft\Command Processor, and assign the value
data to 1. But it still does not work.

Any updates?
 
Michelle said:
You are right, I am using the statement in the following FOR loop:

for /f "usebackq tokens=*" %%j in (`hares -value %2 Owned_paths`) do (
set MOUNT=%%j
set MOUNT=%MOUNT:TAB=,%
)

I have tried creating a value DelayedExpansion in
HKEY_CURRENT_USER\Software\Microsoft\Command Processor, and assign the
value
data to 1. But it still does not work.

Any updates?

Try this:
for /f "usebackq tokens=*" %%j in (`hares -value %2 Owned_paths`) do (
set MOUNT=%%j
set MOUNT=!MOUNT:TAB=,!
)

With delayed expansion enabled, variable values given as %variablename% are
still expanded immediately. To actually delay the expansion, you need to use
exclamation marks instead of percent signs.

/Al
 
Michelle said:
You are right, I am using the statement in the following FOR loop:

for /f "usebackq tokens=*" %%j in (`hares -value %2 Owned_paths`) do (
set MOUNT=%%j
set MOUNT=%MOUNT:TAB=,%
)

I have tried creating a value DelayedExpansion in
HKEY_CURRENT_USER\Software\Microsoft\Command Processor, and assign the value
data to 1. But it still does not work.

Any updates?

You can set delayed expansion right in the batch.

setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%j in (`hares -value %2 Owned_paths`) do (
set MOUNT=%%j
set MOUNT=!MOUNT:TAB=,!
)
 
Back
Top