0

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.

Hennes
  • 64,768
  • 7
  • 111
  • 168
Mr. Mendelli
  • 1,239
  • 2
  • 24
  • 42
  • 1
    this might help.. you can use *nix tools like sed and grep.. from cygwin or gnuwin32 (cygwin has a later version so is better) `C:\blah>(echo abc & echo def) | sed "2isdf"` that outputs abc(new line) sdf(new line) def(new line). – barlop Apr 29 '18 at 08:02
  • sed would also let you match part of a line and replace that part. `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. – barlop Apr 29 '18 at 08:04
  • `echo abcdefgh | sed -r "s/(...)(..)(...)/\1zz\3/"` returns `abczzfgh` 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 say `echo 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 `[.]` – barlop Apr 29 '18 at 08:08
  • you can also limit a replace to a particular line e.g. `(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 particularly `grep -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 – barlop Apr 29 '18 at 08:12
  • 1
    also this may help https://superuser.com/questions/339118/regex-replace-from-command-line it mentions a bunch of solutions including vbscript(which is native), and a thing called jrepl which is built on native stuff. But notice nobody has done anything totally batch it might be possible but it'd be hideously ugly, hence even a batch master like dbenham did a hybrid batch with jscript rather than pure batch(which he included at that link). – barlop Apr 29 '18 at 08:18
  • In addition to the approach already suggested, you may even want to try something like [Python](https://www.python.org) which generally has a relatively low learning curve and fairly powerful text manipulation capabilities. – Anaksunaman Apr 29 '18 at 08:45

0 Answers0