Edit by command line a txt file

0

I need to modify line 5 and line 15 in a .txt file by means of a .bat modifying the date of the day.

The data to be modified would be the following:

HostFile=BIBLIOTECA/ARCHIVOddmmaa
PCFile=\servidor\carpeta\subcarpeta\ARCHIVOddmmaa

In which you automatically add today's date to the file name, for example:

HostFile=BIBLIOTECA/ARCHIVO220817
PCFile=\servidor\carpeta\subcarpeta\ARCHIVO220817

This would be the structure of the file to be modified:

[DataTransferFromAS400]
Version=2.0
[HostInfo]
Database=*SYSBAS
HostFile=BIBLIOTECA/ARCHIVOddmmaa
HostName=
[ClientInfo]
ASCIITruncation=1
ConvType=0
CrtOpt=1
FDFFile=
FDFFormat=1
FileOps=503209343
OutputDevice=2
PCFile=\servidor\carpeta\subcarpeta\ARCHIVOddmmaa
PCFileType=1
SaveFDF=0

The code to set the date in a variable I already have it:

set aa=%date:~8,4%
set mm=%date:~3,2%
set dd=%date:~0,2%

I would be missing how to edit lines 5 and 15 in the .txt file; What would it be like?

    
asked by Jimmy Bianco 22.08.2017 в 15:00
source

2 answers

1

This Batch program modifies your file in the way described:

@echo off
setlocal EnableDelayedExpansion

set "fecha=%date:~0,2%%date:~3,2%%date:~8,4%"

(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" input.txt') do (
   set "line=%%b"
   if %%a equ 5 (
      set "line=!line:ddmmaa=%fecha%!"
   ) else if %%a equ 15 (
      set "line=!line:ddmmaa=%fecha%!"
   )
   echo !line!
)) > output.txt
move /Y input.txt output.txt
    
answered by 26.08.2017 в 06:05
0

Make three files, one that has lines 1-4, another that has from 6 to 14 and another that has the 16 at the end.

  • Then copy the first file to your destination file
  • Then add line 5 with the edited date to the final destination
  • Then add the second file to the target file
  • Then add the edited line 15 to the target file
  • Then add the third file to the target file
answered by 22.08.2017 в 17:00