changing specific .ini file and save it but doing it as .bat

January 3, 2018 at 03:26:13
Specs: Windows 10
Hello,
I'm new here and I would like to change an .ini file,
in fact:
-locate my .ini file
-delete what is inside my .ini file
-replace it with something else
-save it
-put it back where it belongs
all this with a batch file .bat
does someone will explain to me how can I do this?
exemple:
I've got an .ini file in D:\new folder\test.ini
in the test.ini file there is something wrote.
I just would like to change the writing by something else
and save it.

Thank you very much for your help.


See More: changing specific .ini file and save it but doing it as .bat

Report •

#1
January 3, 2018 at 06:46:52
try this

::----start of test.bat----
@echo off
set "pathtoini=D:\new folder\test.ini"
::making a backup for you just in case
copy "%pathtoini%" "D:\new folder\test.backup.ini"
echo off > %pathtoini%
echo testsentence >> %pathtoini%
::----end of test.bat----

edited by moderator: clutter


Report •

#2
January 5, 2018 at 00:13:06
An example of the initial .ini file content, and the intended result (after processing) would help immensly. I think that you want to replace specific content within the target .ini file, and not the whole file, but I'm usually wrong in my assumptions.

message edited by nbrane


Report •

#3
January 5, 2018 at 04:25:35
:: If there's only ONE file with the targat name
:: and it's on D:
:: and you want it to contain: some new line
::
::====== script starts here ===============
::
:: inimod.bat 2018-01-05 19:12:54.48
@echo off & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in ('dir/b/s d:\ABC.INI') do (
> %%a echo some new line
)
goto :eof
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
January 11, 2018 at 02:53:04
Hello,
thank you very much guys.
but unfortunately it doesn't work as expected...
In fact, "nbrane" was right.
I'll have to explain:
I want to change some lines inside the desktop.ini file
in a specific folder.
like this lines :
IconResource=(and put something else after the "=" sign)
LocalizedResourceName=(and put something else after the "=" sign)
that's it.
and of course save it.
to be able to change folder name and icon in a specific folder.
thank you very much for your help.


Report •

#5
January 11, 2018 at 07:32:28
Are the contents of the file desktop.ini always the same, in other words, after the script is run the data in the file is always the same?

In that case just tell us what needs to be put into the file.
Because in that case we can just hardcode what needs to be written to the file, which will take us about 1 minute to write :)

nvm didnt read properly again haha.

I'm just going to wait till nbrane responds...

i5-6600K@4.670GHz/4.336GHz cache@1.33v | 2x4GB Crucial-DDR4-2133@ 14-4-14-28 1T 2668MHz@1.3v | ASUS Z170-K | Samsung 250GB SSD 850 EVO | MSI RX 570 4GB@1380cc&2100mc bios-powertune-mod | Corsair VS450


Report •

#6
January 11, 2018 at 15:14:25
Hey, Hi,
no the contents will be different but I just have five different desktop.ini for the time being to setup so,
this should not be a problem.
I can do one .bat with the five different folder I presume...
or even 5 .bat I don't care...
the content is different but If you can write the right one for one, it will be good for all of them anyway...
Thanks again.

Report •

#7
January 12, 2018 at 12:45:01
This one worked in my tests. The structure / nesting is atrocious, but done to avoid
using non-system variables which don't handle special characters very well. And this
is just a prototype core, not finalized by any means: input, output filenames and
replacement data are all "hard-wired".

@echo off>new & setlocal
set name=replaced name data
set icon=new icon
set infile=test.ini

:: make a backup
copy %infile% temp.ini
for /f "tokens=*" %%a in (%infile%) do (
for /f "tokens=1* delims==" %%b in ("%%a") do (
if /i "%%c" equ "localizedresourcename" (
>>new echo %%c=%name%
) else (
  if /i "%%c" equ "iconresource" (
  >>new echo %%c=%icon%
  ) else (
    >>new echo %%a
  )
 )
)
)
echo finished conversion: ---------------
more new.ini

message edited by nbrane


Report •

#8
January 23, 2018 at 05:56:45
Hello,
thank you very much nbrane but it doesn't work at all.
so I send you what is needed to be changed in
my desktop.ini which is in some place I must indicate of course :
I need to change the location point:
"
[.ShellClassInfo]
IconResource=.........................
IconIndex=
LocalizedResourceName=........................
[ViewState]
Mode=
Vid=
FolderType=Generic"

