I am making a batch script that retrieves a file from one location, and copies it to another, and then renames it to autoexec.cfg
to fix a problem with a game. The only potential conflict I face is if a user has already created an autoexec.cfg
file. So, I need to make my script parse the info from within the target file, and insert it into the very top of an existing autoexec.cfg
file if it's present. I know how to replace text using findstr
, but I need something like inserting text into a specific location using line and column placement. Here is my current script:
IF NOT EXIST "%LOCALAPPDATA%\ElDewrito\keys.cfg" goto :NOKEYS
IF EXIST "AutoExec.cfg" goto :HASKEYS
IF EXIST "%LOCALAPPDATA%\ElDewrito\keys.cfg" goto :EXECUTE
:NOKEYS
SET msgboxTitle=Error
SET msgboxBody=You don't have any keys to import!
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
WSCRIPT "%tmpmsgbox%"
goto :END
:HASKEYS
SET msgboxTitle=Error
SET msgboxBody=You've already imported your keys.
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
WSCRIPT "%tmpmsgbox%"
goto :END
:EXECUTE
COPY "%LOCALAPPDATA%\ElDewrito\keys.cfg" ".\AutoExec.cfg"
goto :SUCCESS
:SUCCESS
SET msgboxTitle=Import
SET msgboxBody=Keys successfully imported!
SET tmpmsgbox=%TEMP%\Message.vbs
IF EXIST "%tmpmsgbox%" DEL /F /Q "%tmpmsgbox%"
ECHO msgbox "%msgboxBody%",0,"%msgboxTitle%">"%tmpmsgbox%"
WSCRIPT "%tmpmsgbox%"
goto :END
:END
Is it possible to parse/copy strings from one text-based document, and paste it into another at a specific position to avoid breaking the structure of the second document? I am aware that the Exchanges aren't for solving one's problems, I just need an idea of if this is feasible, and how to implement it.
C:\blah>(echo abc & echo def) | sed "2isdf"
that outputs abc(new line) sdf(new line) def(new line).sed "s/abc/def/"
You could also get sed to match e.g. 8 character then 15 characters then 7 characters.. and keep the 8, replace the 15 characters with something else. and keep the 7. So that's like a column replacement. You just need to know some basic regexes and capturing.echo abcdefgh | sed -r "s/(...)(..)(...)/\1zz\3/"
returnsabczzfgh
The syntax is sed "s/find/replace/" within the find, the round brackets mean group and capture it, of which you care about the capture. You could sayecho abcdefgh | sed -r "s/(...)..(...)/\1zz\2/"
since you are replacing the middle two characters there's no need to capture them. Dot is regex talk for any character. The Regex is what is in the find portion. If you wanted a literal dot in the find portion then you could do[.]
(echo abc&echo aef) | sed "1s/a/z/"
in contrast, try the same thing removing the 1, so(echo abc&echo aef) | sed "s/a/z/"
You might also want to play with grep particularlygrep -o
as findstr returns a whole line whereas grep -o returns only what is matched. And you might want to look at awk, apparently it's very good though as of yet, i'm not familiar with using awk