Nested Loops with Rename - batch-rename

I have problems with nested loops and renaming in a batch-script maybe can help me please with this. Every single loop is working but when I want to put them together I am a little bit screwed :-)
The first loop is going throw an array, to be more flexible with name-changing.
The second loop is pointing to a folder and rename all PNGs underneath, also in sub folders, with the array variables from the first loop.
Thank in advance for any hints!
#ECHO OFF
Setlocal EnableDelayedExpansion
:: rename array variables
SET RPATT[1]=a
SET RPATT[2]=b
:: replace variables
SET REPLACE=_
FOR /L %%P IN (1,1,2) DO (
SET UPATT=!RPATT[%%P]!
ECHO LOOP1
ECHO !UPATT!
PAUSE
FOR /R "D:\test\" %%R IN (*.png) DO (
SET "FILE=%%~nxR"
ECHO LOOP2
ECHO !UPATT!
PAUSE
REN "%%R" "!FILE:%UPATT%=%REPLACE%!"
)
)

Related

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

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.

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.

More than 1 batch file with same variable

I was wondering if it was even possable to have the same variable in 2 different batch programs.
:home
Set /p Name=What is your Name?
Set /p Com=What Command do you wish to use?
If %Com% == #HELP goto msgbox
(MORE IF's ARE USED)
goto msgbox
:msgbox
cls
start msgbox.cmd
Echo Welcome to the MSGBOX PROGRAM %Name%
%Com% Code was typed, here is the Help file
goto Help
:Help
cls
(display's the help for program)
Set /p return=Would you like to return?
If %return%==Y goto home
If %return%==N goto exit
(Set errorlevel==1 if no selection)
:exit
cls
Echo press Enter to exit program
pause >nul
yes.
1) If you call the second bat file from the first , the second will inherit all variables.
2) You can also define environment variable.You can use SETX command
setx variable value
Though it's written in the registry you cannot use it straightforward - it will be accessible in the next cmd sessions.So you'll need temporary variable in the script:
Set /p Com=What Command do you wish to use?
rem making variable accessible for the next run or for another batch
setx Com %com%
And you can override its value for every cmd session or script you want with set.After that it will use the registry value again.
3) And you can also use a temp file, but this will require to read it in the next batch :
Set /p Com=What Command do you wish to use?
echo %com%>c:\command.file
in the next script you'll need to use:
for /f "useback tokens=* delims=" %%# in ("c:\command.file") do set "com=%%#"

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