if you have some time to fix it,
thank you very much.
Regards.


Report •

#9
January 24, 2018 at 18:58:24
I fixed a mistake in my variables. see if this works:
@echo off>new & setlocal
set name=replaced name data
set icon=new icon
set infile=test.ini

:: make a backup
copy %infile% temp.ini
for /f "tokens=*" %%a in (%infile%) do (
for /f "tokens=1* delims==" %%b in ("%%a") do (
if /i "%%b" equ "localizedresourcename" (
>>new echo %%b=%name%
) else (
  if /i "%%b" equ "iconresource" (
  >>new echo %%b=%icon%
  ) else (
    >>new echo %%a
  )
 )
)
)
echo finished conversion: ---------------
more new

Note that this only works if there's only ONE instance of the two target strings in the file. I wrote another one that avoids that, but it's even messier than this one, so I'll wait and see if this works.

message edited by nbrane


Report •

#10
January 28, 2018 at 15:38:58
Hello nbrane and thank you very much,
but it still doesn't work.
the backup is well done, ok.
in fact the problem is it change absolutely nothing in the desktop.ini file or just not save it I presume:
"
set name=TEST
set icon=C:\Program Files\ICON\GParted.ico
set infile=desktop.ini

:: make a backup
copy %infile% temp.ini
for /f "tokens=*" %%a in (%infile%) do (
for /f "tokens=1* delims==" %%b in ("%%a") do (
if /i "%%b" equ "LocalizedResourceName" (
>>new echo %%b=%name%
) else (
if /i "%%b" equ "IconResource" (
>>new echo %%b=%icon%
) else (
>>new echo %%a
)
)
)
)
echo finished conversion: ---------------
more new
"
Thank you very much, if you have some time to check up what I've done and fix it.
Regards.


Report •

#11
January 28, 2018 at 15:44:45
In case, as you wrote, if, it's still to messy to change to line at the same time,
I can do one .bat and another one few second after....
if it will be possible as well to wrote on the .bat where the desktop.ini is located like : D:\TEST\desktop.ini
it would be great.
Thanks again.
Regards.

Report •

#12
January 28, 2018 at 20:34:53
It may be a Unicode issue. You can check this from command-prompt by navigating to the directory and execute "edit desktop.ini"
If it has "spaces" between each letter, then the batch fails as written. The following fix might fix:
@echo off>new & setlocal
set name=replaced name data
set icon=new icon
set infile=test.ini

:: make a backup
copy %infile% temp.ini
for /f "tokens=*" %%a in ('type %infile%') do (
for /f "tokens=1* delims==" %%b in ("%%a") do (
if /i "%%b" equ "localizedresourcename" (
>>new echo %%b=%name%
) else (
  if /i "%%b" equ "iconresource" (
  >>new echo %%b=%icon%
  ) else (
    >>new echo %%a
  )
 )
)
)
echo finished conversion: ---------------
more new

Note that the new file will not be Unicode. I doubt that makes a difference to the OS, but hence the backup prior. "new" will not replace desktop.ini until you copy it over.
ps: if Unicode, this test will illustrate:
@echo off & setlocal
for /f "tokens=*" %%a in (desktop.ini) do echo %%a
echo all content that batch sees...
pause

for /f "tokens=*" %%a in ('type desktop.ini') do echo %%a
echo any better?
goto :eof

message edited by nbrane


Report •

#13
January 30, 2018 at 14:53:53
Hello nbrane and thanks you very much again,
the last one was working better.
what I don't understand is why this bat makes a backup as just a "new" file why any .---
anyway it seems to work for creating the right file.
now can I change the name to "desktop.ini" file?
or better, why can you juste create a new desktop file and just remove the old one?
I don't know if this could be possible.
thank you for your help anyway.
Regards.
"
@echo off>new & setlocal
set name=TEST
set icon=C:\Program Files\ICON\GParted.ico
set infile=desktop.ini

:: make a backup
copy %infile% desktop.ini
for /f "tokens=*" %%a in (%infile%) do (
for /f "tokens=1* delims==" %%b in ("%%a") do (
if /i "%%b" equ "LocalizedResourceName" (
>>new echo %%b=%name%
) else (
if /i "%%b" equ "IconResource" (
>>new echo %%b=%icon%
) else (
>>new echo %%a
)
)
)
)
echo finished conversion: ---------------
more new
"


