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

Author Topic: Remove Spaces from String  (Read 13762 times)

0 Members and 1 Guest are viewing this topic.

nothlit

    Topic Starter


    Intermediate

    Thanked: 2
    Remove Spaces from String
    « on: June 11, 2009, 09:05:55 AM »
    In the following code

    Code: [Select]
    Set IP=%string:~0,15
    REM Echo With spaces? "%string%"
    Set IP=%IP: =%
    REM Echo IP no spaces "%IP%" & pause

    This seems to remove the spaces, but when SETLOCAL EnableDelayExpansion in the example below, while in a for loop

    Code: [Select]
    Set StorIP=%%B
    Echo "!StorIP!"
    Set StorIP=!StorIP: =!
    Echo "!StorIP!"
    Set %%B=!StorIP!
    Echo %%B ^& "!StorIP!" & Pause

    Set StorIP=!StorIP: =! seems to keep a space at the end in the IP address, so the IP would be "127.0.0.1 "

    The code I currently have ...
    Code: [Select]
    setlocal enabledelayedexpansion

    set filename=C:\Vision\etc
    set outputfile=test.cfg
    set log=C:\Vision\batlog
    set equalsign=^=


    Set CurrIDP=127.0.0.1
    Set newIDP=192.168.1.1


    For /f "tokens=1,2,3 delims=;=" %%A in (%filename%\system.cfg) do (
    set hasoutput=0
     

    Set StorIP=%%B
    Echo "!StorIP!"
    Set StorIP=!StorIP: =!
    Echo "!StorIP!"
    Set %%B=!StorIP!
    Echo %%B ^& "!StorIP!" & Pause

    If "%%B"=="%CurrIDP%" echo %%A!equalsign!%newIDP%;%%C;>>%outputfile% & set /a hasoutput+=1 & set /a nochg+=1 & Echo A:""%%A"" , B:""%%B"" , C:""%%C"" ^& errorlevel:%Errorlevel% Hasoutput:%hasoutput% - !hasoutput!>>%log%\testdrlog.log

    If "!hasoutput!"=="0" echo %%A!equalsign!%%B;%%C>>%outputfile% & Echo A:""%%A"" , B:""%%B"" , C:""%%C"" ^& errorlevel:%Errorlevel% Hasoutput:%hasoutput% - !hasoutput!>>%log%\testdrlog.log
    )

    An example file
    Code: [Select]
    SERVER.DB=127.0.0.1                    ; DB Files
    SERVER.IMAGE=127.0.0.1                  ; Images
    SERVER.PARMS=127.0.0.1                  ; Parms
    SERVER.LOG=127.0.0.1                      ; System Audit Logging
    BRANCH.IMAGE=127.0.0.1                    ; Branch Capture Server
    SERVER.COMM=127.2.2.1                  ; Communications Server

    What I am trying to do is change an IP address in a file by re-writing the file into another. I think I'm close but can't seem to get ride of the extra white noise space in token %B. thanks for the help.
    « Last Edit: June 11, 2009, 09:19:45 AM by nothlit »

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Remove Spaces from String
      « Reply #1 on: June 11, 2009, 09:40:36 AM »
      what is your expected output?

      nothlit

        Topic Starter


        Intermediate

        Thanked: 2
        Re: Remove Spaces from String
        « Reply #2 on: June 11, 2009, 10:16:56 AM »
        Code: [Select]
        SERVER.DB=192.168.1.1                    ; DB Files
        SERVER.IMAGE=192.168.1.1                  ; Images
        SERVER.PARMS=192.168.1.1                  ; Parms
        SERVER.LOG=192.168.1.1                    ; System Audit Logging
        BRANCH.IMAGE=192.168.1.1                  ; Branch Capture Server
        SERVER.COMM=127.2.2.1                  ; Communications Server

        Something to the effect above. Basically In part of my other code it finds the current IP and then bassed on that IP it changes it to something else, but if the IP differs like in the last line I want it to keep the line the same.

        The ; is a comment out or explanation of what the line does and because it is a comment if the IP address is followed by ;DB files I don't believe it will matter, if it does in my output I can just add a couple spaces.

        gh0std0g74



          Apprentice

          Thanked: 37
          Re: Remove Spaces from String
          « Reply #3 on: June 11, 2009, 05:59:19 PM »
          i still don understand. show all relevant input files as well..

          nothlit

            Topic Starter


            Intermediate

            Thanked: 2
            Re: Remove Spaces from String
            « Reply #4 on: June 11, 2009, 07:30:18 PM »
            ::I just typed my response and lost it so this is a much shorter response::

            Re-asking question Maybe that will explain it better?

            How does one remove spaces from the end of a string when in a FOR loop?


            I am using this bit of code because while in a FOR loop i need my nested VARs to work as counters
            Code: [Select]
            setlocal enabledelayedexpansion
            With the above on and using Set StorIP=!StorIP: =! in my code I still wind up with a space in my Stored IP address. In other words token %%B shown with quotes to show spaces "192.168.1.1                    " becomes "192.168.1.1 " How do I remove all the spaces? With the extra Space this will always fail If "%%B"=="%CurrIDP%"


            I have found this bit of code to work when the default setlocal disabledelayedexpansion is true
            Code: [Select]
            Set IP=%string:~0,15
            REM Echo With spaces? "%string%"
            Set IP=%IP: =%
            REM Echo IP no spaces "%IP%" & pause


            nothlit

              Topic Starter


              Intermediate

              Thanked: 2
              Re: Remove Spaces from String
              « Reply #5 on: June 11, 2009, 07:39:33 PM »

              Code: [Select]
              SERVER.DB=127.0.0.1                    ; DB Files
              SERVER.IMAGE=127.0.0.1                  ; Images
              SERVER.PARMS=127.0.0.1                  ; Parms
              SERVER.LOG=127.0.11.1                      ; System Audit Logging
              BRANCH.IMAGE=127.0.0.1                    ; Branch Capture Server
              SERVER.COMM=127.2.2.1                  ; Communications Server



              That is the 'System.cfg' file

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: Remove Spaces from String
                « Reply #6 on: June 11, 2009, 08:22:15 PM »
                actually i am not interested in how you do it. I am only asking 3 things
                1) input files
                2) clear explanation of what you want using input files as example
                3) output expected.
                that's all.

                nothlit

                  Topic Starter


                  Intermediate

                  Thanked: 2
                  Re: Remove Spaces from String
                  « Reply #7 on: June 11, 2009, 08:42:39 PM »
                  actually i am not interested in how you do it. I am only asking 3 things
                  1) input files
                  2) clear explanation of what you want using input files as example
                  3) output expected.
                  that's all.

                  1.Input File - 'system.cfg'

                  Code: [Select]
                  SERVER.DB=127.0.0.1                    ; DB Files
                  SERVER.IMAGE=127.0.0.1                  ; Images
                  SERVER.PARMS=127.0.0.1                  ; Parms
                  SERVER.LOG=127.0.0.1                      ; System Audit Logging
                  BRANCH.IMAGE=127.0.0.1                    ; Branch Capture Server
                  SERVER.COMM=127.2.2.1                  ; Communications Server



                  2.clear explanation of what you want using input files as example
                  Based on Hardcoded IP in BATCH lets say IP=127.2.2.1 I want to replace this IP with another 192.168.1.1

                  3.output expected.
                  Code: [Select]
                  SERVER.DB=127.0.0.1                    ; DB Files
                  SERVER.IMAGE=127.0.0.1                  ; Images
                  SERVER.PARMS=127.0.0.1                  ; Parms
                  SERVER.LOG=127.0.0.1                      ; System Audit Logging
                  BRANCH.IMAGE=127.0.0.1                    ; Branch Capture Server
                  SERVER.COMM=192.168.1.1                  ; Communications Server



                  gh0std0g74



                    Apprentice

                    Thanked: 37
                    Re: Remove Spaces from String
                    « Reply #8 on: June 11, 2009, 08:54:00 PM »
                    vbscript
                    Code: [Select]
                    Set objFS = CreateObject("Scripting.FileSystemObject")
                    strFile = "c:\test\file.txt"
                    strHardCodedIP = "127.2.2.10"
                    Set objFile = objFS.OpenTextFile(strFile)
                    Do Until objFile.AtEndOfStream
                    strLine = objFile.ReadLine
                    If InStr(strLine,"SERVER.COMM") >0 Then
                    splitted = Split(strLine,"=")
                    strIP_with_comments  = splitted(1)
                    strIP = Split(strIP_with_comments," ")
                    strIP(0)=strHardCodedIP
                    strIP = Join(strIP," ")
                    splitted(1) = strIP
                    strLine = Join(splitted,"=")
                    End If
                    WScript.Echo strLine
                    Loop
                    output
                    Code: [Select]
                    C:\test>more file.txt
                    SERVER.DB=127.0.0.1                     ; DB Files
                    SERVER.IMAGE=127.0.0.1                  ; Images
                    SERVER.PARMS=127.0.0.1                  ; Parms
                    SERVER.LOG=127.0.0.1                            ; System Audit Logging
                    BRANCH.IMAGE=127.0.0.1                          ; Branch Capture Server
                    SERVER.COMM=127.2.2.1                   ; Communications Server

                    C:\test>cscript /nologo test.vbs
                    SERVER.DB=127.0.0.1                     ; DB Files
                    SERVER.IMAGE=127.0.0.1                  ; Images
                    SERVER.PARMS=127.0.0.1                  ; Parms
                    SERVER.LOG=127.0.0.1                            ; System Audit Logging
                    BRANCH.IMAGE=127.0.0.1                          ; Branch Capture Server
                    SERVER.COMM=127.2.2.10                  ; Communications Server

                    nothlit

                      Topic Starter


                      Intermediate

                      Thanked: 2
                      Re: Remove Spaces from String
                      « Reply #9 on: June 11, 2009, 09:06:11 PM »
                      I love the reply but I can't do VB script :( The system it is running on has to be dos.

                      gh0std0g74



                        Apprentice

                        Thanked: 37
                        Re: Remove Spaces from String
                        « Reply #10 on: June 11, 2009, 09:13:51 PM »
                        you mean you are using plain old MSDOS 6.22 ??? if you are using Win2k/WinXP, you can use vbscript
                        Code: [Select]
                        c:\test> cscript /nologo myscript.vbs

                        nothlit

                          Topic Starter


                          Intermediate

                          Thanked: 2
                          Re: Remove Spaces from String
                          « Reply #11 on: June 11, 2009, 09:40:59 PM »
                          No Can't use VB script because I know nothing about it and the rest of my code is written in DOS. The rest of my code works the only part I can't finish is taking about the spaces at the end of the string.

                          wbrost



                            Intermediate
                          • Thanked: 11
                            Re: Remove Spaces from String
                            « Reply #12 on: June 12, 2009, 06:49:19 AM »
                            you could try the following as long as there is always a space after:


                            Code: [Select]
                            SET A=%%A
                            SET B=%%B
                            SET C=%%c

                            SET A=%A:~0,-1%
                            SET B=%B:~0,-1%
                            SET C=%C:~0,-1%


                            KenL



                              Beginner
                              Re: Remove Spaces from String
                              « Reply #13 on: June 12, 2009, 07:22:27 AM »
                              I had the same problem in my generated scripts. DOS will actually take everything between the last IP number and all the spaces until the ; comment.

                              Move your comments above each line and make sure there is a Carriage return after the IP address.

                              If this file is generated, add a Carriage Return/Line Feed at the end of the IP address.


                              Ken
                              Windows XP PRO/Server 2003. Date Format DD/MM/YYYY

                              nothlit

                                Topic Starter


                                Intermediate

                                Thanked: 2
                                Re: Remove Spaces from String
                                « Reply #14 on: June 12, 2009, 07:44:27 AM »
                                you could try the following as long as there is always a space after:


                                Code: [Select]
                                SET A=%%A
                                SET B=%%B
                                SET C=%%c

                                SET A=%A:~0,-1%
                                SET B=%B:~0,-1%
                                SET C=%C:~0,-1%



                                Thank you for the replies KenL and wbrost

                                @wbrost, Wow I knew that bit of code and didn't even think about it. In my quick 5 minute testing it appears it will work regardless of the length of my string, as long as there is a space I appear to be good and in the cases so far Set StorIP=!StorIP: =! leaves a space. Thanks for the pointer.

                                @KenL
                                In fact it is a config file for an installed application and I'm trying to automate some changes on many systems. Good idea though. :)