Solved copy names in square brackets from an ini file to txt file

November 4, 2019 at 11:13:01
Specs: Windows 10
I need to copy names in square brackets from an ini file and be saved in a txt file
and then those names I will use to create a txt file with the name extracted from the ini file
Can it be done in one script or better to use two scripts?

See More: copy names in square brackets from an ini file to txt file

Reply ↓  Report •

✔ Best Answer
November 7, 2019 at 12:39:01
Ok, fixed the problem with the "undefined character"s in the source-file. You were right! The parentheses were bombing the bat. Here's the updated script, now having the benefit of the correct source-file content (I'm assuming!):
::---------- begin batchscript saved as drew.bat on my end
:: this next line is important. BE SURE you spell it right. the caps are to help with that.
@echo off & setlocal EnableDelayedExpansion
:: this next line not really needed but leaving it in anyway
set fname=nul
set infile=%1
if not defined infile (
echo usage: %0 inputfilename
echo ouput is appended to filenames drawn from within [] in the inputfile
echo Since output is appended, remember to delete the files if you want fresh start
goto :eof
)
for /f "tokens=*" %%a in (%infile%) do (
echo %%a | findstr "\<\[.*\]\>"&& (
call :xx %%a
) || (
>>!fname! echo %%a
)
)
goto :eof

:xx
set fname=%~1.txt
set fname=%fname:[=%
set fname=%fname:]=%
echo next file: %fname%
:: ---------- end batch-script

message edited by nbrane



#1
November 4, 2019 at 14:41:45
can you give me a few example lines (or the whole .ini file if its not too long)
this will help me out with making less mistakes.
i should be able to do it in 1 .bat file (script file)

also its almost midnight and i have work tommorow, so ill see what i can do.
worst case ill answer in 20 hours

i5-6600K[delid]@4.836GHz Core/4.630 Cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL18@3018MHzCL12@1.465v | Sapphire Nitro+ SE RX 590 8GB@1640Mhz core@1.167v/2236MHz


Reply ↓  Report •

#2
November 5, 2019 at 00:14:43
You should at least post the contents of your ini file to build an idea for choosing the best scripting language in your case !

Reply ↓  Report •

#3
November 5, 2019 at 07:19:57
sorry
the text of .ini

filtering=true
midi_gain=100
mute=false
speech_volume=192
native_mt32=true
mt32_device=mt32
local_server_port=12345
talkspeed=60

[atlantis]
description=
path=

[atlantis-1]
description=
extra=Floppy
path=c:\director\
gameid=
guioptions=sndNoSpeech

[atlantis-amiga]
description=
extra=
path=c:\director\
gameid=atlantis
platform=
guioptions=sndNoSpeech midiAmiga

need create files atlantis.txt, atlantis-amiga.txt, atlantis-1.txt. etc.etc
thanks


Reply ↓  Report •

Related Solutions

#4
November 5, 2019 at 10:49:38
So, you want to extract and save those lines in a text file from the ini file ?
Demo Here

atlantis
atlantis-1
atlantis-amiga


message edited by Hackoo


Reply ↓  Report •

#5
November 5, 2019 at 11:54:35
I want to extract and save those lines in a name text file from the ini file
the files in blank only the names

atlantis.txt
atlantis-1.txt
atlantis-amiga.txt

thanks and sorry i use translator

message edited by drewdty


Reply ↓  Report •

#6
November 5, 2019 at 12:23:48
Give a try for this batch file : ExtractData_From_INI_File.bat

@echo off
Title Extract Data between two square brackets from an ini file
Set "InputFile=Test.ini"
Set "OutputFile=OutputFile.txt"
Call :ExtractData "%InputFile%" "%OutputFile%"
If Exist %OutputFile% Start "" %OutputFile%
Exit
::-----------------------------------------------------------------------------------
:ExtractData <InputFile> <OutputFile>
(
	echo WScript.StdOut.WriteLine Extract("%~1"^)
	echo Function Extract(Data^)
	echo Dim strPattern,strResult,oRegExp,Match,colMatches 
	echo Data = WScript.StdIn.ReadAll
	echo strPattern = "^\[(.+)\]"
	echo Set oRegExp = New RegExp
	echo oRegExp.Global = True
	echo oRegExp.Multiline = True
	echo oRegExp.IgnoreCase = True 
	echo oRegExp.Pattern = strPattern
	echo set colMatches = oRegExp.Execute(Data^)
	echo For Each Match in colMatches
	echo 	strResult = strResult ^& Match.SubMatches(0^) ^&".txt" ^& vbcrlf
	echo Next
	echo Extract = strResult
	echo End Function
)>"%tmp%\%~n0.vbs"
cscript //nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
Exit /B
::----------------------------------------------------------------------------------


Reply ↓  Report •

#7
November 5, 2019 at 19:20:25
@echo off & setlocal
set fname=nul
set infile=%1
if not defined infile (
echo usage: %0 inputfilename
echo ouput is appended to filenames drawn from within [] in the inputfile
goto :eof
)
for /f "tokens=*" %%a in (%infile%) do call :xx "%%a"
goto :eof

:xx
:: check for the square brackets to get next filename
echo %~1 | findstr "\<\[.*\]\>"|| (
>>%fname% echo %~1
goto :eof
)

set fname=%~1
set fname=%fname:[=%
set fname=%fname:]=%
Rem OR: set fname=%fname:~1,-1%
:: ---------- end script
this creates (or appends to) 3 files "atlantis*" that are inside the square brackets in the source-file. The bracket-items are assumed to be singular per line, and not mixed in stream.

message edited by nbrane


Reply ↓  Report •

