Batch file edit text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
msswamy
Posts: 2
Joined: 01 Feb 2021 21:06

Re: Batch file edit text file

#16 Post by msswamy » 02 Feb 2021 10:33

Thanks for your solution and it solved problem with exclamation mark ('!').
Regarding my new line issue, below is my sample text file.

Code: Select all

[Components.Section]
 Element_1
 !Element_2
 Element_3
Below are the string variables,

Code: Select all

set "replace=[Components.Section]"
set "replaced=[Components.Section]\nMyElement_0"
Finally my text file should come like below,

Code: Select all

[Components.Section]
 MyElement_0
 Element_1
 !Element_2
 Element_3
But it is coming like below,

Code: Select all

[Components.Section]\nMyElement_0
 Element_1
 !Element_2
 Element_3

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: Batch file edit text file

#17 Post by T3RRY » 02 Feb 2021 12:44

msswamy wrote:
02 Feb 2021 10:33
Thanks for your solution and it solved problem with exclamation mark ('!').
Regarding my new line issue, below is my sample text file.
\n is the literal string \n in batch scripting.

a linefeed can be defined as a variable using the following, however it requires delayed expansion to be enabled to use.

Code: Select all

@Echo Off
(Set LF=^


%= Linefeed. do not modify this or above three lines. =%)
Setlocal enabledelayedexpansion
>>"%~dp0outfile.txt" Echo/this is!LF!a demo
Type "%~dp0outfile.txt"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#18 Post by penpen » 07 Feb 2021 14:40

@msswamy: You didn't react to the post of T3RRY, so i don't know whether you found it solved your issue or not.

penpen

sk8ordie
Posts: 13
Joined: 10 Feb 2021 03:56

Re: Batch file edit text file

#19 Post by sk8ordie » 11 Nov 2021 17:13

Hi!

in cases where I can use it relatively well, but how can it be solved when there are unknown numbers in a particular place?
I could somehow describe it in such a way that there are always random numbers at that location

result.txt

Code: Select all

https://stream9.nava.hu/navahu_bdrm/_definst_/amlst:mnfa-84/manifest_w634869604_ps3854000_pd60000000_qc2Vzc2lkPSZvbXE9dHJ1ZSZucD0mbGI9NTdoN1dqVDg5N2VXVnVLUnhhQk9BYzJmS2JKWSUyQlhudGtNQXhSRWxET3BDc0ttR055T2FlRGJSbFQ5SGQyMzZ6MFpReXpmTVlBSlFMJTBEJTBBbEFxWmlOeFBqQSUzRCUzRCUwRCUwQQ==.mpd
randoms:
_ps4434700_
_ps0268120_
_ps1828551_
.
.
etc

i try to use _ps* but this not worked :)


this is what need to find or maybe just ps?
_ps

and always append this number
0000100

target.txt

Code: Select all

https://stream9.nava.hu/navahu_bdrm/_definst_/amlst:mnfa-84/manifest_w634869604_ps0000100_pd60000000_qc2Vzc2lkPSZvbXE9dHJ1ZSZucD0mbGI9NTdoN1dqVDg5N2VXVnVLUnhhQk9BYzJmS2JKWSUyQlhudGtNQXhSRWxET3BDc0ttR055T2FlRGJSbFQ5SGQyMzZ6MFpReXpmTVlBSlFMJTBEJTBBbEFxWmlOeFBqQSUzRCUzRCUwRCUwQQ==.mpd
thanks in advance for your help!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Batch file edit text file

#20 Post by penpen » 12 Nov 2021 08:04

When there are unknown numbers in a particular place, then you either have to get to know those (e.g. using the "for/f"-command), or have to use something that is able to search for structures in strings.

So that depends on the source file you want to process; if there's are only a single (or very few) occurency(/-ies) of those unknown numbers per file, then using for/f to split the string into parts along delimiter '_' (underscore character) and check if the format is correct might be sufficient, though that task easily gets complicated based on the strings or encding you are using.

Therefore in that case you have described, i would suggest to use the latter option (although you have probably have read into that topic a bit); for instance you could use regular expressions to describe the structure, which would make dave's JREPL.BAT a possible candidate to use.

penpen

sk8ordie
Posts: 13
Joined: 10 Feb 2021 03:56

Re: Batch file edit text file

#21 Post by sk8ordie » 12 Nov 2021 08:12

okay thanks for the recommendation, I've been to the jrepl topic for other reasons, I just didn't think it could be used for this operation

Post Reply