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

Author Topic: Trimming a text file from right to left till special character  (Read 3315 times)

0 Members and 1 Guest are viewing this topic.

sumiRoot

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 7
    Trimming a text file from right to left till special character
    « on: November 22, 2018, 08:13:25 PM »
    Hey Friends,

    I have a text file name as abc.txt and the data reside as below:

    Computer hope/abc/file_name.xml
    Computer hope/abc/file_name2.xml
    Hope Computer/def/file_name3.xml
    ....

    So now i want the trimmed data from the abc.txt file to new file named as def.txt

    file_name.xml
    file_name2.xml
    file_name3.xml

    Can you please help me in this, can we create batch file for this ?

    I have tried searching online but get it below code but not what i want.

    @ECHO off
    cls
    setlocal enableextensions enabledelayedexpansion
    set num=15 ::trimmed letters
    set /a num=%num%+1

    for /f "tokens=* delims= " %%f in (abc.txt) do (   
    set a=%%f
    echo !a:~0,%num%!
    echo !a:~0,%num%! >>def.txt
    )
    pause

    Thanks,
    hope to hear back from you soon.

    Salmon Trout

    • Guest
    Re: Trimming a text file from right to left till special character
    « Reply #1 on: November 23, 2018, 01:29:29 PM »
    As long as the strings are in the form of file paths, you can use the %~nx syntax for FOR variables, which returns the name, dot, and extension, and was designed for this purpose:

    Code: [Select]
    @echo off
    if exist def.txt del def.txt
    for /f "delims=" %%A in (abc.txt) do (
            echo %%~nxA >> def.txt
            )

    abc.txt
    Code: [Select]
    Computer hope/abc/file_name.xml
    Computer hope/abc/file_name2.xml
    Hope Computer/def/file_name3.xml

    def.txt
    Code: [Select]
    file_name.xml
    file_name2.xml
    file_name3.xml

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Trimming a text file from right to left till special character
    « Reply #2 on: November 23, 2018, 08:09:27 PM »
    And the syntax that Salmon Trout has shown is well documented in the help file for the `FOR` command.
    Code: [Select]
    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:

        %~I         - expands %I removing any surrounding quotes (")
        %~fI        - expands %I to a fully qualified path name
        %~dI        - expands %I to a drive letter only
        %~pI        - expands %I to a path only
        %~nI        - expands %I to a file name only
        %~xI        - expands %I to a file extension only
        %~sI        - expanded path contains short names only
        %~aI        - expands %I to file attributes of file
        %~tI        - expands %I to date/time of file
        %~zI        - expands %I to size of file

    sumiRoot

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows 7
      Re: Trimming a text file from right to left till special character
      « Reply #3 on: November 25, 2018, 11:50:24 PM »
      Thanks for your help Salmon Trout, it solved my problem.
      Thanks Squashman for your syntax in For clause.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Trimming a text file from right to left till special character
      « Reply #4 on: November 26, 2018, 12:44:45 PM »
      Thanks Squashman for your syntax in For clause.
      It's not my syntax.  That is from the help file for the FOR command.

      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Trimming a text file from right to left till special character
      « Reply #5 on: November 26, 2018, 02:13:41 PM »
      :P
      " Anyone who goes to a psychiatrist should have his head examined. "