To avoid this error enclose SET /A expression in quotes; also, you must double the percent-sign operator, as !k said:
Code: Select all
SET /A "tabscount=(40-strlen)/8"
SET /A "remainder=(40-strlen)%%8"
I took the liberty of modify somewhat your code. This is my version:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for /F "skip=4 delims=pR tokens=1,2" %%a in (
'reg query hkcu\environment /v temp' ) do set TAB=%%b
del file.txt
for %%a in ("%cd%") do set "home=%%~NXa"
cd ..
IF NOT EXIST directories.conf (
cd "%home%"
CALL "make directory list.bat"
cd ..
)
chcp 1250
FOR /F "delims=" %%R IN (directories.conf) DO (
IF EXIST "%%R\scenery" (
echo %%R
SET "string=%%R"
SET "#=%%R"
SET strlen=0
CALL :strLen
echo LEN: !strlen!
SET /A "tabscount=(40-strlen)/8"
SET /A "remainder=(40-strlen)%%8"
echo REMAINDER: !remainder!
echo Tabs count: !tabscount!
if !tabscount! equ 0 (
if !remainder! gtr 0 (
SET tabscount=1
)
)
echo counting...
for /L %%i in (1,1,!tabscount!) do (
SET "string=!string!%TAB%"
)
echo !string!>>file.txt
pause
) ELSE (
ECHO NOT INCLUDED %%R
)
)
REM Echo Activation file created...
pause
goto :EOF
:strLen
if defined # ( SET "#=%#:~1%"& SET /A strlen+=1 & goto strLen )
exit /B
Note that a literal TAB character inserted in your code does not correctly appear in the listing of this forum (I received it as 3 spaces). I use the method I suggested above instead.
I made several minor changes to what I guess should be, so perhaps I made some mistakes. For example, in your code:
Code: Select all
cd ".\%home%\"
CALL ".\%home%\make directory list.bat"
cd ..
the
".\%home%\" expression mean "the %home% directory below current directory" that is exactly the same than just
"%home%". However, after you entered into that directory with
cd ".\%home%\", the
CALL ".\%home%\make directory list.bat"imply that there is a
second directory with same name (%home%) below the first one. I think this is an error. I think the right code should be:
Code: Select all
cd "%home%"
CALL "make directory list.bat"
cd ..
When an IF or FOR commands are executing you must note that there is
pending code that will the completed until the IF or FOR terminates. For example, the code between parentheses in a FOR command will be repeatedly executed for all the values of the FOR set, right? The same behavior happen with the THEN or ELSE parts of an IF command. Please, pay attention to this phrase:
Any IF/FOR pending code is cancelled if a GOTO command is executed inside it. This mean in short that you can not include a GOTO inside any IF or FOR, except if the GOTO destination label is
outside of the first-level IF or FOR. In order to include a code segment that use GOTO inside an IF/FOR, the segment must be modified to not include GOTO or be extracted into a subroutine. For example, I extracted this code:
Code: Select all
:loop
if defined # (SET #=%#:~1%&SET /A strlen += 1&goto loop)
into this subroutine:
Code: Select all
:strLen
if defined # ( SET "#=%#:~1%"& SET /A strlen+=1 & goto strLen )
exit /B
and leave a
CALL :strLen in its place. Also, I changed this code:
Code: Select all
:appendTabs
if %tabscount% gtr 0 (
SET "string=%string%%TAB%"
SET /A tabscount-=1
goto :appendTabs
)
by this equivalent one, that not use GOTO:
Code: Select all
for /L %%i in (1,1,!tabscount!) do (
SET "string=!string!%TAB%"
)
I made several changes of % by !. In short: in order to use the value of any %variable% that may change
inside parentheses, you must write !variable! instead and insert a
SetLocal EnableDelayedExpansion command at beginning. For further details, type
SET /?.