change values in a txt file

August 18, 2009 at 06:05:52
Specs: Windows XP
I woudl like to have a script that:
1. copies a file to another file & adds date to the end of it.
2. then opens up a text file and if the 4th field is = 0 and 5th field is greater than zero and 6th field = word TON, then multiply 4th field by 2000 and replace 4th field value with new calc value. then divide 5th value by 2000 and replace that value and replace the word TON with the word POUND. do this for the entire file.
close txt file and save


See More: change values in a txt file

Report •


#1
August 18, 2009 at 08:19:36
Is the file to be copied a text file? Otherwise you can't add a date (to-day date and which format?).

Is the text file to be processed the same previously copied or another?

How are the fields separated (by commas, spaces...)?

You state a condition to perform the process is the 4th field to be equal zero then multiply by 2000... but 0 * 2000 is just 0!

Revise carefully what you posted.


Report •

#2
August 18, 2009 at 16:00:24
sorry about that..
The file to be copied is a text file and I am just making a copy of the original to keep it as a reference point. I dont care which of the files I change...the orginal or the copy. the fields are separated by comma.
It should have read...
if the 4th field is = 0 and 5th field is greater than zero and 6th field = word TON, then multiply 3rd field by 2000 and replace 3rd field value with new calc value. then divide 5th value by 2000 and replace that value(I need to go out 5 decimal places, ex.. 0.12345..is that possible rounding up) and replace the word TON with the word POUND in the 6th field. do this for the entire file when the 3 conditions are met.
close txt file and save


Report •

#3
August 19, 2009 at 03:04:08
Well, but unfortunately NT batch script offers basic integer arithmetics only so more info is needed about numeric fields format, i.e.

is the decimal part fixed in extension and how many digits or always variable?

As I said in batch there are no real numbers so any computation must be handled from scratch.


Report •

Related Solutions

