Batch file to add characters to beginning and end of each line in txt file

后端 未结 4 1361
小鲜肉
小鲜肉 2020-12-06 06:50

I have a text file, I was wondering anyone have a batch file to add \" to the beninning and \", at the end of each line in a text file?

For example I have



        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 07:17

    set "f=PATH\FILE.txt"
    call :add_str_beginning_end_each_line "BEGIN_LINE--" "--END_LINE" "%f%"
    
    
    REM : Adds strings at the beginning and end of each line in file
    :add_str_beginning_end_each_line
        set "str_at_begining=%~1"
        set "str_at_end=%~2"
        set "input_file=%~3"
    
        set "tmp_file=tmp.ini"
    
        REM : >NUL => copy command is silent
        REM : Make a backup
        copy /Y "!input_file!" "!input_file!.bak" >NUL
        copy /Y "!input_file!" "!tmp_file!" >NUL
        del "!input_file!"
    
        REM : Add strings at each line
        for /f "delims=" %%a in (!tmp_file!) do (
            >>"!input_file!" echo !str_at_begining!%%a!str_at_end!
        )
    
        REM : delete backup
        del "!tmp_file!"
        del "!input_file!.bak"
    
        REM : exiting the function only
        EXIT /B 0
    

    You can edit the code :

        "!input_file!" echo !str_at_begining!%%a!str_at_end!
    

    By removing !str_at_end! to add str only at the beginning of the line, where %%a is the actual line.

提交回复
热议问题