Solved How to change /n to newline

October 10, 2013 at 03:36:29
Specs: Windows 7
I am trying to change backslash characters to extended mode using batch file. can any one help How to change \n to new line or \t to tab.

See More: How to change /n to newline

Report •


✔ Best Answer
October 11, 2013 at 06:33:01
Thank you again nbrane and Razor2.3

slimmed version
Option Explicit
Dim fso,strFilename,strNi,strNiReplace,strTab,strTabR,objFile,oldContent,newContent,secContent

strFilename="c:\testfile.txt"
strNi="/n"
strNiReplace=vbcrlf
strTab="/t"
strTabR=vbtab

'Is it worth it at all
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
wscript.echo "file not found!"
wscript.Quit
end if

'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll

'Write file
newContent=replace(oldContent,strNi,strNiReplace,1,-1,0)
secContent=replace(newContent,strTab,strTabR,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write secContent

objFile.Close

::mike

http://www.computing.net/howtos/sho...



#1
October 10, 2013 at 12:14:15
There are 3 native scripting environments, and 4 native scripting languages in Windows 7. Of those, batch is the worst at handling data. Why not pick up one of the other, better scripting languages?

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

#2
October 10, 2013 at 14:27:51
well depending on how picky you are

put this into a batch file and change vbs.vbs to whatever you want to call it.
cscript "C:\myfile.vbs"

save this as a myfile.vbs, change "c:\testfile" to the name of your file.

Option Explicit
Dim fso,strFilename,strNi,strNiReplace,strTab,strTabR,objFile,oldContent,newContent

strFilename="c:\testfile.txt"
strNi="/n"
strNiReplace="newline"
strTab="/t"
strTabR="tab"

'Is it worth it at all
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
wscript.echo "file not found!"
wscript.Quit
end if

'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll

'Write file
newContent=replace(oldContent,strNi,strNiReplace,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write newContent

'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll

'Write file
newContent=replace(oldContent,strTab,strTabR,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write newContent
objFile.Close

::mike

http://www.computing.net/howtos/sho...


Report •

#3
October 10, 2013 at 17:55:01
I assume he's looking for the new line and tab character(s), not the word "new line" or "tab."

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
October 10, 2013 at 18:19:35
Yes I suppose that would be logical, I misunderstood. Apologies for wasting either of your times.

::mike

http://www.computing.net/howtos/sho...


Report •

#5
October 10, 2013 at 19:20:52
@Mike: hey, don't give up so easy! You had it, with just this change:
strNiReplace="newline"
strTabR="tab"

to:
strNiReplace=vbcrlf
strTabR=vbtab

Or, for newline, you may want either VBCR or VBLF


Report •

#6
October 10, 2013 at 21:27:30
Thanks a lot for all of you for helping me in getting solution
As nbrane said
It is working fine with the following
strNiReplace="newline"
strTabR="tab"
to:
strNiReplace=vbcrlf
strTabR=vbtab

Report •

#7
October 10, 2013 at 22:27:03
It might be better to mark nbrane as the best answer. I am glad that the compilation fixed the issue. 😊

::mike

http://www.computing.net/howtos/sho...


Report •

#8
October 11, 2013 at 05:41:51
I'm not sure I like the whole read/write to the file twice deal, but it does work. Powershell version looks like this:
(Get-Content "c:\testfile.txt" -ReadCount 0) | 
ForEach-Object { $_ -replace "\\n", "`n" } |
ForEach-Object { $_ -replace "\\t", "`t" } > "c:\testfile.txt"

How To Ask Questions The Smart Way


Report •

#9
October 11, 2013 at 06:33:01
✔ Best Answer
Thank you again nbrane and Razor2.3

slimmed version
Option Explicit
Dim fso,strFilename,strNi,strNiReplace,strTab,strTabR,objFile,oldContent,newContent,secContent

strFilename="c:\testfile.txt"
strNi="/n"
strNiReplace=vbcrlf
strTab="/t"
strTabR=vbtab

'Is it worth it at all
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(strFilename)=false then
wscript.echo "file not found!"
wscript.Quit
end if

'Read file
set objFile=fso.OpenTextFile(strFilename,1)
oldContent=objFile.ReadAll

'Write file
newContent=replace(oldContent,strNi,strNiReplace,1,-1,0)
secContent=replace(newContent,strTab,strTabR,1,-1,0)
set objFile=fso.OpenTextFile(strFilename,2)
objFile.Write secContent

objFile.Close

::mike

http://www.computing.net/howtos/sho...


Report •

#10
October 11, 2013 at 08:12:29
LOL

The title says /n; the verbage says backslash.
So maybe it's one of:

/n
\n
0d0a

or maybe not.

=====================
M2 Golden-Triangle


Report •

Ask Question