Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove a carriage return from a text document

Status
Not open for further replies.

collierd

MIS
Dec 19, 2001
509
DE
Hello

Does anybody know if the following is possible:

I have a number of text files generated monthly (i.e. A123450001.TXT)
Each of these text files are generated from a data extract into text format
Problem is, it keeps adding an extra carriage return at the end
Can vbscript be used to remove the last line only from a txt file?
If so, how

Thanks

Damian.
 
We did this in vb to remove blank lines from the end of a file - maybe you can make some kind of use out of it:

Open "x:\extracts\bmpc\BRANCH01\subgen2.dat" For Input As #InputFile
Open "x:\extracts\bmpc\BRANCH01\subgen.dat" For Output As #OutputFile

Do While Not EOF(InputFile)
strGatewayLine = Space(0)
Line Input #InputFile, strGatewayLine
Form1.Label2.Caption = Left(strGatewayLine, 20)

If Trim(strGatewayLine) <> &quot;&quot; Then
Print #OutputFile, strGatewayLine
End If

DoEvents
Loop

Close #InputFile
Close #OutputFile
 
On Error Resume Next
Dim WshShell
Dim FSO
Dim WshSysEnv
Dim fscDrvEnv
Dim alluprogStr
Dim file2Open
Dim destFld
Dim ndataDir


Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
Set FSO = Wscript.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set WshSysEnv = WshShell.Environment(&quot;SYSTEM&quot;)
fscDrvEnv = WshSysEnv(&quot;FSCDrive&quot;)

userprofStr = WshShell.ExpandEnvironmentStrings(&quot;%USERPROFILE%&quot;)


file2Open = fscDrvEnv & &quot;\LOTUS\Notes\V5.08_DE\_SCRIPT\_FILES\notes.ini&quot;
ndataDir = &quot;\fscnotesdata&quot;
file2Create = userprofStr & ndataDir & &quot;\notes.ini&quot;

'Wscript.Echo file2Create
'Wscript.Echo file2Open

If FSO.FileExists(file2Open) Then
Set tsIni = FSO.OpenTextFile(file2Open)
Else
'Wscript.Echo &quot;cant find source notes.ini&quot;
End If

Set newInifile = fso.CreateTextFile(file2Create, True)

newInifile.WriteLine &quot;[Notes]&quot;
Do While Not tsIni.AtEndOfStream

sLine = Trim(tsIni.ReadLine)
If sLine <> &quot;&quot; Then
'will remove empty lines
newInifile.WriteLine sLine
End If


Loop

newInifile.Close
tsIni.Close


or try.....
hmm reading txt file into a dictionary object

then do something like
If objDic.Item(objDic.Count - 1) = &quot;&quot; Then
objDic.Remove (objDic.Count - 1)...that will remove the
End If
last line,,,

 

Set WshShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
Set FSO = Wscript.CreateObject(&quot;Scripting.FileSystemObject&quot;)


file2Open = &quot;c:\test.txt&quot;
file2Create = &quot;c:\test2.txt&quot;

'Wscript.Echo file2Create
'Wscript.Echo file2Open

If FSO.FileExists(file2Open) Then
Set tsIni = FSO.OpenTextFile(file2Open)
Else
'Wscript.Echo &quot;cant find source notes.ini&quot;
End If

Set newInifile = fso.CreateTextFile(file2Create, True)


Do While Not tsIni.AtEndOfStream

sLine = Trim(tsIni.ReadLine)
If sLine <> &quot;&quot; Then
'will remove empty lines
newInifile.WriteLine sLine
End If


Loop

newInifile.Close
tsIni.Close

the above is tested and does the job
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top