Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to find a specific space and delete it  (Read 5140 times)

0 Members and 1 Guest are viewing this topic.

tripodkid

    Topic Starter


    Greenhorn

    Batch file to find a specific space and delete it
    « on: July 25, 2009, 11:36:12 AM »
    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!

    Chris

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Batch file to find a specific space and delete it
      « Reply #1 on: July 25, 2009, 10:51:57 PM »
      you 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," \","\")



      Salmon Trout

      • Guest
      Re: Batch file to find a specific space and delete it
      « Reply #2 on: July 26, 2009, 03:06:01 AM »
      @echo off
      set string1=\Rock and Roll\Music \Dance
      set string2=%string1: \=\%
      echo %string1%
      echo %string2%

      tripodkid

        Topic Starter


        Greenhorn

        Re: Batch file to find a specific space and delete it
        « Reply #3 on: July 26, 2009, 11:17:12 AM »
        Thanks Salmon Trout! It worked perfectly!

        Salmon Trout

        • Guest
        Re: Batch file to find a specific space and delete it
        « Reply #4 on: July 26, 2009, 11:45:59 AM »
        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

        « Last Edit: July 26, 2009, 11:57:31 AM by Salmon Trout »

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Batch file to find a specific space and delete it
          « Reply #5 on: July 26, 2009, 07:40:43 PM »
          @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

          Salmon Trout

          • Guest
          Re: Batch file to find a specific space and delete it
          « Reply #6 on: July 27, 2009, 12:28:36 AM »
          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.