Batch File to edit specific line of Text file
Moderator: DosItHelp
Re: Batch File to edit specific line of Text file
Problem resolved.
Last edited by werejago on 15 Jan 2021 10:19, edited 2 times in total.
Re: Batch File to edit specific line of Text file
Problem resolved.
Last edited by werejago on 15 Jan 2021 10:19, edited 2 times in total.
Re: Batch File to edit specific line of Text file
"^" 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.
Re: Batch File to edit specific line of Text file
caret
Code: Select all
^
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
&&
Code: Select all
&
Re: Batch File to edit specific line of Text file
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!
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!
Re: Batch File to edit specific line of Text file
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
Re: Batch File to edit specific line of Text file
I'm thinking it still has to do with this same line of code
Looks like it's at the Else if "E" because "C" doesn't seem to be assigned to anything on this line?
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)"
Re: Batch File to edit specific line of Text file
You still need an ampersand before the ENDLOCAL.
Re: Batch File to edit specific line of Text file
Thanks Squashman! I added the ampersand before the endlocal on that line of code.
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.
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)"
Re: Batch File to edit specific line of Text file
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.
and this is I believe the line that needs help with.
Thank you for any efforts!
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
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)"
Re: Batch File to edit specific line of Text file
Problem resolved.
Last edited by werejago on 15 Jan 2021 10:18, edited 2 times in total.
Re: Batch File to edit specific line of Text file
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
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.
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%
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.
Re: Batch File to edit specific line of Text file
Problem resloved.
Last edited by werejago on 15 Jan 2021 10:18, edited 2 times in total.