Batch File to edit specific line of Text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Batch File to edit specific line of Text file

#1 Post by werejago » 01 Oct 2020 07:52

Hello I,ve been searching endlessly now to figuring this out...

I have a text file I want to modify through a batch command using input from user.

I have a text file that needs a specific line of text to be modified by user input.

Need to edit Line 2 or : "03252001" needs to be modified by user input and saved as the same filename...
Any ideas?

This is the text file below.



Enter the eight-digit sensor lot number (minus the WSV and the dashes) on Line 2:
03252001
Begin sensor serializaitons, DO NOT change information on Line 3 or beyond.
01A01
01A02
01A03
01A04
01A05
01A06
01A07
01A08
01A09
01A10
01A11
01A12
01A13
01A14
01A15
01A16
01B01
01B02
01B03
01B04
01B05
01B06
01B07
01B08
01B09
01B10
01B11
01B12
01B13
01B14
01B15
01B16
01C01
01C02
01C03
01C04
01C05
01C06
01C07
01C08
01C09
01C10
01C11
01C12
01C13
01C14
01C15
01C16
01D01
01D02
01D03
01D04
01D05
01D06
01D07
01D08
01D09
01D10
01D11
01D12
01D13
01D14
01D15
01D16
01E01
01E02
01E03
01E04
01E05
01E06
01E07
01E08
01E09
01E10
01E11
01E12
01E13
01E14
01E15
01E16
01F01
01F02
01F03
01F04
01F05
01F06
01F07
01F08
01F09
01F10
01F11
01F12
01F13
01F14
01F15
01F16
01G01
01G02
01G03
01G04
01G05
01G06
01G07
01G08
01G09
01G10
01G11
01G12
01G13
01G14
01G15
01G16

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to edit specific line of Text file

#2 Post by aGerman » 01 Oct 2020 08:57

I'm a little confused about the content. In particular if "Enter the eight-digit sensor lot number (minus the WSV and the dashes) on Line 2:" is part of the file or if you just repeated your request.

Update the file name and give it a try. Maybe it's what you're looking for ...

Code: Select all

@echo off &setlocal
set "file=foo.txt"
set /p "num=Enter the number: "

<"%file%" set /p "line1="

setlocal EnableDelayedExpansion
>"!file!.~tmp" echo(!line1!
>>"!file!.~tmp" echo(!num!
<"!file!" >>"!file!.~tmp" more +2

move /y "!file!.~tmp" "!file!"
Steffen

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#3 Post by werejago » 01 Oct 2020 09:24

I'll give it a shot and let you know and thank you for the quick response!

And to further explain the "Enter the eight-digit sensor lot number (minus the WSV and the dashes) on Line 2:" IS indeed part of the literal txt file I'm trying to modify.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to edit specific line of Text file

#4 Post by aGerman » 01 Oct 2020 09:31

This was also my assumption. So, in this case it should likely work out of the box...

Steffen

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#5 Post by werejago » 01 Oct 2020 11:31

This works excellent thank you!

I have a few questions about cleaning it up.

1) Is there an easier way to delete files in the same folder the batch file is used in without deleting the batch file itself?

2) Can I clear what the command prompt displays when user is asked to input workorder number? EX. (Just ask for a workorder # without showing the work being done previously in the script).

3) And finally a way to verify the work order was entered in correctly by having to re enter the work order in the command prompt. (Enter in the number twice for verification).


Here is the code I'm using below
del /F /Q "Z:\Users\Wes\Barcode Serialization Files\Sensor Engraving Master File.EZD"
del /F /Q "Z:\Users\Wes\Barcode Serialization Files\Sensor Engraving.EZD"
del /F /Q "Z:\Users\Wes\Barcode Serialization Files\Sensor Serialization Master File.txt"
del /F /Q "Z:\Users\Wes\Barcode Serialization Files\Sensor Serialization.txt"

Z:\Users\Wes\

xcopy "Z:\Users\Wes\Laser Engraver Master Files" "Z:\Users\Wes\Barcode Serialization Files"

