Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- Color 02
- TITLE Text Substitution by Line Number
- Setlocal EnableDelayedExpansion
- :select
- cls
- Set /p "inputfile=[Type or paste full filepath, or drop file If no Spaces:]>"
- IF not defined inputfile goto select
- REM - Test existance of file, return to select if not exist
- IF NOT exist "%inputfile%" GOTO select
- REM - Capture info to rename temp file with later
- for /f "delims=" %%a in ("%inputfile%") do (
- Set inputfile=%%a
- Set name=%%~na
- Set Location=%%~dpa
- Set EXT=%%~xa
- )
- Set "TempReplaces=!Location!!name!!EXT!"
- REM - Count number of lines in file
- Set avail=0
- REM - Check available lines:
- for /f "delims=" %%a in (%inputfile%) do (
- set /a avail+=1
- )
- REM - Select Line Number
- :linenumber
- cls
- ECHO File: %inputfile%
- Set /p subline=[File Contains: %avail% Lines. Line Number to Substitute:]
- REM - Ensure the linenumber is numerical
- Set /a test=%subline%+1
- IF %test% LSS 2 GOTO linenumber
- REM - Enter Replacement String
- Set /p substitution=[Enter Replacement Text:]
- REM - Function counts lines then Echoes the defined text when target line is encountered.
- for /f "delims=" %%l in (%inputfile%) do (
- set /a line+=1
- if !line! EQU !subline! (
- echo %substitution% >>%Location%TempSub%EXT%
- ) else (
- echo %%l >>%Location%TempSub%EXT%
- )
- )
- REM - Replace File With Substitution
- DEL /Q "%TempReplaces%"
- DEL /Q "%TEMP%\inputfile.txt"
- TIMEOUT 1 > nul
- FOR %%n IN ("%Location%TempSub%EXT%") DO (
- REN %%n "%name%!EXT!"
- )
- ECHO Done
- TIMEOUT 2 > nul
- EXIT
Advertisement
Add Comment
Please, Sign In to add comment