-1

I have written below in a batch file to copy a csv file in a folder to txt file and move into our import folder

copy "\\jarvisfs1\groups\InforIFC\Payroll\NEW\*.csv" "\\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"
------------------------------------------------------------------------

The text file that is generated no longer seems to have the columns seperated when trying to import into sql or bi Tool.

But when I use the below command using a vba script the columns are fine within Sql or BI Tool

Dim objFSO, objFile, objFileTSV
Dim strLine, strNewLine
Dim FileNameLength, LineLength, NewFileName, Linepos, Quote, QuoteCount, 
TotalFilesConverted
set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("scripting.filesystemobject")
strCurPath = objFSO.GetAbsolutePathName(".")
TotalFilesConverted = 0
For Each objFile In objFSO.getfolder(strCurPath).Files

            If UCase(Right(objFile.Name, 4)) = ".CSV" Then
                            Result = WshShell.Popup("Converting " & 
  objFile.Name & "...", 3,"" , 64)
                            FileNameLength = Len(objFile.Name)-4
                            NewFileName = "EPRO.txt"
                            Set objFile = objFSO.OpenTextFile(objFile, 1)
                            Set objFileTSV = 
  objFSO.CreateTextFile(NewFileName)

                            Do Until objFile.AtEndOfStream
                                            strLine = objFile.ReadLine
                                            LineLength = Len(strLine)
                                            Linepos =1
                                            strNewLine =""
                                            Quote = False
                                            QuoteCount = 0


   Do While Linepos <= LineLength
                                                            If Mid(strLine, 
   Linepos, 1) = "," And Not Quote Then

    strNewLine = strNewLine + vbTab

   Quote = False
                                                            ElseIf 
  Mid(strLine, Linepos, 1) = Chr(34) Then

 QuoteCount = QuoteCount + 1

 If QuoteCount = 2 And Linepos <> LineLength Then

 If Mid(strLine, Linepos, 2) = Chr(34) & Chr(34) Then

 strNewLine = strNewLine + Chr(34)

  Linepos = Linepos + 1

 Quote = True

  QuoteCount = 1

  Else

  Quote = False

  QuoteCount = 0

  End If

  Else

  Quote = True

  End If
                                                           ElseIf QuoteCount 
  = 0 And Mid(strLine, Linepos, 1) = "-" Then

  strNewLine = strNewLine + Mid(strLine, Linepos, 1) + "0"
                                                            Else

  strNewLine = strNewLine + Mid(strLine, Linepos, 1)
                                                            End If
                                                            Linepos = 
   Linepos + 1
                                            Loop


                                            objFileTSV.WriteLine strNewLine
                                            strNewLine = ""
                            Loop
                            objFile.Close
                            TotalFilesConverted = TotalFilesConverted +1
                            objFileTSV.Close

            End If
    Next

    MsgBox CStr(TotalFilesConverted) + " Files Converted from CSV to TXT."

But using the above command the columns are fine within the text file. The issue I have with the 2nd script is I cannot get the script to run automatically using Task Schu, where as the batch file I can get to run automatically

How it needs to appear

How it appears after the new script is ran

Alex
  • 1
  • 6
  • You just edited your entire question into a completely different question. Please don't do that. The answers don't make sense anymore – Buh Buh Nov 28 '17 at 12:59

2 Answers2

1

In Batch it is easy enough to just remove the quotes and commas and replace the commas with a whitespace

We can therefore just copy the CSV files to a tempfile, then we re-write the content to your actual text file after we replace all the quotes and commas.

@echo off
setlocal enabledelayedexpansion

if exist "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt" (del /Q "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt")
copy "\jarvisfs1\groups\InforIFC\Payroll\NEW*.csv" "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt"
for /f "delims=" %%f in(\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt) do (
set var=%%f
set var=!var:"=!
set var=!var:,= !
echo !var! >> "\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"
)
del /Q "\jarvisfs1\groups\InforIFC\Payroll\NEW\tempfile.txt"