#4
August 19, 2009 at 06:36:11
the 3rd field could be 6.0 or 12.50 the decimal is in the text file. the decimal is normall 2 positions like the 12.50 but when its an even number its just 6.0.
The 5th field is always at 5 decimal places out(0.12345 but it could be 153.12345 or 20.12345 the decimal is displayed in the text file

Report •

#5
August 19, 2009 at 07:23:34
Thanks for the info and your patience. I'll try to give you the script but you have to be patient a while as it is not an easy job.

See you as soon (with the script obviously), keep in touch.


Report •

#6
August 19, 2009 at 07:28:24
Thanks IVO... your help is appreciated.

Report •

#7
August 20, 2009 at 04:05:34

WARNING: The below script has a flaw! I'll try to correct it as soon as possible.

Here the script, I tested it carefully and the code worked fine, but please check with care the math as I run a limited set of conditions. To run at prompt type

mypro " Your_File_IN" "Your_Copy"

Example: mypro "C:\My Data\myinput.txt" "D:\Myinput.txt"

Don't miss the double quotes. Report any issue.


:: MYPRO.BAT Usage mypro "File_IN" "File_OUT"
@echo off & setlocal EnableDelayedExpansion

echo.
if not exist %1 (echo.  File "%~f1" not found & goto :EOF)

echo.  "%~f1" copied to "%~f2"
echo.  "%~f1" being processed...
echo.   DONE.

type nul > "%~dpn1.new"
copy %1 %2 > nul
echo.[Date] %date% >> %2

for /F "tokens=1-6,* delims=," %%a in ('type %1') do (
  set fld1=%%a
  set fld2=%%b
  set fld3=%%c
  set fld4=%%d
  set fld5=%%e
  set fld6=%%f

  set cnt=0
  set fld=!fld4:.=!& if !fld! equ 0 set /A cnt+=1
  set fld=!fld5:.=!& if !fld! gtr 0 set /A cnt+=1
  if "!fld6!"=="TON" set /A cnt+=1

  if !cnt! equ 3 (
    set fld6=POUND
    call :NORM !fld3:.=,! fld3 *
    call :NORM !fld5:.=,! fld5 /
  )
  echo.!fld1!,!fld2!,!fld3!,!fld4!,!fld5!,!fld6!,%%g>> "%~dpn1.new"
)
del %1
ren "%~dpn1.new" "%~nx1"
goto :EOF [Return_To_OS]

:NORM
  if "%1"=="[Date]" goto :EOF
  set up=
  set int=%1& if %1 equ 0 set int=
  set dec=%2& if "!dec:~1,1!"=="" set dec=%20
  set int=%int%%dec%
  if %4==/ set int=!int!000
  set /A int%4=2000
  if %4==* (
    if !int! lss  10 set int=0!int!
    if !int! lss 100 set int=0!int!
    set %3=!int:~0,-2!.!int:~-2!
  ) else (
    if !int:~-3! gtr 500 set up=ON
    set int=!int:~0,-3!
    if defined up set /A int+=1
    if !int! lss     10 set int=0!int!
    if !int! lss    100 set int=0!int!
    if !int! lss   1000 set int=0!int!
    if !int! lss  10000 set int=0!int!
    if !int! lss 100000 set int=0!int!
    set %3=!int:~0,-5!.!int:~-5!
  )
goto :EOF [End_Of_NORM]
:: End_Of_Batch


Report •

#8
August 20, 2009 at 13:55:01

Here the fixed (almost I hope) version; try and report...

:: MYPRO.BAT Usage mypro "File_IN" "File_OUT"
@echo off & setlocal EnableDelayedExpansion

echo.
if not exist "%~f1" (echo.  File "%~f1" not found & goto :EOF)
if "%2"=="" (echo.  Destination for copy missed & goto :EOF)
if "%~f1"=="%~f2" (echo.  Source and Destination can't be the same & goto :EOF)

echo.  "%~f1" copied to "%~f2"
echo.  "%~f1" being processed...
echo.   DONE.

type nul > "%~dpn1.new"
copy %1 %2 > nul
echo.[Date] %date% >> %2

for /F "tokens=1-6,* delims=," %%a in ('type %1') do (
  set fld1=%%a
  set fld2=%%b
  set fld3=%%c
  set fld4=%%d
  set fld5=%%e
  set fld6=%%f

  set cnt=0
  set fld=!fld4:.=!& if !fld! equ 0 set /A cnt+=1
  set fld=!fld5:.=!& if !fld! gtr 0 set /A cnt+=1
  if "!fld6!"=="TON" set /A cnt+=1

  if !cnt! equ 3 (
    set fld6=POUND
    call :NORM !fld3:.=,! fld3 *
    call :NORM !fld5:.=,! fld5 /
  )
  echo.!fld1!,!fld2!,!fld3!,!fld4!,!fld5!,!fld6!,%%g>> "%~dpn1.new"
)
del %1
ren "%~dpn1.new" "%~nx1"
goto :EOF [Return_To_OS]

:NORM
  set up=
  set zero=
  set int=%1& if %1 equ 0 (set int=1& set zero=ON)
  set dec=%2& if "!dec:~1,1!"=="" set dec=%20
  set int=%int%%dec%
  if %4==/ set int=!int!000
  set /A int%4=2000
  if %4==* (
    if defined zero set /A int-=200000
    if !int! lss  10 set int=0!int!
    if !int! lss 100 set int=0!int!
    set %3=!int:~0,-2!.!int:~-2!
  ) else (
    if defined zero set /A int-=50000
    if !int:~-3! gtr 500 set up=ON
    set int=!int:~0,-3!
    if defined up set /A int+=1
    if !int! lss     10 set int=0!int!
    if !int! lss    100 set int=0!int!
    if !int! lss   1000 set int=0!int!
    if !int! lss  10000 set int=0!int!
    if !int! lss 100000 set int=0!int!
    set %3=!int:~0,-5!.!int:~-5!
  )
  if !%3:~0^,1!==. set %3=0!%3!
goto :EOF [End_Of_NORM]
:: End_Of_Batch


Report •

#9
August 21, 2009 at 11:22:29
Ivo.
I am having trouble making this work. it copies the files but i cant get it to look at the columns and the cnt field is not increasing off zero.
in your example the 3rd field is really fld5, teh 4th fiels is really fld8 and teh 5th field is really fld7
i would like to send you a copy of a test file I am working with to see if you can get it to work

Report •

#10
August 21, 2009 at 11:24:29
here is a sample of what the file looks like..
"IVC349529 ","8/19/2009 ","8/19/2009 ","HL #2(P)(STAF 10g) "," 11.99","TON "," 165.82819"," 0.00"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 ","HL #2(P) "," 23974.00","POUND "," 0.00000"," 0.00"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 ","CONTRACT CORN (FINE) 0011070 "," 197.52","Bushel "," 3.03016"," 598.53"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," MIDDS "," 4794.79","POUND "," 0.05873"," 281.60"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," DRIED DISTILLERS GRAIN "," 4794.79","POUND "," 0.04167"," 199.80"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," HIPRO SBM "," 2361.43","POUND "," 0.19150"," 452.22"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," LIME BULK "," 299.67","POUND "," 0.01506"," 4.51"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," FAT BLEND "," 239.73","POUND "," 0.26455"," 63.42"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," L-LYSINE LIQUID "," 141.44","POUND "," 0.44176"," 62.49"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," FAT BLEND (COATER) "," 107.88","POUND "," 0.26455"," 28.54"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," SALT "," 95.89","POUND "," 0.03112"," 2.98"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," THREONINE "," 27.56","POUND "," 1.10942"," 30.59"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," DFS SWINE TRACE MINERAL "," 22.79","POUND "," 0.39156"," 8.93"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," COPPER SULFATE "," 11.98","POUND "," 0.89356"," 10.71"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," GROWER FINISHER PREMIX "," 10.78","POUND "," 1.90158"," 20.52"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," RONOZYME P-CT "," 3.83","POUND "," 1.69676"," 6.51"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 "," STAFAC 20 "," 6.00","POUND "," 3.35336"," 20.12"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 ","TOLL MILL CHARGE (P) STD "," 11.99","TON "," 15.50000"," 185.85"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 ","DIESEL FUEL SURCHARGE "," 11.99","TON "," 0.72000"," 8.63"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 ","FLAT DELIVERY FEE "," 1.00","TON "," 2.18441"," 2.18"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"
"IVC349529 ","8/19/2009 ","8/19/2009 ","EXTENDED MILEAGE CHARGE "," 1.00","TON "," 0.14570"," 0.15"," ISF-QUIRK 1 F047 BARN2 BIN 4706 "," 16,384"

Report •

#11
August 21, 2009 at 12:01:13
i can send an attachment of the file but dont see how I do that here so that you get the real feel of the file. it doesnt really lay out the way it does above

Report •

#12
August 21, 2009 at 13:15:55
Hi lynsie,

I sent you a private message with my e-mail address, but why did you number erroneusly the fields in your starting posts?

From your previous one the layout of the file looks absolutely different what I expected from.


Report •

#13
August 21, 2009 at 13:20:31
I am new to this, I didnt think i would get the entire script, but just helpful hints. This is really great though, so next time i will know to have true values. I hope you can help out with the file

Report •

#14
August 21, 2009 at 13:32:42
OK lynsie,

I'll try hard as I can to send you by e-mail the working version of the script before monday, but success is not guaranteed.

Ciao

Ivo from Italy


Report •

#15
August 21, 2009 at 13:35:04
Thanks Ivo

Report •

#16
August 23, 2009 at 03:52:15
Check your mail in-box

Report •


Ask Question