Setting Variable from extracted line in 3rd Party File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cruzer250
Posts: 1
Joined: 06 Dec 2016 14:39

Setting Variable from extracted line in 3rd Party File

#1 Post by Cruzer250 » 06 Dec 2016 14:58

Hello dostips forums!

I am currently working on a script for personal pc care, involving cleaning, repair, personal optimizations, ect. The problem I am currently presented with would be easy to work around given that I chose not to work with an ini file, but In this specific case I would like to use an ini for keeping the script simple and easy to read/edit.

CleanConfig.ini

Code: Select all

[Lvl1Clean]
Directory,%temp%,all
Script=W7ClearEventLogs.cmd=blank

[Lvl2Clean]
Directory,C:\SomeFolder\Anotherfolder\,*.log
Script=SomeScriptToRun.cmd=blank
Directory,C:\AnotherFolder\Somefolder\,*.txt

[Lvl3Clean]
Script,SomeScript.cmd,blank
Script,AnotherScript.cmd,blank
Script,AnotherScript.cmd,blank
Directory,C:\SomeCache,*.txt


This file is pretty generic as of now and does not contain any real useful anything, it is just for now to get the script working properly in troubleshooting as I am writing.

Start.cmd

Code: Select all

@echo on
:start
cls
echo *******************
echo CMP Cleaner 1.0
echo *******************
echo.
echo [1] For Level 1 Clean
echo [2] For Level 2 Clean
echo [exit] to exit
echo.
set /p option=
if %option% ==1 (goto Level1Clean)
if %option% ==2 (goto Level2Clean)
goto start

:Level1Clean
for /f "tokens=*" %%a in (CleanConfig.ini) do (
  set parts=%%a
echo %parts%
  REM FOR /f "tokens=1,2,3 delims=," %%a IN (%parts%) do echo %%a & echo %%b & echo %%c
)
:next
pause

goto end
Rem Example
:Example
for /f "tokens=*" %%a in (Lvl1Clean.txt) do (
  Clean\%%a
)
pause

:end


There is still currently a bit missing from this file, but as for now it doesn't make a difference, as I am just working with the level 1 clean option for now. Under :Level1Clean I can easily get each line in the ini file to echo, and I were to add if the var equals an ini key then exit it works fine.

Where I'm having troubles is for some reason I am not able to set the "parts" variable to the line being extracted from the ini. When I turn echoing off to view what is going on it looks like it is being set just fine but when I echo "parts" it echos blank.

Sorry for the mess! So far this has been quickly set up and still needs to be cleaned up.

All help is appreciated! Thank you

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Setting Variable from extracted line in 3rd Party File

#2 Post by penpen » 06 Dec 2016 16:47

This is the same issue, with the same possible solutions that you could see here:
http://www.dostips.com/forum/viewtopic.php?p=50140#p50140.


In your special case, you may avoid reading the value of %%a from the environment variable "parts":

Code: Select all

:: ...
:Level1Clean
for /f "tokens=*" %%a in (CleanConfig.ini) do (
  set parts=%%a
echo %%a
  REM FOR /f "tokens=1,2,3 delims=," %%b IN (%%a) do echo %%b & echo %%c & echo %%d
)
:next
:: ...


penpen

Post Reply