Finally notice how the replace function works. the character you want to replace comes before the = and the character you want to replace it with comes after the = So if you want to replace + with - in a string then you will do !var:+=-! or %var:+=-% if you do not setlocal enabledelayedexpansion to understand exactly how delayedexpansion works, you can run setlocal /? from cmd.exe

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Gerhard, being fairly new to this, my programming knowledge is a little limited, so apologies if this is a stupid question.. Are you saying I can run the above command in a batch file instead of my original one batch file – Alex Nov 27 '17 at 11:35
  • @AlexAllen yes. You can just replace the current one with this one. It will do what you want it to do. If you want me to add more comments to explain the code, I will do so. – Gerhard Nov 27 '17 at 11:51
  • The command removed the double-quotes but not the , the data now appears like Hotel Code,Hotel Name,Dpt Code,Date,Month,Nominal,Revenue 001,M. Chester Abbots Well,01,2017-10-01,Oct,012510,791.68. Also if you have the time to add an explanation that would be great.. Thank you in advance – Alex Nov 27 '17 at 11:51
  • Thanks for the explanation that made perfect sense... The code worked but I'm still having an issue as the file doesn't separate out the columns after the script is run.. Its as if the columns have been merged – Alex Nov 27 '17 at 13:52
  • @AlexAllen was that part of the initial question? – Gerhard Nov 27 '17 at 14:58
  • no it wasn't as I initially thought that the reason why it was grouping the columns when converting to text file was due to the double-quotes and commas. But After running your script i have come to realize this isnt the case. I'm basically trying to convert a csv to txt and keeping the existing columns and format in tact. – Alex Nov 27 '17 at 15:04
  • @AlexAllen edit you question and show a snippet of the result as well as the expected result. – Gerhard Nov 27 '17 at 15:22
  • does the above make sense? – Alex Nov 28 '17 at 10:12
  • @AlexAllen But now the question changed completely? Where does the `vba` script come from? – Gerhard Nov 28 '17 at 10:14
  • Apologies, as I initially thought the reason why it wasn't reading the columns was because it was adding the double quotes and comma when transferring to txt. But after running the script you gave me yesterday I realized that not the reason. I'm bascically trying to get a script that will convert csv to txt keeping the columns that I can run automatically using Task Scheduler – Alex Nov 28 '17 at 11:09
  • @AlexAllen can you then please add a snippet of what the output is currently and what you prefer it to be? – Gerhard Nov 28 '17 at 11:19
  • I have added both images for you to see what I mean. I hope it makes senses – Alex Nov 28 '17 at 17:07
  • @AlexAllen Ok. So let's change the spaces for tabs or revert to commas. So in my script i gave you replace this one with `set var=!var:,= !` by channg the space before ! With a tab. Id that doea not work. Rem tbat entire line and see if it places the collumns back for you. Your editor needs either tabs or commas to aplit between columns. – Gerhard Nov 28 '17 at 17:13
  • Sorry for the spelling. My phone has extremely amaal buttons. – Gerhard Nov 28 '17 at 18:02
  • that worked. Thank you for you all your help.. Dont worry i understood what you meant.. Also you explanation made senses as well.. Until next time my friend.. Hope you have a good Christmas and new years – Alex Nov 29 '17 at 09:13
  • Me again.. This script works a dream until it merges all the csv's into a text. It add an additional row after the last row data. Do you know how I stop this from happening – Alex Dec 07 '17 at 16:12
0

I don't think that command adds in the quotes and commas. They were there to begin with. Open your csv in Notepad and see them yourself.

So you just need some way of removing them. Although that is technically posible in cmd, I wouldn't want to attempt it. So here is a solution in powershell:

(Get-Content "\jarvisfs1\groups\InforIFC\Payroll\NEW*.csv") | ForEach-Object {
    $_.Replace(",", "").Replace("`"", "")
} | Set-Content "\jarvisfs1\groups\InforIFC\Payroll\NEW\EPRO.txt"
Buh Buh
  • 7,443
  • 1
  • 34
  • 61