Link to home
Start Free TrialLog in
Avatar of luketr
luketr

asked on

Batch file to edit file

I have a file that is generated from a system automatically and within the file it has financial figures with a decimal point in.
For example
The file produces the value £56.40
As 000000000000000564
I need the file to produce the figure as 0000000000000005640

The file produces the value £31.43
000000000000031.43
I need the file to produce the figure as 0000000000000003143

So if the value is a whole number it drops the zero of the end.
If the value is not a round number it inserts a decimal point and requires an additional zero before the value.

The figure will always start from column line 2 and 50 spaces in and end at 68 spaces in.
The file could have 100s of lines.

I need a batch file to edit this file automatically.

Any help would be appreciated. It need to a windows bat file not PowerShell.

I have attached the file.
ifc.txt
Avatar of Qlemo
Qlemo
Flag of Germany image

You are requesting that the line will be 1 char longer after that, right?
Avatar of luketr
luketr

ASKER

Yes that's correct
Your main problem doing it in batch is you have & in descriptions and potential for other characters in their to cause issues.  Is a VBScript OK, you can call it from batch with cscript //nologo fixfile.vbs or the like?

Steve
Hey, that looks like COBOL output... :-)

~bp
You are requesting that the line will be 1 char longer after that, right?
I don't think the answer to this is "yes".  As I look at the data I see:

000000000000000084
000000000000000014
0000000000002100.3
0000000000002100.3
000000000000350.05
000000000004014.11
000000000004014.11

I think the desired result of this would be the following:

000000000000008400
000000000000001400
000000000000210030
000000000000210030
000000000000035005
000000000000401411
000000000000401411

Wouldn't it? Values with no "." in them should get "00" added to the end. Values with "." in them, and only one position to the right of it get the "." removed, and a "0" added to the right. Values with a "." in them, and two digits to the right get the "." removed, and one "0" added to the left.

This way we still have a fixed length field, but with an implied two decimal places.

~bp
Avatar of luketr

ASKER

Yes you are correct, thank you for correcting me
Sorry, but dragon-it is correct - no cmd processing because of the ampersand. It just cannot be done without external tools. I would take his advice and switch to VBS, since PS isn't an option for you.
How do you know you would have "ampersand"?
This is the VBS I came up with:


dim objFSO, objFileIn, objFileOut
dim args,strFileName,strLine,strFix

set args = wscript.Arguments

if args.count <2 then
  wscript.echo "Usage: cscript //nologo fixit.vbs filename.txt newname.txt"
  wscript.quit
end if

strFileName=args(0)
strNewName=args(1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileIn  = objFSO.OpenTextFile(strFileName)
Set objFileOut = objFSO.CreateTextFile(strNewName)

Do while not objFileIn.AtEndOfStream
  strLine=objFileIn.ReadLine

  if len(strLine) > 50 then
    strFix=mid(strLine,50,18)
    if mid(strFix,16,1)="." then
      strFix="0" & left(strFix,15) & mid(strFix,17,2)
    elseif mid(strfix,17,1)="." then
      strFix=left(strFix,16) & mid(strFix,18,2) & "0"
    else 
      strFix=mid(strFix,3) & "00"
    end if
     objFileOut.writeline left(strLine,49) & strFix & mid (strLine,68)
     wscript.echo strFix
 else
    ' Send any short line out as they are
    objFileOut.writeline strLine
    wscript.echo strLine
  end if

Loop 

objFileIn.Close
objFileOut.Close

Open in new window


It reads the filenames from the command line, i.e.

cscript //nologo fixit.vbs ifc.tct out.txt

It shows the changed lines as it makes them, remove the wscript.echo to hide that.

Steve
Avatar of luketr

ASKER

Hi Steve,

That worked brilliantly!

However I have had to remove 1 leading zero before the financial figure and I have tried to edit your script and haven't had much success.

So the file is generated with 17 characters before your script edits the file.

I have attached the amended source file.

I can log this as another question to give you more points if you require.


Thanks

Luke
Here is fine, should be just a case of removing 1 from all the numbers needed that are effected, will see.


Steve
Try this...  seems it starts at same place but 17 digits?  If so have changed so starts at position 50 still but gets 17 chars, not 18, and last line adds rest of line from one character less

dim objFSO, objFileIn, objFileOut
dim args,strFileName,strLine,strFix

set args = wscript.Arguments

if args.count <2 then
  wscript.echo "Usage: cscript //nologo fixit.vbs filename.txt newname.txt"
  wscript.quit
end if

strFileName=args(0)
strNewName=args(1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileIn  = objFSO.OpenTextFile(strFileName)
Set objFileOut = objFSO.CreateTextFile(strNewName)

Do while not objFileIn.AtEndOfStream
  strLine=objFileIn.ReadLine

  if len(strLine) > 49 then
    strFix=mid(strLine,50,17)
    if mid(strFix,16,1)="." then
      strFix="0" & left(strFix,15) & mid(strFix,17,2)
    elseif mid(strfix,17,1)="." then
      strFix=left(strFix,16) & mid(strFix,18,2) & "0"
    else 
      strFix=mid(strFix,3) & "00"
    end if
     objFileOut.writeline left(strLine,49) & strFix & mid (strLine,67)
     wscript.echo "[" & strFix & "]"
 else
    ' Send any short line out as they are
    objFileOut.writeline strLine
    wscript.echo strLine
  end if

Loop 

objFileIn.Close
objFileOut.Close

Open in new window

Avatar of luketr

ASKER

I have used this script

dim objFSO, objFileIn, objFileOut
dim args,strFileName,strLine,strFix

set args = wscript.Arguments

if args.count <2 then
  wscript.echo "Usage: cscript //nologo fixit.vbs filename.txt newname.txt"
  wscript.quit
end if

strFileName=args(0)
strNewName=args(1)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFileIn  = objFSO.OpenTextFile(strFileName)
Set objFileOut = objFSO.CreateTextFile(strNewName)

Do while not objFileIn.AtEndOfStream
  strLine=objFileIn.ReadLine

  if len(strLine) > 48 then
    strFix=mid(strLine,48,17)
    if mid(strFix,16,1)="." then
      strFix="0" & left(strFix,15) & mid(strFix,17,2)
    elseif mid(strfix,17,1)="." then
      strFix=left(strFix,16) & mid(strFix,18,2) & "0"
    else
      strFix=mid(strFix,3) & "00"
    end if
     objFileOut.writeline left(strLine,48) & strFix & mid (strLine,65)
     wscript.echo strFix
 else
    ' Send any short line out as they are
    objFileOut.writeline strLine
    wscript.echo strLine
  end if

Loop

objFileIn.Close
objFileOut.Close

Which has nearly worked just the values that have a decimal point are not quite correct.

It removes the decimal point but doesn't add an additional zero at the end.


Before 000000000000001.2

After

000000000000000012

required to be

0000000000000000120

I have attached the before file and the file after using the script file.
before.txt
after.txt
Avatar of luketr

ASKER

I have changed the starting position to 48 and have put that in the script too.
will look in a bit, kids tea, bed, bath time etc. now...
best we go back to original one i did, the 50way bits will need amending as now, and the last bit to pickup rest of line.

the bit still to change is the middle part, didnt look before as thought had worked offset from right but didn't

will look when on pc.

steve
Avatar of luketr

ASKER

If you can get this working for me i will be over the moon.

Thanks

Steve
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of luketr

ASKER

You are a legend, thank you so much for doing this.  Really appreciate it and works perfectly.

Thanks

Luke
No problem... lot more reliable in VBScript for that than batch.