dbohdan/initool

[Windows CMD] Cannot write multiple values/only change one value in a file, when not writing to a temporary file first

e-t-l opened this issue · 2 comments

(Not a duplicate of #11.)
Do you have to pipe the output to temporary files? Does it not work to overwrite the same file?

e.g.

C:\> initool get filename.ini

[section1]
abc=xyz
[section2]
foo=bar

C:\> initool set filename.ini section1 abc zyx

[section1]
abc=zyx
[section2]
foo=bar

C:\> initool set filename.ini section1 abc zyx > filename.ini
C:\ initool get filename.ini

[section1]
abc=zyx

C:\>

In the above example, I use set to change the value of one key, and then pipe that output back to the original filepath. Although the stdout of set shows all the keys of the original file with just the one key that has changed (this is expected), when it is piped back to the filepath, it overwrites the entire file with just the modified line, deleting all other lines (this is bad).

It's not the end of the world to write the output to a temp file and back, but it's an extra step that seems like it shouldn't be necessary. I'm more concerned by the fact that the stdout from the set command doesn't get written accurately when piped to a file.

Just noticed this line in the ReadMe

One exception is if you redirect initool's output to the same file as input, which results in an empty file like with other programs.

I guess I'm not familiar enough with whatever "other programs" you're referencing, so I suppose the behavior described in my issue might be expected/desirable. I can't imagine why that would be the case, but feel free to close the issue if you don't plan to fix it.

That line about "other programs" in the readme needs to explain the issue better. In short, this is operating system behavior, and individual programs do not control it. Redirecting a program's output to a file with > truncates the file's contents to nothing before the program gets control.

Check out https://superuser.com/questions/597244/why-does-redirecting-the-output-of-a-file-to-itself-produce-a-blank-file for a longer explanation. While it is about Unix, it applies to Windows. I have added a link to the same Super User question to the readme line.

I don't want to implement overwrite behavior in initool right now, so instead I have added a Windows batch file and a Unix shell script that help you modify the file input file correctly. The batch for Windows is called initool-overwrite.cmd. Please test it and tell me if it helps.