Batch File to edit specific line of Text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Squashman
Expert
Posts: 4222
Joined: 23 Dec 2011 13:59

Re: Batch File to edit specific line of Text file

#46 Post by Squashman » 07 Jan 2021 10:14

werejago wrote:
07 Jan 2021 09:58
Terry I found my problem!

It was simply the pathing for the ezcad program.
The path includes: "C:\Users\ptech\Documents\Ezcad2.14.9(20170509)\EzCad2.exe" the "(20170509)" in the path i believe was causing the problem and crashing or ending the program.

Any suggestions for a quick fix besides renaming the file?
I may be able to get away with renaming the pathing without the (20170509) if not.

Thank you for taking the time and your feedback is highly appreciated!
Any literal uses of a closing parentheses inside of an existing parenthesized code block need to be escaped.

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

Re: Batch File to edit specific line of Text file

#47 Post by werejago » 07 Jan 2021 10:24

Problem resolved.
Last edited by werejago on 15 Jan 2021 10:19, edited 2 times in total.

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

Re: Batch File to edit specific line of Text file

#48 Post by werejago » 07 Jan 2021 10:28

Problem resolved.
Last edited by werejago on 15 Jan 2021 10:19, edited 2 times in total.

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

Re: Batch File to edit specific line of Text file

#49 Post by werejago » 07 Jan 2021 10:41

"^" closes parenthesis and "&&" is used to add multiple commands within a menu item, got it thanks.
Last edited by werejago on 15 Jan 2021 10:19, edited 2 times in total.

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

Re: Batch File to edit specific line of Text file

#50 Post by T3RRY » 07 Jan 2021 12:00

werejago wrote:
07 Jan 2021 10:41
I didn't know "^" closes parenthesis and "&&" is used to add multiple commands within a menu item.

Thank you so much for the patience with a noob like myself!
caret

Code: Select all

^
escapes the parentheses, essentially stripping it of its properties as a command token.
Doublequoting a string / argument containing parentheses within a parenthesised code block is another method of achieving the same.


A simple example:

Code: Select all

C:\Users\tcdou>For %G in (one two)Do (echo/"(%G)")
C:\Users\tcdou>(echo/"(one)")
"(one)"
C:\Users\tcdou>(echo/"(two)")
"(two)"
C:\Users\tcdou>For %G in (one two)Do (echo/(%G))
) was unexpected at this time.
C:\Users\tcdou>For %G in (one two)Do (echo/(%G^))
C:\Users\tcdou>(echo/(one) )
(one)
C:\Users\tcdou>(echo/(two) )
(two)
C:\Users\tcdou>

Code: Select all

&&
Only acts as conditional concatenation if the previous command succeeded.

Code: Select all

&
Concatenates multiple commands regardless of errorlevel returned, unless of course there is a breaking error in the commands preceeding it.

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

Re: Batch File to edit specific line of Text file

#51 Post by werejago » 07 Jan 2021 12:54

After trying to break the script I found some what of a small issue.

My menu commands currently are:
R for repeat (Re-entry of workorder)
C for continue (Enter starting position)
E for exit.

When I enter in "C" for continue I'm able to input my serial starting position that saves into a text file and it confirms Y/N to go back to the main menu.

If I enter the wrong serial starting position after confirmed Yes and it takes me to the main menu if I press "C" again it auto exits.

Any ideas how i can fix this? not that big of an issue I can work around it if not. Thanks!

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

Re: Batch File to edit specific line of Text file

#52 Post by werejago » 07 Jan 2021 12:55

Here is the current script I'm working on.

Code: Select all

@echo off
Color F
rem /*_________________________________________Deletes all Files in folder besides itself or other folders */ 
9>>"%~f0" (>nul 2>&1 del /f /q *.*)

rem /*_________________________________________Copies and rename designated files */ 
xcopy "Z:\Production\Cell 3 and 4\Laser Engraver Master Files" "C:\Users\ptech\Desktop\Barcode Serialization Files" 
rename "Sensor Engraving Master File.EZD" "Sensor Engraving.EZD"
rename "Sensor Serialization Master File.txt" "Sensor Serialization.txt"

cls

@echo off & CD "%~dp0" & Goto :Main
rem /*_________________________________________Functions; sets parameters, sets input, verfies input to parameters */
:GetIN [ReturnVar] [Length] [Allowed Characters] [Format]
Setlocal EnableDelayedExpansion
Set "Input=(Set "nVar="&Echo/Enter %2 Digit %1 %4:&(For /L %%. in (1 1 %2)Do For /F "Delims=" %%G in ('Choice /N /C %3')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% & Echo/%1 [!nVar!] Confirmed
Endlocal & Set "%1=%nVar%" 2> Nul
Exit /B 0
:UpdateWO
 <"!file!" set /p "line1="
 >"!file!.~tmp" Echo/!line1!
 >>"!file!.~tmp" echo/!#WO!
 <"!file!" >>"!file!.~tmp" more +2
 move /y "!file!.~tmp" "!file!"
