0

I have the following file, where each new line is created with the characters ~\r\n<CR><LF>.

Sample test file

I want to remove all occurrences of ~\r\n<CR><LF> and replace with "" (empty string) so that all the file data appears on a single line. To do this, I wrote a script in VBScript (replace.vbs) and I run it in the commandline with cscript replace.vbs "~\r\n<CR><LF>" "" - the first parameter is the original characters I want replace and the second parameter is the new text to write into the file.

My script can successfully replace regular text - for example, if I pass only <CR><LF> as the string to replace, it will work as expected. But my issue is when I pass the full ~\r\n<CR><LF>, it does not modify the file at all. I think it has something to do with the ~\r\n characters being incorrectly passed into the command.

The following is my script:

Const ForReading = 1
Const ForWriting = 2

strOldText = Wscript.Arguments(0)
strNewText = Wscript.Arguments(1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "D:\FTP\Private\EDI\KleinschmidtTemp\"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
   if instr(objFile.Name,"BRO") <> 0 and instr(objFile.Name,".edi") <> 0 then
    Set objFileToRead = objFSO.OpenTextFile(objStartFolder + objFile.Name, ForReading)

    strText = objFileToRead.ReadAll
    objFileToRead.Close
    strNewText = Replace(strText, strOldText, strNewText)

    Set objFileToEdit = objFSO.OpenTextFile(objStartFolder + objFile.Name, ForWriting)
    objFileToEdit.WriteLine strNewText
    objFileToEdit.Close

    objFSO.MoveFile objStartFolder + objFile.Name, "D:\FTP\Private\EDI\Kleinschmidt\Outgoing\" + objFile.Name
   end if
Next

EDIT: So I found out that for our production files, <CR><LF> will not be plaintext - they are actually non printable ASCII control characters. So I made the following change to my script, I used the Chr() function to get these characters and properly replace them.

Const ForReading = 1
Const ForWriting = 2

'strOldText = Wscript.Arguments(0)
'strNewText = Wscript.Arguments(1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "D:\FTP\Private\EDI\KleinschmidtTemp\"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
   if instr(objFile.Name,"BRO") <> 0 and instr(objFile.Name,".edi") <> 0 then
    Set objFileToRead = objFSO.OpenTextFile(objStartFolder + objFile.Name, ForReading)

    strText = objFileToRead.ReadAll
    objFileToRead.Close
    'strNewText = Replace(strText, strOldText, strNewText)
    strNewText = Replace(strText, Chr(10), "")      ' 10 is ASCII code for LF, line feed
    strNewText = Replace(strNewText, Chr(13), "")   ' 13 is ASCII code for CR, carriage return

    Set objFileToEdit = objFSO.OpenTextFile(objStartFolder + objFile.Name, ForWriting)
    objFileToEdit.WriteLine strNewText
    objFileToEdit.Close

    'objFSO.MoveFile objStartFolder + objFile.Name, "D:\FTP\Private\EDI\Kleinschmidt\Outgoing\" + objFile.Name
   end if
Next

I still have one issue though... When I open the resulting file, everything is on a single line which is what I want. But my script does not remove the <CR><LF> at the very end of the file, so there is an extra blank line. How can I fix this?

New resulting file

MERNboy
  • 125
  • 14
  • Judging by the sample file the `` is just literal text so to replace `\r` and `\n` use `vbCr` and `vbLf` or `vbCrLf` or `vbNewLine`. See [\r\n, \r and \n what is the difference between them?](https://stackoverflow.com/a/15433225) – user692942 Apr 28 '21 at 08:23
  • @user692942 Please see my edit, I found out `` is not actually plaintext. Not sure why my question was closed as I did just edit it :( – MERNboy Apr 28 '21 at 13:46
  • Which is the same as `vbCr` and `vbLf` respectively. You need to understand the different ways of specifying carriage return and linefeeds in VBScript, hence the duplicate and why the question was closed. – user692942 Apr 28 '21 at 16:21
  • @user692942 Thanks, I understand that but it seems in both my question and the question you linked that these methods do not completely remove all the line breaks. In my case, the very last line break is still there. Is this normal? – MERNboy Apr 28 '21 at 17:40

3 Answers3

1

This is far easier to do from the command line or in a batch file. But since you are using VBScript, here's what you need to do.

First, do away with the script arguments. Passing control characters to a script argument would be a nightmare to get working correctly. Instead, we'll hardcode the search into the script. If you absolutely need to do this using arguments, comment and I'll update the script to do that but it gets a lot more confusing.

Const ForReading = 1
Const ForWriting = 2

' strOldText = Wscript.Arguments(0)
strOldText = "~" & vbCrLf & "<CR><LF>"
' strNewText = Wscript.Arguments(1)
strNewText = ""

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "D:\FTP\Private\EDI\KleinschmidtTemp\"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
For Each objFile in colFiles
   if instr(objFile.Name,"BRO") <> 0 and instr(objFile.Name,".edi") <> 0 then
    Set objFileToRead = objFSO.OpenTextFile(objStartFolder + objFile.Name, ForReading)

    strText = objFileToRead.ReadAll
    objFileToRead.Close
    strNewText = Replace(strText, strOldText, strNewText)

    Set objFileToEdit = objFSO.OpenTextFile(objStartFolder + objFile.Name, ForWriting)
    objFileToEdit.WriteLine strNewText
    objFileToEdit.Close

    objFSO.MoveFile objStartFolder + objFile.Name, "D:\FTP\Private\EDI\Kleinschmidt\Outgoing\" + objFile.Name
   end if
Next

EDIT

In response to the edit, you are adding the extra line to the file when you write it back. You're using:

objFileToEdit.WriteLine

The WriteLine method appends a new line character to the end of your string. Just use Write instead.

objFileToEdit.Write strNewText
Nilpo
  • 4,675
  • 1
  • 25
  • 39
  • Thanks for your answer. I think I did what you described in a slightly different way. Would you mind looking at my edit and see if you can help? I solved it for the most part but I still have one small issue... – MERNboy Apr 28 '21 at 13:38
  • Basically just taken what [I said 1 hour earlier](https://stackoverflow.com/questions/67286461/removing-r-n-characters-in-a-file?noredirect=1#comment118950662_67293747). – user692942 Apr 28 '21 at 16:23
  • @MERNboy I have responded to your edit. – Nilpo Apr 28 '21 at 21:10
  • 1
    @Nilpo Thanks, this was the solution I was looking for! – MERNboy Apr 29 '21 at 13:07
0

I would try "~" & vbCrLF & "" as the search. vbCrLf is the constant for the new line carriage return combo.

  • Actually it should probably be `"~" & vbCrLf & ""`, but it would need to run inside the script file because you can't pass `vbCrLf` on the command line only VBScript understands it. – user692942 Apr 28 '21 at 08:25
-2

You can do this in Notepad++, by turning on the 'Extended' Search Mode on in the find in replace dialog box. replace \r\n with \n.

enter image description here

It also appears you are doing this from a Window's machine, and if you have Cygwin, or git bash on your system you can also use sed, for example:

sed -i sed 's/\r$//' input

If you don't want to edit the file in place, and test the results exclude the -i flag.

matt_linker
  • 127
  • 1
  • 4
  • 1
    Thanks for your help. I know of this replace functionality in Notepad++, but I don't want to manually edit all files. I would like to automate this process with my script since there will be multiple files a day at irregular times. – MERNboy Apr 27 '21 at 16:28