Can I stop text pushing forward the following text in batch? - variables

I want to place a variable before text on the same line without pushing the text forward when the variable changes its amount of characters.
Eg.
#echo off
set var=1
:start
cls
echo Variable: %var% 'Text'
echo ' '
pause >nul
set /a var= %var% * 10
goto start
Every time the variable expands the text shifts to the right, is it possible to keep the text in the same place?

set your variable to a known lenght (adding spaces to it and cutting at a fixed length
#echo off
set "spaces= "
set var=1
:start
cls
set var=%var%%spaces%
set var=%var:~0,15%
echo Variable: %var%'Text'
echo %spaces:~0,15%' '
pause >nul
set /a var= %var% * 10
goto start

#ECHO Off
SETLOCAL enabledelayedexpansion
SET "string= Text"
SET /a var=1
:loop
FOR /l %%a IN (1,1,30) DO IF "!var:~%%a,1!"=="" SET "newstr=%var%!string:~%%a!"&GOTO foundlen
:foundlen
ECHO %newstr%
SET /a var*=10
IF %var% gtr 0 GOTO loop
GOTO :EOF
Assumes the length of var is less than 30.
Varies %%a until %%a is the length of the prefix string and then strings the prefix to the other string, removing %%a characters.
If the prefix string var may be empty, then
set "newstr=%string%"
if defined var for /l ...
I chose to force the loop to terminate by exploiting the 31-bit nature of integer mathematics in cmd - if the number grows >2**31 then it may appear negative. This is simply a way to terminate the loop rather than explicitly checking that it has the maximum allowable length.

Related

How to replace any double quotes with two double quotes inside a batch variable?

I'm trying to make a program that types out the clipboard, word by word. So far, I have done everything except work out how to escape the double quote in the VBS file. To do this, I need another double quote. This is my code as of now:
#echo off & setlocal & cd "%temp%" & paste > x.txt
for /f "delims=" %%A in (x.txt) do set X=%%A & timeout /t 4 /nobreak
:loop
for /f "tokens=1*" %%A in ("%X%") do (
set "Y=%%A" & set X=%%B
)
set Z=%Y:^"=^"^"%
echo CreateObject("Wscript.Shell").SendKeys "%Z% " > x.vbs
start /wait x.vbs & if defined Y (set "Y=" & goto :loop) & pause
I am obviously doing something wrong on this line:
set Z=%Y:^"=^"^"%
You dont need to escape the quotes:
set z="a--b"
echo %z:"=""%

Colored line code does not accept variables as colors

I am coding a batch image viewer, set out of pixels which are shown on the screen with a actual way of colored words, lines or even just letters in the same line with different colors. Said that I am making a pixel type of a image viewer. The problem is that the code for coloring the lines that I found on stack overflow does not accept me to enter a variable instead of the color code(P.S. The variable contains the color code) while calling the command in code.
This is the code for the actually command that let's me do color coding:
:Echo.Color %1=Color %2=Str [%3=/n]
setlocal enableDelayedExpansion
set "str=%~2"
:Echo.Color.2
set "str=a%ECHO.DEL%!str:\=a%ECHO.DEL%\..\%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%!"
set "str=!str:/=a%ECHO.DEL%/..\%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%!"
set "str=!str:"=\"!"
pushd "%ECHO.DIR%"
findstr /p /r /a:%~1 "^^-" "!str!\..\!ECHO.FILE!" nul
popd
for /l %%n in (1,1,12) do if not "!ECHO.FILE:~%%n!"=="" <nul set /p "=%ECHO.DEL%"
<nul set /p "=%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%%ECHO.DEL%"
if not "%~3"=="" echo.
endlocal & goto :eof
:Echo.Color.Var %1=Color %2=StrVar [%3=/n]
if not defined %~2 goto :eof
setlocal enableDelayedExpansion
set "str=!%~2!"
goto :Echo.Color.2
:Echo.Color.Init
set "ECHO.COLOR=call :Echo.Color"
set "ECHO.DIR=%~dp0"
set "ECHO.FILE=%~nx0"
set "ECHO.FULL=%ECHO.DIR%%ECHO.FILE%"
for /F "tokens=1 delims=#" %%a in ('"prompt #$H# & echo on & for %%b in (1) do rem"') do set "ECHO.DEL=%%a"
goto :eof
The command to display something is:
call :Echo.Color E0 "a"
That will display the "a" letter in a yellow background and black font color.
The actual thing I want to do is:
call :Echo.Color %c1% "a"
The %c1%is actually set from a c1.txt file and it's contents are E0 which should produce the same result, just with the .txt file I get to make my own picture format kind of and then anyone could edit the file and make his own colored pixel which would make a picture.
The solution to this answer is that someone gives me a different color code which I would not prefer, the smarter thing is to actually edit this code and somehow help me making the thing so it can use the variables. Or I have another solution of mine but I don't know will it work and how to get it to work. I could again echo out all of the commands with the variable and then save that into a text file(each one of them) and then load them back but I don't know how to use a variable as a actual command.

Batch echo %var(number of %ifno%)%

#echo off
set varno=1
for /r %%A in (*.txt) do call :loopdo "%%A"
goto end
:loopdo
set var=%1
set var=%var:*libraries\=%
set var%varno%=%varno%. %var:.txt"=%
set /a varno=%varno%+1
goto :eof
:end
set ifno=1
if %ifno% LSS %varno% (
echo %var%%ifno%
set /a ifno=%ifno%+1
)
pause
How could I make this echo %var1% %var2% etc?
At the moment it just echoes textfile1.txt"1 textfile2.txt"2 and I can see why but I don't know how to make it do the above.
I have tried %varifno% and %%var%ifno%% but they don't work - again I can see why.
If you want to evaluate variables within a variable name, setlocal enabledelayedexpansion. Also, use variable expansion to strip the .txt off the end of the filename within your :loopdo subroutine. You're pretty close otherwise.
#echo off
set varno=1
for /r %%A in (*.txt) do call :loopdo "%%~pnA"
goto end
:loopdo
set "var=%~1"
set "var=%var:*libraries\=%"
set /a varno+=1
goto :eof
:end
set ifno=1
if %ifno% LSS %varno% (
setlocal enabledelayedexpansion
echo !var%ifno%!
endlocal
set /a ifno+=1
)
pause
This is untested, as your use of var%varno% and %var%%ifno% is a little confusing. I don't see that you're ever actually setting a variable called %var%%ifno%. Hopefully the changes I made will lead you in the right direction, if it doesn't provide the complete solution.

How to add column to txt tab delimited file

I have some data exported through a batch file from my sql server database in tab delimited format. How can I add a column to the file through a batch script with the heading "channel" and the text "channel1" to each row beneath the heading?
#echo off
setlocal
set "file_path=F:\scriptests\textsql\Book1.txt"
break>tempfile
rem setlocal enableDelayedExpansion
set "firstline="
for /f "useback tokens=* delims=" %%A in ("%file_path%") do (
if not defined firstline (
rem after %%A the symbol is tab but not space
echo %%A "channel"
set "firstline=."
) else (
rem after %%A the symbol is tab but not space
echo %%A "channel1"
)
)>>tempfile
rem first check created tempfile before uncomment the line bellow
rem move /y tempfile "%file_path%"
endlocal

How to copy value in clipboard to a variable?

I have a string in clip, and I want to copy it to a variable var.
I have to use that string to find further sub-strings in variable.
You can take advantage of the functionality built into internet explorer, and use its clipboard object. Then you can wrap up the VBScript which accesses the IE objects with a command script:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :GetClipboardText Text
ECHO %LineCount% lines copied.
ECHO Line1: %Text1%
ECHO Line2: %Text2%
ECHO Line3: %Text3%
ECHO Line4: %Text4%
ENDLOCAL
GOTO :EOF
:GetClipboardText
CALL :GetTempFilename TempFile
ECHO Set objHTML = CreateObject("htmlfile")>%TempFile%
ECHO WScript.Echo objHTML.ParentWindow.ClipboardData.GetData("text")>>%TempFile%
SET LineIndex=1
FOR /F "delims=" %%A IN ('%TempFile% //NOLOGO') DO (
SET %1!LineIndex!=%%A
SET /A LineIndex=!LineIndex!+1
)
SET LineCount=%LineIndex%
DEL %TempFile%
GOTO :EOF
:GetTempFilename
FOR /F "delims=:. tokens=2-5" %%A IN ('ECHO ^| TIME') DO SET T=%%A%%B%%C%%D & GOTO :X
:X
CALL :Trim %T% T
SET %1=%Temp%\TMP%T%.vbs
GOTO :EOF
:Trim
SET %2=%1
GOTO :EOF
Unfortunately, due to the nature of the command script engine, I have to split up the text into lines. In my example, %LineCount% will contain the number of lines, which will be retried by %Text1%, %Text2%, and so on.
Using The clip command
but it don't exist in Windows XP :(
It depends upon your operating system, toolkit, language. If you code in C++ with Qt on Linux, you could use QClipboard::text
Batch files can't directly interact with the clipboard. However, there are various people that have written little tools to do this for you, so you could get the GetClip tool from this website.