Re: Batch File to edit specific line of Text file
Posted: 07 Jan 2021 10:24
Problem resolved.
A Forum all about DOS Batch
https://www.dostips.com/forum/
caret
Code: Select all
^
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
&
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
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)"
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)"
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)"
Code: Select all
%menu:lbl=labelname%