bhavik sanghvi
bhavik sanghvi

Reputation: 105

Batch commands to swap text from file

This is the input file:-

Hello,1-23456
How are you,2-34567
.
.
.

I want output:-

1-23456,Hello
2-34567,How are you
.
.
.

Please help me to create a batch file for this.

I have tried this

@echo off &setlocal

set "textfile=inputchq.csv"
set "newfile=output.txt"

(for /f "tokens=1,2 delims=," %%i in (%textfile%) do set srno=%%a&set comments=%%b (
    set "line=%%i"
    setlocal enabledelayedexpansion
set "line=%comments%,%srno%"
    echo(!line!
    endlocal
))>>"%newfile%"

But i am not getting the desired output

Upvotes: 1

Views: 670

Answers (1)

Stephan
Stephan

Reputation: 56238

@echo off &setlocal

set "textfile=inputchq.csv"
set "newfile=output.txt"

(for /f "tokens=1,2 delims=," %%a in (%textfile%) do (
    echo %%b,%%a"
))>>"%newfile%"

Edit to match additional requirement from comment:

you need delayed expansion and to escape the special character "|"

@echo off 
setlocal enabledelayedexpansion

set "textfile=inputchq.csv"
set "newfile=output.txt"

(for /f "tokens=1,2 delims=," %%a in (%textfile%) do (
    set "line=%%b,%%a"
    echo !line:^|=.!
))>>%newfile%

Upvotes: 0

Related Questions