Replace tab characters with commas in a string

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?
 
F

foxidrive

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

Guest

So I used

set str=%str:TAB=,%.

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

Todd Vargo

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

Guest

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?
 
A

Al Dunbar

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
 
T

Todd Vargo

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=,!
)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top