Report •

#14
January 30, 2018 at 20:07:56
#1)
what I don't understand is why this bat makes a backup as just a "new" file why any .---
anyway it seems to work for creating the right file.
now can I change the name to "desktop.ini" file?

#2)
or better, why can you juste create a new desktop file and just remove the old one?
I don't know if this could be possible.

the file "new" is just a temporary holding-file for the processed "desktop.ini" content. It is meant to be "moved" to the desktop.ini location from which it was derived. this is what you described in item #2.

@echo off>new & setlocal
set name=TEST
set icon=C:\Program Files\ICON\GParted.ico
set pth=\users\owner
set infile=%pth%\desktop.ini

:: make a backup
copy %infile% temp.ini
for /f "tokens=*" %%a in ('type %infile%') do (
for /f "tokens=1* delims==" %%b in ("%%a") do (
if /i "%%b" equ "localizedresourcename" (
>>new echo %%b=%name%
) else (
  if /i "%%b" equ "iconresource" (
  >>new echo %%b=%icon%
  ) else (
    >>new echo %%a
  )
 )
)
)
:: this does the move, replacing the desktop.ini with the new content
:: the path is just an example, you will need to supply your correct path to the ini file
move new %pth%\desktop.ini

your last posting had some pretty trashed up code which will probably fail and have lots of errors. recommend using this one and not trying to use that one.

Report •

#15
February 4, 2018 at 08:04:21
Here is an example that i made before in batch : Hackoo_Change_Icon_Folder.bat

For downloading the whole project with the icon archived with winrar for testing on your side : Hackoo_Icon_Folder_Changer.rar

If you want to look for another project too made by vbscript and HTA
just here ==> How can i show the change icon dialog box, with a vbscript ?

