Announcement

Collapse
No announcement yet.

Dos Batch file to read txt file and extract character values.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dos Batch file to read txt file and extract character values.

    hi all,
    I have an text file show as below(output from diruse) , need to extract user's first initial and last name and append "@xxx.com" to create an email address, anyone has idea?

    ! 842.89 1349 SUB-TOTAL: D:\xxx\USERS\jimmy Sortais
    ! 4075.86 5607 SUB-TOTAL: D:\xxx\USERS\Alexia jiang
    .....


    I know" for loop" will be good choice, but stock at detail syntax.

    for /f "tokens=6" %%G in (D:\xxx\Users\personalFolder.txt) do (
    echo %%G > get_users.txt
    )
    thanks in advance!
    jimmy

  • #2
    Re: Dos Batch file to read txt file and extract character values.

    I modify my script to:

    Code:
    for /f "tokens=5,6" %%G in (D:\xxx\Users\personalFolder.txt) do (
    
     FOR /F "tokens=4 delims=\" %%i IN ("%%G") DO set FIRST=%%i
    set F=%FIRST:~0,1%
    echo.%F%
     )
    
    set str=politic
    echo.%str%
    set str=%str:~1,-1%
    echo.%str%
    
    echo.&pause&goto:eof
    the output will be like following:

    D:\>for /F "skip=2 tokens=5,6" %G in (D:\xxx\Users\personalFolder.txt) do
    (
    FOR /F "tokens=4 delims=\" %i IN ("%G") DO set FIRST=%i
    set F=~0,1
    echo.
    )

    D:\>(
    FOR /F "tokens=4 delims=\" %i IN ("D:\xxx\USERS\Alex") DO set FIRST=%i
    set F=~0,1
    echo.
    )

    D:\>set FIRST=Alex


    D:\>set str=politic

    D:\>echo.politic
    politic

    D:\>set str=oliti

    D:\>echo.oliti
    oliti

    D:\>echo. & pause & goto:eof

    Press any key to continue . . .



    my question is how come the "FIRST" is empty? ===>set F=~0,1 ?
    regards!
    jimmy
    Last edited by Rems; 1st January 2010, 19:58.

    Comment


    • #3
      Re: Dos Batch file to read txt file and extract character values.

      if you have gawk for windows (see my sig),
      Code:
      @echo off
      diruse c:\test | gawk -F"\\" "NR>2{sub(/ +/,\"_\",$NF);print $NF\"@xxx.com\"}

      Comment


      • #4
        Re: Dos Batch file to read txt file and extract character values.

        Originally posted by jimmyjiang View Post
        my question is how come the "FIRST" is empty? ===>set F=~0,1 ?
        jimmy, for your batch 'Delayed Expansion' should have been set to Enabled (Setlocal ENABLEDELAYEDEXPANSION).
        Because,,, all the statements added within a for-do loop are infact 'grouped commands'. And, it is not possible to set a variable and expand its value if both these commands are within in the same set of grouped commands.
        After enabeling Delayed Expansion you can and in this case you should use exclamation signs (!var!) around the variable instead of percentage signs (%var%).

        This is a sample using DELAYEDEXPANSION:
        Code:
        @ECHO OFF
        @setlocal ENABLEDELAYEDEXPANSION
        
        Set "inFile=D:\xxx\USERS\personalFolder.txt"
        
        If not exist "%inFile%" (
           >"%inFile%" diruse.exe /* /m "D:\xxx\USERS")
        
        Set "outFile=D:\xxx\USERS\get_users.txt"
        If exist "%outFile%" del "%outFile%"
        
        set "t=3,*"  (tokens from target path)
        
        For /f "tokens=%t% delims=\" %%G in (
           'find.exe "\" ^<"%inFile%"') DO (
        
           For /f "tokens=1,* delims=. " %%i IN (
             'echo\%%H') DO (
        
             set FIRST=%%i& set "FIRST=!FIRST:~0,1!"
             set LAST=%%j
             If defined LAST set "LAST=!LAST: =!"
             If defined LAST Set LAST=.!LAST!
             echo\[email protected]domain.com
             >>"%outFile%" echo\[email protected]domain.com
           )
        )
        
        echo.&pause&goto:eof
        While the sample below not use DELAYEDEXPANSION it does make use of a call to a subroutine for a successfull workaround:
        Code:
        @ECHO OFF
        
        Set "inFile=D:\xxx\USERS\personalFolder.txt"
        
        If not exist "%inFile%" (
           >"%inFile%" diruse.exe /* /m "D:\xxx\USERS")
        
        Set "outFile=D:\xxx\USERS\get_users.txt"
        If exist "%outFile%" del "%outFile%"
        
        set "t=3,*"  (tokens from target path)
        
        for /f "tokens=%t% delims=\" %%G in (
           'find.exe "\" ^<"%inFile%"') DO (
        
           FOR /F "tokens=1,* delims=. " %%i IN (
             'echo\%%H') DO (
             call:write "%%i" "%%j"
             >>"%outFile%" call:write "%%i" "%%j"
           )
        )
        
        echo.&pause&goto:eof
        
        goto:-write subroutine-
           :write
           set FIRST=%~1
           set "FIRST=%FIRST:~0,1%"
           set LAST=%~2
           If defined LAST set LAST=%LAST: =%
           If defined LAST Set LAST=.%LAST%
           echo %FIRST%%LAST%@domain.com
           exit /b 0
        :-write end of subroutine-
        \Rems
        Last edited by Rems; 1st January 2010, 20:32.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Dos Batch file to read txt file and extract character values.

          thanks, this is very helpful!
          jimmy

          Comment


          • #6
            Re: Dos Batch file to read txt file and extract character values.

            Excellent solutions already. Here is my humble addition. I am adding comments to the script.


            Code:
             
            # Script ExtractEmail.txt
            # Input arguments
            var str path, domain
            # path should have a value like "C:/path/to/input file.txt".
            # domain should have a value like "xxx.com".
             
            var str content, line, lastname, firstname, firstinitial
            set $wsep = "\\ "
            cat $path > $content
            while ($content <> "")
            do
                lex "1" $content > $line ; wex "l" $line > $lastname
               wex "l" $line > $firstname ; chex "1" $firstname > $firstinitial
               echo ($firstinitial+$lastname+"@"+domain)
            done


            Script is in biterscripting. Save in file "C:/Scripts/ExtractEmail.txt", run it as


            Code:
            script "C:/Scripts/ExtractEmail.txt" path("C:/path/to/input file.txt") domain("xxx.com")


            Use correct values for the input arguments - path and domain. It will output

            [email protected]
            [email protected]

            To fix case, you can use makelower() or makeupper() functions.
            Last edited by Rems; 29th June 2010, 18:24.

            Comment

            Working...
            X