I have the following file, where each new line is created with the characters ~\r\n<CR><LF>
.
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?