@echo off
Title %~nx0 by Hackoo 2017
mode con cols=80 lines=3 & Color 9E
if _%1_==_Main_ goto :Main
:getadmin
echo(
echo "%~nx0" : Running Admin Shell
set vbs=%temp%\getadmin.vbs
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
::**********************************************************************************
:Main
set "fld=%userprofile%\Desktop\Hackoo"
set "ico=%~dp0icon.ico"
md "%fld%\icons" >nul 2>&1
echo(
echo Please wait a while... we execute the commands to change icon to the folder
If exist "%ico%" (
Attrib -s "%ico%"
) else (
cls & mode con cols=85 lines=3 & color 0C
echo(
echo The file "icon.ico" should be in the same folder as "%~nx0"
Timeout /T 5 /nobreak>nul & exit
)
if exist "%ico%" ( copy "%ico%" "%fld%\icons\icon.ico" /y >nul 2>&1 )
if exist "%fld%\desktop.ini" ( attrib -h -s -a "%fld%\desktop.ini" >nul 2>&1 )

(
echo [.ShellClassInfo]
echo IconResource=icons\icon.ico,0
)> "%fld%\Desktop.ini"

attrib +h +s +a "%fld%\Desktop.ini"
attrib +r "%fld%"
attrib +h "%fld%\icons"
Rem To refresh the explorer by killing and restart it
taskkill /im explorer.exe /f >nul & start explorer

If "%ErrorLevel%" EQU "0" (
cls & echo( & color 9E
echo The commands are executed successfully !
Timeout /T 4 /nobreak>nul
Explorer.exe /e,/root,"%fld%"
) else (
cls & echo( & color 0C
echo There was an error occured while this program is running !
Timeout /T 4 /nobreak>nul
)



Report •

#16
February 9, 2018 at 04:21:27
Hello nbrane,
yes exactly.
it creates just a new which is a file with the right writing in it...
Thanks.

Report •

#17
February 9, 2018 at 04:24:50
Hello Hackoo,
great your .bat.
but how can I indicate to this one, where my .ico is?
should this one must be in the same folder as the desktop.ini I want to change or could
it be somewhere else?
Thanks.

Report •

#18
February 9, 2018 at 04:38:33
Try to change this line set "ico=%~dp0icon.ico" in my code %~dp0 means the current folder
If you know exactly the whole path of your icon like in your example set icon=C:\Program Files\ICON\GParted.ico
You can write it like this :
set "icon=%ProgramFiles%\ICON\GParted.ico"
The script can create a folder named Icons and copy inside it your icon and hide it.
Did you tried my script from Archived RAR ?

message edited by Hackoo


Report •

#19
February 12, 2018 at 04:09:58
Hello Hackoo,
yes I did.
and thanks very much.
this is why I've answered.
so I change it but in your script I cannot see, in fact, for which
folder you change the .icon.
could it be possible as well to change the name of this folder?
could be great...
thanks again.

Report •

#20
February 12, 2018 at 08:44:19
Give a try for this new script Changing multiple folder icons by dropping folder as input to a batch script

CustomIconFolder.bat

@echo off & Setlocal EnableDelayedExpansion
Color 0A & Mode 78,5
set "ScriptName=%~nx0"
set /a "count=0"
Title Drag and Drop a folder or multi folders over "%ScriptName%"
if "%~1"=="" goto error
for %%a in (%*) do ( 
	set /a "count+=1"
	set "$Folder=" & pushd "%%~a" 2>nul && ( popd  & set "$Folder[!count!]=%%~a"
		) || ( 
		set "$Folder="
		Setlocal DisableDelayedExpansion
		color 0C & echo(
		echo    "%%~a"
		echo     ====^> is not a folder !
		echo     Exiting the script . . .
		endlocal
		Timeout /T 3 /nobreak>nul & exit
	)
)
Rem Dispaly selected folders
Mode 75,10
Setlocal EnableDelayedExpansion
for /L %%i in (1,1,%count%) do (
	If [%count%] EQU [1] (
		echo You have chosen this folder : 
		echo [%%i] - "!$Folder[%%i]!"
	) else (
		echo [%%i] - "!$Folder[%%i]!"
	)
)
Timeout /T 2 /nobreak>nul
Mode 78,8 & Cls & echo(
echo Please drag and drop your custom icon to be set to your folder over here
echo and press enter...
echo(
echo Or just write the whole path and press enter ...
Set /p "Icon="
If [!Icon!] EQU [] (
	cls & echo(
	echo  The selected icon is : "%systemroot%\system32\shell32.dll,47"
	Timeout /T 3 /nobreak>nul
		for /L %%i in (1,1,%count%) do (
			echo !$Folder[%%i]!
			Call :WriteDesktopIni !$Folder[%%i]! "%systemroot%\system32\shell32.dll,47"
		)
) Else (
		for %%a in (!Icon!) do ( set "Icon_Name=%%~nxa" & set "Ext=%%~xa" )
		If /I [!Ext!] EQU [.ICO] (
			cls & echo(
			echo The selected icon is : "!Icon_Name!"
			echo From this path : !Icon!
			Timeout /T 3 /nobreak>nul
			for /L %%i in (1,1,%count%) do (
				echo "!$Folder[%%i]!"
				Copy /y !Icon! "!$Folder[%%i]!\!Icon_Name!">nul 2>&1
				Attrib +H "!$Folder[%%i]!\!Icon_Name!">nul 2>&1
				Call :WriteDesktopIni !$Folder[%%i]! "!Icon_Name!"
			)
		) else (
			Cls & Color 0C & echo( 
			echo The extension : [*!Ext!] is not allowed
			Timeout /T 3 /nobreak>nul
		)
)
cls
echo(
echo  End of the script "%ScriptName%"
Timeout /T 2 /nobreak>nul & Exit
::***************************************************************************
:WriteDesktopIni [Folder] [Icon]
if exist "%~1\desktop.ini" ( attrib -h -s -a "%~1\desktop.ini" >nul 2>&1 )
(
	ECHO [.ShellClassInfo] 
	ECHO IconResource=%~2
)>"%~1\desktop.ini"
attrib +S +H +A "%~1\desktop.ini">nul 2>&1
attrib +R %~1>nul 2>&1
goto :eof
::***************************************************************************
:Error
Mode 78,10
echo( & echo(
echo   You should drag and drop a folder or multi folders over "%ScriptName%"
echo(
echo   Or Usage in command line like this syntax : 
echo(
echo   %~nx0 "FolderPath1" "FolderPath2" "FolderPath3" "FolderPath4"
Timeout /T 10 /nobreak>nul & exit
::***************************************************************************

message edited by Hackoo


Report •

Ask Question