-2

I'm trying to manipulate a simple data series set (input data) as listed below to get an output csv file as shown below. I have been trying really hard to work out where the bugs are and how to code this script, but could not work it out.

The main thing I'm trying to do is find a really fast code that can remove the dot from the currency FX prices only and not remove the dot from the date. The solution that was provided in this thread will take 56 hours to manipulate 6.6 million rows of data in just 1 file, which is a very long time to wait, as I have around 50 files to do, so this would take around 117 days to do this job using the code posted here. The code programmed by "Magoo" was amazingly fast, around 10 times faster than other codes posted. I did not realize that the way a batch script is coded can make a very large difference to the time it takes to complete my data manipulation job.

Would Magoo or someone else on this forum be able to modify Magoo's code (which is posted below) so that the code can remove the dot in the Currency prices, but not remove the dot in the date, please also see input data and output data listed below.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "filename1=%sourcedir%\q65529783.csv"
SET "outfile=%destdir%\outfile.csv"

(
FOR /f "usebackqtokens=1-7delims=," %%a IN ("%filename1%") DO (
 FOR /f "tokens=1-4delims=." %%s IN ("%%d.%%e") DO SET /a value=(%%s*100000^)+1%%t-(%%u*100000^)-1%%v&SET /a value=10*value+1
 ECHO %%a,%%b,%%c,%%d,%%e,%%f,!value!
)
)>"%outfile%"

GOTO :EOF

INPUT DATA:

2003.05.04,21:00,118.940,118.952,118.940,118.952,13
2003.05.04,21:01,118.961,118.967,118.958,118.967,13
2003.05.04,21:02,118.972,118.972,118.955,118.955,18
2003.05.04,21:03,118.953,118.961,118.949,118.949,21
2003.05.04,21:04,118.953,118.953,118.946,118.946,8

OUTPUT DATA SHOULD LOOK LIKE THIS:

