tabulators

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

tabulators

#1 Post by doscode » 30 Mar 2012 09:42

i have three questions concering tabulators:

1) How to add a tabulator to output, for example to this code:
echo Cplumn1 tabulator column2 > file.txt

2) How to remove multiple tabulators from a file? Not all tabulators, but all tabulators that are not solitary. In situation that I want to read a file with for /F loop and with tabulator as a delimiter.

3) I would like to count characters in string %%C which is column 1. Then to round down the number divided by 8: round(stringlength/8) and subtract the result from 40.

n = 40-round(strlen(%%C)/8);

This is calculation for count of tabulators which I want to put after column 1. But I would need it to make it in CMD, to have sense. How to integrate the calculation in loop that adds the tabulators into the string.
Last edited by doscode on 30 Mar 2012 10:07, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: tabulators

#2 Post by abc0502 » 30 Mar 2012 09:59

hi, sorry for asking but what is "tabulators" :oops:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: tabulators

#3 Post by foxidrive » 30 Mar 2012 10:06

doscode wrote:How to add a tabulator to output, for example to this code:
echo Cplumn1 tabulator column2 > file.txt


You need to use a text editor to insert a TAB character in the code, like Notepad.

Some editors can be set to convert TABs to spaces so they can't be used unless you configure the text editor



doscode wrote:How to remove multiple tabulators from a file? Not all tabulators, but all tabulators that are not solitary. In situation that I want to read a file with for /F loop and with tabulator as a delimiter.


I don't understand what you mean by solitary (maybe not three TABs in a row?) and what you want to do. Please give an example. For /f uses TABs and spaces as delimiters by default.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: tabulators

#4 Post by doscode » 30 Mar 2012 10:12

Here it is not possible to paste tab, so in case that I would use space instead tab.

In this sentence are six solitary spaces.

Code: Select all

But   in     this    sentence    are     not solitary   spaces.


OK, than I will ask somebody to make some binary program to me to do that.

Let me know how could I remove the tabs that are not "solitary". So to tabtabtab change to tab.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: tabulators

#5 Post by !k » 30 Mar 2012 10:20

1)
http://www.dostips.com/forum/viewtopic.php?f=3&t=1733 wrote::: 9 0x09 011 TAB (horizontal tab)

or simple use right text editor

Code: Select all

set "Tab=   "
echo Cplumn1%Tab%column2 > file.txt

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: tabulators

#6 Post by !k » 30 Mar 2012 10:29

2)
"for/f" loop w/o "delims" uses tab & spase as delimiters. Use it.

or use right text editor again

Code: Select all

set "Tab=   "
for /f "tokens=1,2* delims=%Tab%" %%a in (file.txt) do echo %%a%Tab%%%b

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: tabulators

#7 Post by doscode » 30 Mar 2012 10:42

!k wrote:
http://www.dostips.com/forum/viewtopic.php?f=3&t=1733 wrote::: 9 0x09 011 TAB (horizontal tab)



This part of job is too hard for me, so I don't know what to do with it. But man, I love you! The trick with SET is so simple and effective!

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: tabulators

#8 Post by !k » 30 Mar 2012 10:51

doscode wrote:This part of job is too hard for me
And I inattentive :oops:
dbenham wrote::: Aborts with an error message to stderr and errorlevel 1 if IntVal
:: corresponds to one of the following problematic characters:

so text editor is single way :roll:

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: tabulators

#9 Post by Aacini » 30 Mar 2012 12:35

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
set line=One!TAB!Two!TAB!!TAB!!TAB!Three
echo !line!
for /F "tokens=1-3" %%a in ("!line!") do echo %%a %%b %%c

Code: Select all

One     Two                     Three
One Two Three
Previous Batch file show:

