1.

Solve : Batch file to find a specific space and delete it?

Answer»

Hello all,

PLEASE will someone show me how to write a batch file that will delete any space before the last \ in a var.

eg turn "\Rock and Roll\Music \Dance" into "\Rock and Roll\Music\Dance"

The length and CONTENTS of the string are unknown  but the space is always immediately before the last \

Many Thanks!

Chrisyou can use vbscript instead
Code: [SELECT]strData = "\Rock and Roll\Music \Dance"
ArrayData = Split(strData,"\")
For i=LBound(ArrayData) To UBound(ArrayData)
ArrayData(i) = Trim(ArrayData(i))
Next
strFinal = Join(ArrayData,"\")
WScript.Echo strFinal

'or another way, using replace
WScript.Echo Replace(strData," \","\")


echo off
set string1=\Rock and Roll\Music \Dance
set string2=%string1: \=\%
echo %string1%
echo %string2%
Thanks Salmon Trout! It worked perfectly!To replace every occurence of abc with xyz in a string:

Change the string itself:

Code: [Select]set string=%string:abc=xyz%
Or create a new, CHANGED string:

Code: [Select]set string2=%string1:abc=xyz%
Usual remarks about delayed expansion etc apply

Quote from: Salmon Trout on July 26, 2009, 03:06:01 AM

echo off
set string1=\Rock and Roll\Music \Dance
set string2=%string1: \=\%
echo %string1%
echo %string2%


I think OP will not ever have such situations, but just for resiliency and completeness, i am sure the above can be made to remove more than 1 space eg

Code: [Select]\Rock and Roll\Music     \Dance
Quote from: gh0std0g74 on July 26, 2009, 07:40:43 PM
I think OP will not ever have such situations, but just for resiliency and completeness, i am sure the above can be made to remove more than 1 space eg

Code: [Select]\Rock and Roll\Music     \Dance


Code: [Select]set string=%string:abc=xyz%
The first string, represented above as "abc" (the one to be replaced) can be any* nonzero length and the second string , represented above as "xyz" (the replacement string) can be of any length from zero upwards, that is, a zero length second string results in every instance of the first string being deleted. They don't have to be the same length.

*I say "any" length; obviously there'll be a limit, a POWER of 2 minus 1, 255 or 65535 I expect.


Discussion

No Comment Found