Need help copying a line of text from a file to another (without mangling it) :AppendFileLineToFile
Posted: 22 Apr 2024 03:54
Hello
I made this function to copy arbitrary lines of text, from one file to another
The logic of copying the correct rows to the right place LARGELY works
Unfortunately, it does damage some lines of text and it completely breaks if it encounters ">>" possibly other characters
For example this line
becomes this line
And this line
breaks it in a very strange way that I do not understand.
Here is a screenshot
The original file is on the right, the newly created on the left
This is part of a larger project, which allows copying selected batch functions to another batch file.
I have created a special "function switching" batch file, when you call it, the first argument is the function that will be executed with the rest of your arguments.
Here I demonstrate the "listfunctions" function, which outputs a list of all function existing in a batchfile
And now I demontrate the "AddFunctionToBatch" function, which copies chosen functions from a batchfile to another batchfile. It ALMOST works
This previous command, creates the test\mybfw.bat file, which contains (almost) everything it needs to perform the task of the original bfw.bat file.
Unfortunately the functions :GetLabelRow and :AppendFileLineToFile are mangled by this process
The original bfw.bat
https://pastebin.com/456kfhTi
The new test\mybfw.bat file
https://pastebin.com/vkkMGin9
You can also observe the bfw.bat file at
https://github.com/batchfileframework/B ... fw/bfw.bat
I also include it as a zip here
I made this function to copy arbitrary lines of text, from one file to another
Code: Select all
::Usage Call :AppendFileLineToFile inputfile outputfile 3 4 50-75 5 6 7 ... N
:AppendFileLineToFile
set "_AppendFileLineToFile_prefix=_AFLTF"
set "_AFLTF_InputFile=%~1"
set "_AFLTF_OutputFile=%~2"
:AppendFileLineToFile-arg
for /f "delims=- tokens=1,2" %%a in ("%~3") do ( set "_AFLTF_Start=%%a" & set "_AFLTF_Stop=%%b" )
if not defined _AFLTF_Stop set /a _AFLTF_Stop=%_AFLTF_Start%
Setlocal enabledelayedexpansion
if %_AFLTF_Start% GTR 1 set /a "_AFLTF_skip=%_AFLTF_Start%-1"
if %_AFLTF_Start% GTR 1 ( set "_AFLTF_skip=skip^=%_AFLTF_skip%^" ) else ( set "_AFLTF_skip=" )
for /f %_AFLTF_skip% delims^=^ eol^= %%a in (' ^( type "%_AFLTF_InputFile%" ^| %SystemRoot%\System32\findstr /N /R /C:".*" ^) 2^>nul ') do (
for /f "delims=:" %%f in ("%%a") do if %%f GTR %_AFLTF_Stop% GoTo :AppendFileLineToFile-end
set _AFLTF_buffer=%%a
if defined _AFLTF_buffer >>"%_AFLTF_OutputFile%" echo(!_AFLTF_buffer:*:=!
)
endlocal
:AppendFileLineToFile-end
if "[%~4]" NEQ "[]" ( shift & GoTo :AppendFileLineToFile-arg )
Call :ClearVariablesByPrefix %_AppendFileLineToFile_prefix% _AppendFileLineToFile_prefix & GoTo :EOF
Unfortunately, it does damage some lines of text and it completely breaks if it encounters ">>" possibly other characters
For example this line
Code: Select all
for /f delims^=:^ tokens^=1 %%a in ('%SystemRoot%\System32\findstr /I /N "^:%~2" "%~1" ^| findstr /I /V "::%~2[a-zA-Z0-9\-\/\?\!\@\%%\$\#\^\*\)\{\}\[\]\:\_]"') do ( if "[%~3]" NEQ "[]" set "%~3=%%a" & exit /b %%a )
Code: Select all
for /f delims=: tokens=1 %%a in ('%SystemRoot%\System32\findstr /I /N ":%~2" "%~1" | findstr /I /V "::%~2[a-zA-Z0-9\-\/\?\\_]"') do ( if "[%~3]" NEQ "[]" set "%~3=%%a" & exit /b %%a )
Code: Select all
if defined _AFLTF_buffer >>"%_AFLTF_OutputFile%" echo(!_AFLTF_buffer:*:=!
Here is a screenshot
The original file is on the right, the newly created on the left
This is part of a larger project, which allows copying selected batch functions to another batch file.
I have created a special "function switching" batch file, when you call it, the first argument is the function that will be executed with the rest of your arguments.
Here I demonstrate the "listfunctions" function, which outputs a list of all function existing in a batchfile
Code: Select all
bfw listfunctions bfw.bat
ShiftedArgumentCaller AddFunctionToBatch GetLastToken IsLastToken GetLabelsOnly IsFile GetFunctionRows GetLabelRow GetFunctionExit GetFunctionPreambleRow GetFunctionPostscriptRow ClearVariablesByPrefix GetFunctionName GetBatchCore GetNextExitRow ListFunctions GetNextFunctionName GetNextFunctionRow GetPreviousExitRow GetEOFrow countLines IsFunctionLabelExcluded AppendFileLineToFile AppendFileLineToFile-arg GetPreviousEmptyRow GetNextEmptyRow BFWFunctionSwitcher-text EndOF_BFWFunctionSwitcher-text
Code: Select all
bfw AddFunctionToBatch test\mybfw.bat CORE ShiftedArgumentCaller AddFunctionToBatch GetLastToken IsLastToken GetLabelsOnly IsFile GetFunctionRows GetLabelRow GetFunctionExit GetFunctionPreambleRow GetFunctionPostscriptRow ClearVariablesByPrefix GetFunctionName GetBatchCore GetNextExitRow ListFunctions GetNextFunctionName GetPreviousExitRow GetEOFrow countLines IsFunctionLabelExcluded AppendFileLineToFile AppendFileLineToFile-arg GetPreviousEmptyRow GetNextEmptyRow BFWFunctionSwitcher-text
Unfortunately the functions :GetLabelRow and :AppendFileLineToFile are mangled by this process
The original bfw.bat
https://pastebin.com/456kfhTi
The new test\mybfw.bat file
https://pastebin.com/vkkMGin9
You can also observe the bfw.bat file at
https://github.com/batchfileframework/B ... fw/bfw.bat
I also include it as a zip here