Fixing Files From DOS 26 Mar ’12

Posted by: Jason Laws

Someone asked me the other day: “How do I fix this data file before I load it?”

All they wanted to do was change one value that contained a reserved word their loader couldn’t cope with.

They were on windows and didn’t want to write anything that needed compiling (c, vb, etc).

Finally, it needed to run from DOS in a RED host script.

Here’s what we came up with – a really simple DOS program that generates and runs a windows powershell script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SETLOCAL ENABLEEXTENSIONS
SET INFILE=c:\in.txt
SET OUTFILE=c:\out.txt
SET OLDSTRING=oldval
SET NEWSTRING=newval
SET FILEPS1=%WSL_WORKDIR%\wsl%WSL_SEQUENCE%.ps1
SET FILEAUD=%WSL_WORKDIR%\wsl%WSL_SEQUENCE%.aud
ECHO Get-Content “%INFILE%” ^| ForEach-Object { $_ -replace “%OLDSTRING%”, “%NEWSTRING%” } ^| Set-Content “%OUTFILE%” > %FILEPS1% 2>%FILEAUD%
powershell -command . %FILEPS1% >> %FILEAUD% 2>&1
ECHO 1
ECHO The string %OLDSTRING% in %INFILE% has been replaced by the string %NEWSTRING% to create %OUTFILE%
TYPE %FILEAUD%
EXIT

If you want to use this, just paste it into a RED host script and change the four variable SET statements to your file names and strings…

Contribute



E-mail addresses are not published and are kept confidential.




* - denotes required field.