Home > other >  Change certain column in text file, and the rest column will remain using batch script
Change certain column in text file, and the rest column will remain using batch script

Time:09-30

sorry if this silly questions,

I need to replace delimiter in text file column and data in certain column only.

I already success to replace delimiter from comma to pipe, but not yet on key in data.

THIS IS BATCH FOR REPLACE DELIMETER :

@echo off
setlocal disableDelayedExpansion

set input="PLUIn.txt"
set output="dibalscopitems2.txt"

::There should be a TAB character after the equal below
set "tab=,"

>%output% (
for /f "delims=" %%A in ('findstr /rn "^" %input%') do (
set ln=%%A
setlocal enableDelayedExpansion
set "ln=!ln:*:=!"
if defined ln set "ln=!ln:%tab%=|!"
echo(!ln!
endlocal
)
) 

THE QUESTION IS : For text file, i want to key in certain column only, and the rest column will auto generate.

As sample below :

Key in text file : 00001,1,7.50,APPLE,APPLE,0,0

Result after run batch : M|00001|1|7.50|APPLE|APPLE|0|0|21|2|1|0.00|000|000|0||2|45|1|

So I only key in item in highlight on blue in images. Refer this image

DETAILED COLUMN :

M|00001|1|7.50|AYAM SEGAR||0|0|21|2|1|0.00|000|000|0||2|45|1|
M|00002|2|6.99|APPLE||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00003|3|9.99|SIAKAP||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00004|4|1.99|ORANGE||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00005|5|12.99|KEMBUNG||0|0|21|2|1|0.00|000|000|0||2|45|1|
  1. A,B @ M - A(add), B(Remove), M(modify)
  2. Plu No - Max 6 digits
  3. Hotkey - Max 3 digits
  4. Unit price - 0.00
  5. Name - 20 characters
  6. Name2 - dummy
  7. Weight unit - 0=weight / 1=pcs
  8. Section (barcode structure no - 1=weight / 2=pcs
  9. Label Format - dummy
  10. EAN Format - dummy
  11. Vat Type - dummy
  12. Offer Price - dummy
  13. Expiry Date - 001 = 1 days
  14. Extra Date - dummy
  15. Tare - dummy
  16. EAN Scannner - dummy
  17. Product Class - dummy
  18. Product Direct Number - dummy
  19. Overwrite Price - 0=allow / 1=notallow
  20. G Text - dummy

INPUT FILES :

M,00001,1,7.50,AYAM SEGAR,,0,0,
M,00002,2,6.99,APPLE,,1,0,
M,00003,3,9.99,SIAKAP,,1,0,
M,00004,4,1.99,ORANGE,,1,0,
M,00005,5,12.99,KEMBUNG,,0,0,

RESULT AFTER RUN :

M|00001|1|7.50|AYAM SEGAR||0|0|21|2|1|0.00|000|000|0||2|45|1|
M|00002|2|6.99|APPLE||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00003|3|9.99|SIAKAP||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00004|4|1.99|ORANGE||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00005|5|12.99|KEMBUNG||0|0|21|2|1|0.00|000|000|0||2|45|1|

ON INPUT FILES, we do not want to key in all column, as some column we do not use it.so from column no 9 until last, the values is not changed. we want to key in from column 1 until 8.

Thank you

CodePudding user response:

Ok, here we go...

PLUIn.txt the input file:

M,00001,1,7.50,AYAM SEGAR,,0,0,
M,00002,2,6.99,APPLE,,1,0,
M,00003,3,9.99,SIAKAP,,1,0,
M,00004,4,1.99,ORANGE,,1,0,
M,00005,5,12.99,KEMBUNG,,0,0,

The Batch file program:

@echo off
setlocal EnableDelayedExpansion

set "data=21|2|1|0.00|000|000|0||2|45|1|"

(for /F "delims=" %%a in (PLUIn.txt) do (
   set "line=%%a"
   echo !line:,=^|!!data!
)) > dibalscopitems2.txt

dibalscopitems2.txt the output file:

M|00001|1|7.50|AYAM SEGAR||0|0|21|2|1|0.00|000|000|0||2|45|1|
M|00002|2|6.99|APPLE||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00003|3|9.99|SIAKAP||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00004|4|1.99|ORANGE||1|0|21|2|1|0.00|000|000|0||2|45|1|
M|00005|5|12.99|KEMBUNG||0|0|21|2|1|0.00|000|000|0||2|45|1|

Some suggestions when you request technical aid:

You should describe your problem in terms as clear and concise as possible. Do not insert additional information not related to the problem that just could confuse! For example:

  • Where is the input data?

  • I want to key in certain column only

  • Ok. How is such data?

  • Because some column we do not use it, the rest column will auto generate.

  • ("auto generate"? using which formula?) Ok, ok... I don't care if you want or want not to enter data, nor if you "key in" such a data or it magically appears in a file. Do you have a file with the data? How is it?

    M,00001,1,7.50,AYAM SEGAR,,0,0,

  • And the output?

    M|00001|1|7.50|AYAM SEGAR||0|0|21|2|1|0.00|000|000|0||2|45|1|

  • Ok. Thanks a lot!

EDIT 2021/09/27: Modified program as requested in comment

New PLUIn.txt input file:

M,00001,1,7.50,AYAM SEGAR,,0,0,1
M,00002,2,6.99,APPLE,,1,0,12
M,00003,3,9.99,SIAKAP,,1,0,123
M,00004,4,1.99,ORANGE,,1,0,555
M,00005,5,12.99,KEMBUNG,,0,0,999

New program.bat:

@echo off
setlocal EnableDelayedExpansion

set "data=|21|2|1|0.00|HERE|000|0||2|45|1|"

(for /F "delims=" %%a in (PLUIn.txt) do (
   set "line=%%a|"
   for /F "tokens=7* delims=," %%b in ("%%a") do (
      set "line=!line:,%%c|=!"
      echo !line:,=^|!!data:HERE=%%c!
   )
)) > dibalscopitems2.txt

New dibalscopitems2.txt output file:

M|00001|1|7.50|AYAM SEGAR||0|0|21|2|1|0.00|1|000|0||2|45|1|
M|00002|2|6.99|APPLE||1|0|21|2|1|0.00|12|000|0||2|45|1|
M|00003|3|9.99|SIAKAP||1|0|21|2|1|0.00|123|000|0||2|45|1|
M|00004|4|1.99|ORANGE||1|0|21|2|1|0.00|555|000|0||2|45|1|
M|00005|5|12.99|KEMBUNG||0|0|21|2|1|0.00|999|000|0||2|45|1|
  • Related