1

I have a notes.ini file with the following starting line:

[Notes]
Directory=c:\lotus\notes\data

I would like to change the "Directory=" parameter to Directory = D:\Users\fr21466\AppData\Roaming\notes

I´m trying this code:

@echo off
set file=c:\lotus\notes\notes.ini
set newline=Directory=%appdata%\notes
set insertline=2
set output=%appdata%\notes\notes.ini
(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^< "%file%"') do (
    if "%%~a"=="%insertline%" (     
        echo %newline%    
        REM ECHO.%%b    
    ) ELSE (     
        echo.%%b     
    )    
)) > %output%

But the output file is generating this:

It is generating the file, but cutting the symbol: "[" in the first column

Notes]
Directory=D:\Users\fr21466\AppData\Roaming\notes

Can someone please help me to solve this?

Thank You!

rojo
  • 24,000
  • 5
  • 55
  • 101
os3pimpas
  • 11
  • 1
  • What happens if you change `delims=[]"` to `delims=]"`? – Compo Jul 11 '17 at 17:10
  • If one of the answers below was helpful, please consider choosing one to mark as accepted. [See this page](http://meta.stackexchange.com/questions/5234/) for an explanation of why this is important. – rojo Jul 13 '17 at 13:03

3 Answers3

1

Try ini.bat (the second one, under the Update section).

copy "c:\lotus\notes\notes.ini" "%appdata%\notes"
ini.bat /s Notes /i Directory /v "%appdata%\notes" "%appdata%\notes\notes.ini"

To fix your current script, try replacing find with findstr and delims=:. The /N option with findstr prefaces lines with nn: rather than [nn], so your ini sections won't be clobbered when their surrounding brackets are treated as a successive delimiter.

@echo off & setlocal
set "file=c:\lotus\notes\notes.ini"
set "newline=Directory=%appdata%\notes"
set "insertline=2"
set "output=%appdata%\notes\notes.ini"
(for /f "tokens=1* delims=:" %%a in ('findstr /n /v "##" ^< "%file%"') do (
    if "%%~a"=="%insertline%" (     
        echo(%newline%    
        REM ECHO.%%b    
    ) ELSE (     
        echo(%%b     
    )    
)) > "%output%"
rojo
  • 24,000
  • 5
  • 55
  • 101
0
@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q45036613.txt"
SET "outfile=%destdir%\outfile.txt"
set file=%filename1%
set newline=Directory=%appdata%\notes
REM set insertline=2
set output=%outfile%
SET "notessection="
(FOR /f "delims=" %%x IN (%file%) DO (
  for /f "tokens=1* delims==" %%a in ("%%x") do (
   IF NOT DEFINED notessection ECHO(%%x
   IF DEFINED notessection (
    IF /i "%%a"=="Directory" (
     ECHO(%newline%
     SET "notessection="
    ) ELSE (
     ECHO(%%x
    )
   )
   IF /i "%%a"=="[Notes]" SET "notessection=Y"
  )
)) > %output%

GOTO :EOF

I used a file named q45036613.txt containing your data + more for my testing. I've also adjusted the paths to suit my system.

This version doesn't rely on the position of the directory entry; it looks for the [Notes] line and adjusts the directory entry that follows by using the characteristic that if defined interprets the run-time value of a variable. Hence, wait for [Notes] then wait for directory

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

To fix it remove [ from line 6

Compo
  • 36,585
  • 5
  • 27
  • 39
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/16680949) – ADreNaLiNe-DJ Jul 12 '17 at 12:11
  • Have you tried it, @ADreNaLiNe-DJ, does removing the character I mentioned and running the code again rectify the issue. And does the response do so without completely changing the original code and writing an essay along side it. Finally I did respond as a comment and then submitted my response as an answer because it exactly solves the issue of the missing square bracket reported in the code output. – Compo Jul 12 '17 at 14:20
  • Answering with 1 line and without explanations is useless. It's not the pricinpal goal of StackOverflow. – ADreNaLiNe-DJ Jul 12 '17 at 14:22
  • It isn't useless at all, it is a full and exact answer, no explanation is necessary, they only had a typo. StackOverflow is here to help with programming issues, their issue was solved by removing a single character, they did not ask for a tutorial or for me to copy and paste information from cmd.exe help or to post all of the rubbish already provided below which also doesn't explain what their issue was and just changes their code. – Compo Jul 12 '17 at 16:03