rename "Sensor Engraving Master File.EZD" "Sensor Engraving.EZD"
rename "Sensor Serialization Master File.txt" "Sensor Serialization.txt"

@echo off &setlocal
set "file=Sensor Serialization.txt"
set /p "num=Enter the number: "

<"%file%" set /p "line1="

setlocal EnableDelayedExpansion
>"!file!.~tmp" echo(!line1!
>>"!file!.~tmp" echo(!num!
<"!file!" >>"!file!.~tmp" more +2

move /y "!file!.~tmp" "!file!"


start notepad "Sensor Serialization.txt""

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

Re: Batch File to edit specific line of Text file

#6 Post by T3RRY » 01 Oct 2020 11:52

1) you can use the output of a filtered Dir command to obtain paths for files you wish to delete in conjunction with an If Not conditional check on the batch files qualified path to exclude the executing batch

2) `@` can be prepended to commands to 'hide' the echo of the command execution, or `@Echo off` can be used at the beginning of your batch to do this for all commands.

3) an input method for numerical input of fixed length that enables confirmation and restricts to integer only input:

Code: Select all

@Echo off
:GetIN
 Setlocal EnableDelayedExpansion
 Set "Input=(Set "nVar="&Echo/Enter # Digit Integer:&(For /L %%. in (1 1 #)Do For /F "Delims=" %%G in ('Choice /N /C 0123456789')Do (<Nul Set /P"=%%G"&Set "nvar=^^!nVar^^!%%G"))&Echo/&Echo/Confirm: ^^!nVar^^! Y/N & For /F "Delims=" %%v in ('Choice /N')Do (If /I "%%~v"=="n" (Goto :retry)))"
:retry
 %Input:#=8% & Echo/Var [!nVar!] Confirmed
 Endlocal & Set "%1=%nVar%" 2> Nul
Exit /B 0

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#7 Post by werejago » 01 Oct 2020 12:24

Can't thank you enough for the solid info! Gonna give it a try and report here with an update.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to edit specific line of Text file

#8 Post by aGerman » 01 Oct 2020 12:58

1) you can use the output of a filtered Dir command to obtain paths for files you wish to delete in conjunction with an If Not conditional check on the batch files qualified path to exclude the executing batch
A tiny little hack:

Code: Select all

9>>"%~f0" (>nul 2>&1 del /f /q *.*)
This should delete every file in the current directory but the own script file. That's because it has been opened for writing and is locked.

Steffen

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

Re: Batch File to edit specific line of Text file

#9 Post by T3RRY » 01 Oct 2020 13:32

aGerman wrote:
01 Oct 2020 12:58
A tiny little hack:

Code: Select all

9>>"%~f0" (>nul 2>&1 del /f /q *.*)
This should delete every file in the current directory but the own script file. That's because it has been opened for writing and is locked.

Steffen
Is the use of an undefined handle necessary for this to work? - Ie does it add any benefit considering the redirection of output within the dir codeblock to nul used to stop the dir output from being appended to the batch?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to edit specific line of Text file

#10 Post by aGerman » 01 Oct 2020 13:54

Is the use of an undefined handle necessary for this to work?
Nope. It's just a remnant from before I redirected stderr and stdout to NUL. As long as you assure that nothing is written to stdout within the parenthesized clause you can remove the 9 without risking side effects.

Steffen

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#11 Post by werejago » 02 Oct 2020 07:11

Okay so I tried combining the two examples of code I got from you guys on here and am struggling getting it to work and it wasn't exactly what I'm looking for.
For verification of the workorder number I was hoping to have the user input it twice to make sure it's the same number. If I could add that with the 8 digit verification code that would be epic.

This is the instructions I'm using for a user input to modify a line of text in a text file:
----------------------------------------------------------------------------------------------------
@echoff &setlocal
set "file=Sensor Serialization.txt"
set /p "num=Enter the number: "

<"%file%" set /p "line1="