Exit /B
:UpdateSER
 <"!File!" (
  Set /P "Line[1]="
  Set /P "Line[2]="
  Set /P "Line[3]="
 )
 >"!File!.~tmp" (
  Echo/!Line[1]!
  Echo/!Line[2]!
  Echo/!Line[3]!
  Echo/!#SER!
 )
 rem /* the below acts upon entries after the entered #SER */
Set "Start=F"
(For /F "UsebackQ Skip=3 Delims=" %%G in ("!File!")Do If not "%%~G" == "!#SER!" (If "!Start!" == "T" (Echo/%%~G))Else Set "Start=T") >>"%File%.~tmp"
move /y "!file!.~tmp" "!file!"
Exit /B
rem /*_________________________________________Script Body; reads/writes to Serialization.txt line 2 */ 
:Main
Set "Menu=Echo/[R]epeat [C]ontinue [E]xit&For /F "Delims=" %%G in ('Choice /N /C:RCE')Do If "%%G"=="R" ( Goto :lbl )Else If "%%G"=="E" (start notepad "Sensor Serialization.txt" && start C:\Users\ptech\Documents\Ezcad2.14.9^(20170509^)\EzCad2.exe "Sensor Engraving.EZD"  Endlocal & Exit /B 0)"
setlocal EnableDelayedExpansion
:WorkOrder
set "file=Sensor Serialization.txt"
Call :GetIn #WO 8 0123456789
rem /* Call function to update workorder # in file */
Call :UpdateWO
%menu:lbl=WorkOrder%
:Serial
Call :GetIn #SER 5 0123456789ABCDEFGH {[01-26][A-H][01-16]}
TYPE "%File%" | findstr.exe /BLIC:"%#SER%" > nul 2> nul || (Echo/Invalid Serial # & Goto :Serial) 
Call :UpdateSer
%menu:lbl=Serial%
rem /* Additions to this script should occur before this Endlocal */




Endlocal

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

Re: Batch File to edit specific line of Text file

#53 Post by werejago » 07 Jan 2021 12:56

I'm thinking it still has to do with this same line of code

Code: Select all

Set "Menu=Echo/[R]epeat [C]ontinue [E]xit&For /F "Delims=" %%G in ('Choice /N /C:RCE')Do If "%%G"=="R" ( Goto :lbl )Else If "%%G"=="E" (start notepad "Sensor Serialization.txt" && start C:\Users\ptech\Documents\Ezcad2.14.9^(20170509^)\EzCad2.exe "Sensor Engraving.EZD"  Endlocal & Exit /B 0)"
Looks like it's at the Else if "E" because "C" doesn't seem to be assigned to anything on this line?

Squashman
Expert
Posts: 4222
Joined: 23 Dec 2011 13:59

Re: Batch File to edit specific line of Text file

#54 Post by Squashman » 07 Jan 2021 13:29

You still need an ampersand before the ENDLOCAL.

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

Re: Batch File to edit specific line of Text file

#55 Post by werejago » 07 Jan 2021 13:50

Thanks Squashman! I added the ampersand before the endlocal on that line of code.

Code: Select all

Set "Menu=Echo/[R]epeat [C]ontinue [E]xit&For /F "Delims=" %%G in ('Choice /N /C:RCE')Do If "%%G"=="R" ( Goto :lbl )Else If "%%G"=="E" (start notepad "Sensor Serialization.txt" && start C:\Users\ptech\Documents\Ezcad2.14.9^(20170509^)\EzCad2.exe "Sensor Engraving.EZD" & Endlocal && Exit /B 0)"
and still my program closes after I enter in "C" to modify starting position entry, "Y" to confirm and if i enter "C" again the program automatically closes. I believe this was T3rrys intention when he helped created this script as it makes sense to press C for continue and to continue out of the program.

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

Re: Batch File to edit specific line of Text file

#56 Post by werejago » 07 Jan 2021 14:28

I believe I need to modify the if do/if else statement by adding one more if option for "C".
Any idea's on how to tackle this?

Here again is the entire script I'm working with.

Code: Select all

@echo off
Color F
rem /*_________________________________________Deletes all Files in folder besides itself or other folders */ 
9>>"%~f0" (>nul 2>&1 del /f /q *.*)

rem /*_________________________________________Copies and rename designated files */ 
xcopy "E:\Experiments\Laser Engraver Master Files" "E:\Experiments\Barcode Serialization Files" 
rename "Sensor Engraving Master File.EZD" "Sensor Engraving.EZD"
rename "Sensor Serialization Master File.txt" "Sensor Serialization.txt"

cls

@echo off & CD "%~dp0" & Goto :Main
rem /*_________________________________________Functions; sets parameters, sets input, verfies input to parameters */
:GetIN [ReturnVar] [Length] [Allowed Characters] [Format]
Setlocal EnableDelayedExpansion
Set "Input=(Set "nVar="&Echo/Enter %2 Digit %1 %4:&(For /L %%. in (1 1 %2)Do For /F "Delims=" %%G in ('Choice /N /C %3')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% & Echo/%1 [!nVar!] Confirmed
Endlocal & Set "%1=%nVar%" 2> Nul
Exit /B 0
:UpdateWO
 <"!file!" set /p "line1="
 >"!file!.~tmp" Echo/!line1!
 >>"!file!.~tmp" echo/!#WO!
 <"!file!" >>"!file!.~tmp" more +2
 move /y "!file!.~tmp" "!file!"
Exit /B
:UpdateSER
 <"!File!" (
  Set /P "Line[1]="
  Set /P "Line[2]="
  Set /P "Line[3]="
 )
 >"!File!.~tmp" (
  Echo/!Line[1]!
  Echo/!Line[2]!
  Echo/!Line[3]!
  Echo/!#SER!
 )
 rem /* the below acts upon entries after the entered #SER */
Set "Start=F"
(For /F "UsebackQ Skip=3 Delims=" %%G in ("!File!")Do If not "%%~G" == "!#SER!" (If "!Start!" == "T" (Echo/%%~G))Else Set "Start=T") >>"%File%.~tmp"
move /y "!file!.~tmp" "!file!"
Exit /B
rem /*_________________________________________Script Body; reads/writes to Serialization.txt line 2 */ 
:Main
Set "Menu=Echo/[R]epeat [C]ontinue [E]xit&For /F "Delims=" %%G in ('Choice /N /C:RCE')Do If "%%G"=="R" ( Goto :lbl )Else If "%%G"=="E" (start notepad "Sensor Serialization.txt" && start C:\Users\ptech\Documents\Ezcad2.14.9^(20170509^)\EzCad2.exe "Sensor Engraving.EZD" & Endlocal & Exit /B 0)"
setlocal EnableDelayedExpansion
:WorkOrder
set "file=Sensor Serialization.txt"
Call :GetIn #WO 8 0123456789
rem /* Call function to update workorder # in file */
Call :UpdateWO
%menu:lbl=WorkOrder%
:Serial
Call :GetIn #SER 5 0123456789ABCDEFGH {[01-26][A-H][01-16]}
TYPE "%File%" | findstr.exe /BLIC:"%#SER%" > nul 2> nul || (Echo/Invalid Serial # & Goto :Serial) 
Call :UpdateSer
%menu:lbl=Serial%
rem /* Additions to this script should occur before this Endlocal */




Endlocal
and this is I believe the line that needs help with.

Code: Select all

:Main
Set "Menu=Echo/[R]epeat [C]ontinue [E]xit&For /F "Delims=" %%G in ('Choice /N /C:RCE')Do If "%%G"=="R" ( Goto :lbl )Else If "%%G"=="E" (start notepad "Sensor Serialization.txt" && start C:\Users\ptech\Documents\Ezcad2.14.9^(20170509^)\EzCad2.exe "Sensor Engraving.EZD" & Endlocal & Exit /B 0)"
Thank you for any efforts!

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

Re: Batch File to edit specific line of Text file

#57 Post by werejago » 07 Jan 2021 14:52

Problem resolved.
Last edited by werejago on 15 Jan 2021 10:18, edited 2 times in total.

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

Re: Batch File to edit specific line of Text file

#58 Post by T3RRY » 07 Jan 2021 19:50

as a macro, no command would have been assigned to C.

The original design was to allow a degree of reusabailty through providing a label name to return to if Repeat was selected using substring modification to replace lbl with the actual label name

Code: Select all

%menu:lbl=labelname%
The macro acts in effect as a pause command that allows selection and execution of options R and E.
Selection of C, having no command assigned to it is saying "Continue this script", and as batch files are parsed on a line by line basis, selecting C will result in any commands
in your batch file below the macro to continue executing line by line.

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

Re: Batch File to edit specific line of Text file

#59 Post by werejago » 08 Jan 2021 07:30

Problem resloved.
Last edited by werejago on 15 Jan 2021 10:18, edited 2 times in total.

Post Reply