- How to create a TAB in a Batch file via 'reg query' trick in Win XP (I don't know if it works in other Win versions).
- That multiple TAB's works like single delimiter in FOR /F command

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: tabulators

#10 Post by doscode » 30 Mar 2012 12:41

!k wrote:so text editor is single way :roll:


I am working on the point 3, calculation of the tabulators count and adding them to string. I have this code:

Code: Select all

set "Myvar=Hello"
set #=%MyVar%
set strlen=0
:loop
if defined # (set #=%#:~1%&set /A strlen += 1&goto loop)

echo LEN: %strlen%
SET /A tabscount=(40-%strlen%)/8
echo Tabs count: %tabscount%
echo counting...
FOR /L %%G IN (0,1,tabscount) DO echo %%G
pause


Actually I don't understand what does the set on line 5, but how to finish it? I try to use the loop but no "counting" happens. What I need to do now is to add the tabs to string Myvar so many times as the tabscount determines. Can you help me with the loop?

Then how to do the adding in DO block? Should something like this work SET "tabs=% %" Or rather SET "Myvar.= "?
Last edited by doscode on 30 Mar 2012 13:34, edited 2 times in total.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: tabulators

#11 Post by doscode » 30 Mar 2012 12:45

Aacini wrote:Previous Batch file show:

- How to create a TAB in a Batch file via 'reg query' trick in Win XP (I don't know if it works in other Win versions).
- That multiple TAB's works like single delimiter in FOR /F command


Thanks for code. I will try it as soon as possible.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: tabulators

#12 Post by doscode » 31 Mar 2012 06:20

I worked on point 3) and I did some codes. i have two codes, first the main loop, that reads directory list and second code that calculates count of tabulators and adds tabulators to list. When I paste the second code to the first code, so there is some error saying "/8 not expected".

Could you help me to fix errors in this scipt?

Code: Select all

@echo off
set "Tab=   "

del file.txt

for /f "delims=\" %%a in ("%cd%") do set "home=%%~nxa"

cd ..

IF NOT EXIST "directories.conf" (
 cd ".\%home%\"
 CALL ".\%home%\make directory list.bat"
 cd ..
 )

chcp 1250

FOR /F "tokens=* delims= usebackq" %%R IN ("directories.conf") DO (
 IF EXIST ".\%%R\scenery" (
  echo %%R
  SET "string=%%R"

  SET #=%string%
  SET strlen=0

  :loop
  if defined # (SET #=%#:~1%&SET /A strlen += 1&goto loop)

  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 /A tabscount=1
   )
  )

  echo counting...
  :appendTabs
  if %tabscount% gtr 0 (
    SET "string=%string%%TAB%"
    SET /A tabscount-=1
    goto :appendTabs
  )

  echo %string%>>file.txt
pause


 ) ELSE (ECHO NOT INCLUDED %%R
)

)

REM Echo Activation file created...
pause


PS: I did not added newline characters yet, because of crashing of the script.

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: tabulators

#13 Post by !k » 31 Mar 2012 08:18

8 not expected

Code: Select all

SET /A remainder=(40-strlen) %% 8

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

it could not be so

#14 Post by doscode » 31 Mar 2012 09:16

The error is not the error, it is modulus sign and the error mentioned "/".

I "removed" the error when I removed:

Code: Select all

IF EXIST ".\%%R\scenery" (


and

Code: Select all

) ELSE (ECHO NOT INCLUDED %%R
)


Where is problem?

And next error:

See this:

Code: Select all

cls
@echo off
REM set "Tab=   "

REM del file.txt

REM for /f "delims=\" %%a in ("%cd%") do set "home=%%~nxa"

REM cd ..

REM IF NOT EXIST "directories.conf" (
REM  cd ".\%home%\"
REM  CALL ".\%home%\make directory list.bat"
 REM cd ..
REM  )

REM chcp 1250

FOR /F "tokens=* delims= usebackq" %%R IN ("directories.conf") DO (
 IF EXIST ".\%%R\scenery" (
  REM echo %%R
  REM SET "string=%%R"

  REM SET #=%string%
  REM SET strlen=0

  REM :loop
  REM if defined # (SET #=%#:~1%&SET /A strlen += 1&goto loop)

  REM echo LEN: %strlen%
  REM SET /A tabscount=(40-strlen)/8
  REM SET /A remainder=(40-strlen)%8
  REM echo REMAINDER:%remainder%

  REM echo Tabs count: %tabscount%

  REM if %tabscount% equ 0 (
   REM if %remainder% gtr 0 (
    REM SET /A tabscount=1
   REM )
  REM )

  REM echo counting...

  :appendTabs

  if %tabscount% gtr 0 (
   REM  SET "string=%string%%TAB%"
   REM SET /A tabscount-=1
    goto :appendTabs
  )

  REM echo %string%>>file.txt
REM pause


 ) ELSE (ECHO NOT INCLUDED %%R
)

)

REM Echo Activation file created...
REM pause


Error Incorrect syntax near to :appendTabs block
Also something wrong

Aacini
Expert
Posts: 1886
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: tabulators

#15 Post by Aacini » 31 Mar 2012 23:43

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 /?.

Post Reply