setlocal EnableDelayedExpansion
>"!file!.~tmp" echo(!line1!
>>"!file!.~tmp" echo(!num!
<"!file!" >>"!file!.~tmp" more +2

move /y "!file!.~tmp" "!file!"
----------------------------------------------------------------------------------------------------

And this is the code I'm using to try and verify that user input entry in the command prompt BEFORE saving to file.
----------------------------------------------------------------------------------------------------

@Echo off
:GetIN
Setlocal EnableDelayedExpansion
Set "Input=(Set "nVar="&Echo/Enter # Digit Integer:&(For /L %%. in (1 1 #)Do For /F "Delims=" %%G in ('Choice /N /C 0123456789')Do (<Nul Set /P"=%%G"&Set "nvar=^^!nVar^^!%%G"))&Echo/&Echo/Confirm: ^^!nVar^^! Y/N & For /F "Delims=" %%v in ('Choice /N')Do (If /I "%%~v"=="n" (Goto :retry)))"
:retry
%Input:#=8% & Echo/Var [!nVar!] Confirmed
Endlocal & Set "%1=%nVar%" 2> Nul
Exit /B 0
----------------------------------------------------------------------------------------------------

Anyone know of the easiest way of combing the two into a single batch? and possibly adding a command to verify the correct number was submitted? :) Would greatly appreciate it!
Last edited by werejago on 14 Oct 2020 07:47, edited 1 time in total.

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#12 Post by werejago » 02 Oct 2020 07:26

The objective here is simply to write a batch file that can modify the 2nd line of text in a text file by user input and having a verification by asking the user to re enter the number that will modify the line of text. I do like the idea of having the 8 digit verification if it's possible to still include that without a hassle but really I'll be happy with what works. Thank you.

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

Re: Batch File to edit specific line of Text file

#13 Post by T3RRY » 02 Oct 2020 08:27

werejago wrote:
02 Oct 2020 07:26
The objective here is simply to write a batch file that can modify the 2nd line of text in a text file by user input and having a verification by asking the user to re enter the number that will modify the line of text. I do like the idea of having the 8 digit verification if it's possible to still include that without a hassle but really I'll be happy with what works. Thank you.
This would be the simplest way to combine the two

Code: Select all

@echo off & Goto :Main
rem /*_________________________________________Functions */
:GetIN
 Setlocal EnableDelayedExpansion
 Set "Input=(Set "nVar="&Echo/Enter # Digit Integer:&(For /L %%. in (1 1 #)Do For /F "Delims=" %%G in ('Choice /N /C 0123456789')Do (<Nul Set /P"=%%G"&Set "nvar=^^!nVar^^!%%G"))&Echo/&Echo/Confirm: ^^!nVar^^! Y/N & For /F "Delims=" %%v in ('Choice /N')Do (If /I "%%~v"=="n" (Goto :retry)))"
:retry
 %Input:#=8% & Echo/Var [!nVar!] Confirmed
 Endlocal & Set "%1=%nVar%" 2> Nul
Exit /B 0
rem /*_________________________________________Script Body */ 
:Main
set "file=Sensor Serialization.txt"
<"%file%" set /p "line1="
Call :GetIn Num
setlocal EnableDelayedExpansion
>"!file!.~tmp" echo(!line1!
>>"!file!.~tmp" echo(!num!
<"!file!" >>"!file!.~tmp" more +2
move /y "!file!.~tmp" "!file!"
Endlocal

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#14 Post by werejago » 02 Oct 2020 09:08

Thank you T3rry!
Works like a charm so far :)
I'll play around with it and see if it completes the job, thanks again!

werejago
Posts: 42
Joined: 01 Oct 2020 07:43

Re: Batch File to edit specific line of Text file

#15 Post by werejago » 08 Oct 2020 09:47

Hey T3rry or anyone who thinks they can help - I have a question about the above commands.

Could you further break down or explain each part of the code or lead me to the right location to figuring it out.

Trying to learn this and would greatly appreciate any help thank you!

Post Reply