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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Spaces from a csv file 1

Status
Not open for further replies.

CSSQL

MIS
Jul 21, 2011
7
GB
Hello all
I need to remove some spaces from a csv and possbily a footer at the bottom. I've tried using some vbs script but none are working. Can anyone help.
This is the example of how the csv file looks:-
50229020 ,0000033139
43760028 ,0000039153
and this is how I want it to look.
50229020,0000033139
43760028,0000039153

Also remove this line at the bottom of the file:-
(11305 rows affected)
 


hi,

Please post the code that you have used.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi this is what I used

Dim sName
Dim fso
Dim fol

' create the filesystem object
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
' get current folder
Set fol = fso.GetFolder("C:\test1")
' go thru each files in the folder

For Each fil In fol.Files
' check if the file name contains underscore
If InStr(1, fil.Name, " ") <> 0 Then
' replace underscore with space
sName = Replace(fil.Name, " ", "")
' rename the file
fil.Name = sName
End If
Next
' echo the job is completed
WScript.Echo "Completed!
 


Are you CERTAIN that it is a SPACE CHARACTER?

Your code does replace a SPACE CHARACTER with nothing.

Your code comment states, " replace underscore with space"

???

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Your code is looking at the file's name, not the content of the file. Is this your intent?
 
Modify this code to point to a file you want to test on. If it works for you, incorporate it with the file looping code you already have.

Code:
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/ms974570.aspx[/URL]
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/yab2dx62.aspx[/URL]
'[URL unfurl="true"]http://www.regular-expressions.info/index.html[/URL]
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/6wzad2b2(v=vs.85).aspx[/URL]

Set objFSO = CreateObject("Scripting.FileSystemObject")

dim filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const fmtUnicode = -1, fmtASCII = 0, fmtDefault = -2

set re = new regexp
dim strInput
dim Matches
dim Match

dim myFile
'change the following file path to point to your file
myFile = "H:\test.csv"

re.IgnoreCase = true

Set filetxt = objFSO.OpenTextFile(myFile, ForReading, False, fmtDefault)

Dim arrFileLines()
i = 0
Do Until filetxt.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = filetxt.ReadLine

strInput = arrFileLines(i)
'wscript.echo(strInput)

re.Pattern = "\s"
re.Global = True

arrFileLines(i) = re.Replace(strInput, "")
'wscript.echo(arrFileLines(i))

i = i + 1
Loop

filetxt.Close

Set filetxt = objFSO.OpenTextFile(myFile, ForWriting, False, fmtDefault)
For j = LBound(arrFileLines) to UBound(arrFileLines)
  filetxt.WriteLine(arrFileLines(j))
Next
filetxt.Close

set filetxt = nothing
 
Thanks for all the help. Will give that script a try. No I wanting it to look at the content and not the filename. Sorry new to VB.
 
Thanks jges, that's worked. Thank you very much.
cssql
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top