2003.05.04,21:00,118940,118952,118940,118952,13
2003.05.04,21:01,118961,118967,118958,118967,13
2003.05.04,21:02,118972,118972,118955,118955,18
2003.05.04,21:03,118953,118961,118949,118949,21
2003.05.04,21:04,118953,118953,118946,118946,8
  • Does nothing? Firstly I suggest you use powershell, not batch script. If you insist on using batch script I first suggest you put a `pause` in it to keep the dos window open so you can see whatever error it is showing. – Nick.Mc Jan 08 '21 at 02:39
  • 1
    Open cmd.exe type `if /?`. the operators you are attempting to use are not valid batch syntax. Additionally, you fail to expand the value variable when testing. See [this question](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) for more. Lastly, avoid reusing the same `for` metavariable within a nested for loop. There is no need for it, and you can quickly lose sight of what the current value of a metavariable is. – T3RR0R Jan 08 '21 at 02:53
  • See also `For /?` for correct usage of the for command options. One more thing, what exactly do you expect this command: `SET /a value = MAX(10,value)` to do? - It's not valid batch syntax for integer arithmetic. If it's something akin to [this](https://www.w3schools.com/jsref/jsref_max.asp) you'll have to use another if condition to determine which value to use. – T3RR0R Jan 08 '21 at 03:00
  • Really sorry, but I made a key mistake and therefore had to revise my question and input and output data – Rocket130713 Jan 08 '21 at 10:57
  • SO is not a free code writing service. – Liam Jan 08 '21 at 13:50

1 Answers1

1

With just some very basic changes to the vbscript part of the code I provided in my last answer, you could easily use the same batch file code I gave there. I'm not really sure why you decided to stick with batch files and the poor set /a implementation anyhow.

<!-- :
@SetLocal EnableExtensions
@If Exist "usdjpy1.csv" ((For /F UseBackQTokens^=1-6Delims^=^,EOL^= %%G In (
            "usdjpy1.csv")Do @For /F %%M In (
            '%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" "%%J" "%%K"'
        )Do @Echo(%%G,%%H,%%I,%%J,%%K,%%L,%%M)>"output.csv")
@Exit /B
-->
<Job><Script Language="VBScript">
i = Int(Eval(WScript.Arguments(0)-WScript.Arguments(1)))
If i = 0 Then i = 1 Else If i < 10 Then i = 10
WScript.Echo i 
</Script></Job>

[Edit1 /]

Here's an update, based upon your changed criteria:

<!-- :
@SetLocal EnableExtensions
@If Exist "usdjpy1.csv" ((For /F UseBackQTokens^=1-6Delims^=^,EOL^= %%G In (
            "usdjpy1.csv")Do @For /F %%M In (
            '%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" "%%J" "%%K"'
        )Do @Echo(%%G,%%H,%%I,%%J,%%K,%%L,%%M)>"output.csv")
@Exit /B
-->
<Job><Script Language="VBScript">
i = Round(Eval((WScript.Arguments(0)-WScript.Arguments(1))*1000),0)
If i = 0 Then i = 1 Else If i < 10 Then i = 10
WScript.Echo i 
</Script></Job>

[Edit2 /]

I have added some additional code to allow for the removal of the decimal points, (in essence, I've passed all four fields to the vbscript portion, and in that multiplied each by 1000, and converted to integers)

<!-- :
@SetLocal EnableExtensions
@If Exist "usdjpy1.csv" ((For /F UseBackQTokens^=1-6Delims^=^,EOL^= %%G In (
            "usdjpy1.csv")Do @For /F %%M In (
            '%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf" "%%I" "%%J" "%%K" "%%L"'
        )Do @Echo(%%G,%%H,%%M)>"output.csv")
@Exit /B
-->
<Job><Script Language="VBScript">
Set objArgs = WScript.Arguments
For Each dblArg In objArgs
    fieldVals = fieldVals&Int(Eval(dblArg * 1000))&","
Next
i = Round(Eval((WScript.Arguments(1)-WScript.Arguments(2))*1000),0)
If i = 0 Then i = 1 Else If i < 10 Then i = 10
WScript.Echo fieldVals & i 
</Script></Job>

[Edit3 /]

Given your completely changed question again, and given that you appear not to want another WSH based solution, here's a quick alternative which, as shown in your new data, simply removes the decimal points in your floating point values, which all appear to be written to three decimal places.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "sourcedir=u:\your files"
Set "destdir=u:\your results"
Set "filename1=%sourcedir%\q65529783.csv"
Set "outfile=%destdir%\outfile.csv"

If Exist "%filename1%" ((SetLocal EnableDelayedExpansion
        For /F UseBackQTokens^=1-6*Delims^=^,EOL^= %%G In ("%filename1%") Do (
            Set "}=%%I,%%J,%%K,%%L"
            Echo(%%G,%%H,!}:.=!,%%M)
        EndLocal)>"%outfile%")
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks Compo for your help. Really appreciate it. Please forgive me for not accepting answers as I did not know how this forum works. I also made a mistake with regards to the Input data, I have just fixed this up now. I forgot to include the decimal in the input data. Does this make any difference to your code above? – Rocket130713 Jan 08 '21 at 03:57
  • Yes it made a difference, @Rocket130713, on this occasion, I have adjusted the posted answer code accordingly. I have not adjusted the code however, to remove decimal points from your field values in the output file, because I do not know whether you had failed to update that part of your post or not. If you need to do that, you should have identified that prior to my posted answer, which would need more adjustment to cater for that. – Compo Jan 08 '21 at 04:09
  • Really sorry Compo for my mistake about forgetting the decimals in the input data. What should I do now, as I'm really stuck? I really need the Output data file to have the decimals removed for Columns 3, 4, 5 and 6. But I don't know how to code this. Would you please be able to help me achieve this Compo? I truly apologize for the inconvenience, I should have got this correct right at the outset. Regards, Rodney. – Rocket130713 Jan 08 '21 at 04:47
  • @Rocket130713, I have made a further change to modify the four field values as requested. – Compo Jan 08 '21 at 05:01
  • Really sorry, but I made a key mistake and therefore had to revise my question and input and output data – Rocket130713 Jan 08 '21 at 10:57
  • Really sorry to upset you Compo. Your coding and methodology is brilliant. Thank you so much for helping me, I really appreciate this. – Rocket130713 Jan 09 '21 at 07:02