#8
November 6, 2019 at 09:01:54
neither of the two examples works for me
in both of them I create a bat file in a folder next to the ini file
I run it as admin but it doesn't create any txt file
I surely do not do well
I'm sorry

Reply ↓  Report •

#9
November 6, 2019 at 18:02:30
No problem. It worked fine on my end. created three files:

file atlantis:
description=
path=

file atlantis-1:
description=
extra=Floppy
path=c:\director\
gameid=
guioptions=sndNoSpeech

file atlantis-amiga:
description=
extra=
path=c:\director\
gameid=atlantis
platform=
guioptions=sndNoSpeech midiAmiga

Did you supply the name of the source-file? If script is saved as xxx.bat, then you should run it:
xxx aaaaa.ini


Reply ↓  Report •

#10
November 7, 2019 at 07:05:20
oooook now I understand, but
Now I run it from cmd as you say
it creates only three files of many more that I should do
I get this error
It was not expected \ at this time.
for what is this?
Is it possible that he found an undefined character in the bat?
we are advancing
thank you very much really
EDIT:
otherwise the files are created but without txt extension.
REEDIT:
haaaa sorry I used it with another ini
the new ini changes some things
how silly I am
I leave you the same modified ini sorry

[scummvm]
gui_browser_native=true
filtering=true
midi_gain=100
mute=false
speech_volume=192
native_mt32=true
mt32_device=mt32
local_server_port=12345
talkspeed=60
gui_use_game_language=true
subtitles=true
multi_midi=false
fullscreen=false
updates_check=0
versioninfo=2.1.0
render_mode=vga
browser_lastpath=G:\Hyperspin\Games\ScummVM
gm_device=fluidsynth
soundfont=Y:\HyperSpin\Emulators\ScummVM\Scummvm_1.8.0\FluidR3 GM.sf2
sfx_volume=192
music_volume=192
speech_mute=false
lastselectedgame=bargon-amiga
music_driver=mt32
opl_driver=auto
aspect_ratio=false
temp_selection=bargon-amiga
autosave_period=300
gui_language=es_ES
enable_gs=false

[atlantis]
description=Indiana Jones and the Fate of Atlantis
path=G:\Hyperspin\Games\ScummVM\Indiana Jones and the Fate of Atlantis (CD, Spanish)\
gameid=atlantis

[atlantis-1]
description=Indiana Jones and the Fate of Atlantis (Floppy)
extra=Floppy
path=G:\Hyperspin\Games\ScummVM\Indiana Jones and the Fate of Atlantis (CD, Spanish)\
gameid=atlantis
guioptions=sndNoSpeech

[atlantis-amiga]
description=Indiana Jones and the Fate of Atlantis (Amiga/Amiga)
extra=Amiga
path=G:\Hyperspin\Games\ScummVM\Indiana Jones and the Fate of Atlantis (CD, Spanish)\
gameid=atlantis
platform=amiga
guioptions=sndNoSpeech midiAmiga

excuse my stupidity

message edited by drewdty


Reply ↓  Report •

#11
November 7, 2019 at 07:15:54
@Hackoo now your bat works for me but I need
convert that list into txt files
many thanks

Reply ↓  Report •

#12
November 7, 2019 at 12:07:11
@derwdty
What did you mean by "convert that list into txt files" ?
Please be more explicit when you ask a question !
What's your final goal, please explain your aim !

Reply ↓  Report •

#13
November 7, 2019 at 12:39:01
✔ Best Answer
Ok, fixed the problem with the "undefined character"s in the source-file. You were right! The parentheses were bombing the bat. Here's the updated script, now having the benefit of the correct source-file content (I'm assuming!):
::---------- begin batchscript saved as drew.bat on my end
:: this next line is important. BE SURE you spell it right. the caps are to help with that.
@echo off & setlocal EnableDelayedExpansion
:: this next line not really needed but leaving it in anyway
set fname=nul
set infile=%1
if not defined infile (
echo usage: %0 inputfilename
echo ouput is appended to filenames drawn from within [] in the inputfile
echo Since output is appended, remember to delete the files if you want fresh start
goto :eof
)
for /f "tokens=*" %%a in (%infile%) do (
echo %%a | findstr "\<\[.*\]\>"&& (
call :xx %%a
) || (
>>!fname! echo %%a
)
)
goto :eof

:xx
set fname=%~1.txt
set fname=%fname:[=%
set fname=%fname:]=%
echo next file: %fname%
:: ---------- end batch-script

message edited by nbrane


Reply ↓  Report •

#14
November 7, 2019 at 13:14:53
@ hackoo sorry, if you don't explain me well
my intention was to create txt files whose name was
those who were [in square brackets] of an ini file, which I didn't know
it was if it could be done in a single batch or had to first create a list and make another batch to create the txt files
Anyway, your batch serves me for another project
really thank you very much
and sorry again

Reply ↓  Report •

#15
November 7, 2019 at 13:26:21
@ nbrane you're a crack, now yes
A small problem is that the files are not empty
but it's the same, it only occupies 1K each file
I would like to know half of you
you are some geniuses
thank you

Reply ↓  Report •

#16
November 7, 2019 at 17:40:23
I think I know what you mean. You need to delete the files, or have the batch delete them for you. I am always hesitant to delete files from the batch, but adding this line at :xx should fix it

:xx
set fname=%~1.txt
set fname=%fname:[=%
set fname=%fname:]=%

:: added to remove old files
del %fname% 2>nul

echo next file: %fname%
::----------
Sound's like the rest is "in the bag" for now anyway.


Reply ↓  Report •

#17
November 9, 2019 at 00:45:55
Thanks again
Now better you have solved my problem

Reply ↓  Report •

Ask Question