setting variables with batch - variables

Hi im trying to batch append information from one file to another but without success
heres the code
#echo off
set backup=C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN"
copy C:\"VTS\Advanced Tools\Advanced Offset\AP\LOADING SQUENCE.AP" %backup%
the issue is that the backup is not actually C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN" but an actual other location stored in there.
basicaly i want to set the contents of the file C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN" as my backup variable

Use the command lines below. set /p V=<FILE will set the value of the variable V to the content of FILE.
set /p backup=<C:\"VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN"
copy C:\"VTS\Advanced Tools\Advanced Offset\AP\LOADING SQUENCE.AP" %backup%

give this a try
#echo off
setlocal enabledelayedexpansion
for /f %%a in ("C:\VTS\Advanced Tools\Advanced Offset\PEN\LOCATION ACAD LSP.PEN") do (
set backup=%%a
echo copy "C:\VTS\Advanced Tools\Advanced Offset\AP\LOADING SQUENCE.AP" "!backup!"
goto :eof
)
and remove the echo after testing

Related

Tricky variable setting from a text file

I have a windows batch file that reads in various lines of a text file for processing. During the execution I have to open another text file that has only one line in it. The line contains a serial number with about 3 or 4 spaces after the end.
I need to read in this file called (value.txt), grab the data in the first line and assign the value to a variable without any spaces at the end.
After that I need to add the .pdf extension to this variable and set to another variable.
I then need to then check if the variable with the pdf extension exists in the current directory as a file name. If it does then can I change the variable value to add -1 before the .pdf. If this file still exists then try -2 etc.
Thus
File name read in called value.txt
55400TERM1 (variable VAR1 is assigned with no spaces)
55400TERM1.pdf (variable VAR2 is set with the .pdf extension)
55400TERM1-1.pdf (variable VAR2 is updated if 55400TERM.pdf exists)
55400TERM1-2.pdf (variable VAR2 is updated if 55400TERM1.pdf exists)
Etc etc - loops until it cannot file a existing file with the variable value.
Here is a comment batch script for this task:
#echo off
rem If text file with file name does not exist in current
rem directory, exit this batch file as nothing can be done.
if not exist value.txt (
echo %~f0: Missing file "value.txt" in "%CD%".
goto :EOF
)
rem Read first line from text file. Command FOR automatically splits up the
rem line into strings using space and horizontal tab character as string
rem delimiter. As the line contains just one string with trailing spaces
rem most likely caused by wrong redirecting an output into the text file
rem the single string is assigned to the variable with no trailing spaces.
for /F %%I in (value.txt) do (
set "FileName=%%I"
goto CheckFile
)
echo %~f0: No string in file "value.txt" in "%CD%".
goto :EOF
:CheckFile
if exist "%FileName%.pdf" goto FindLatestFile
echo No file "%FileName%.pdf" found in "%CD%".
set "FileName=%FileName%.pdf"
goto ProcessFile
rem The command DIR as used here lists all files matching the wildcard
rem specification in reverse order according to last modification date.
rem This batch file assumes that the file with highest number is the
rem newest modified file. This speeds up determining the file with the
rem highest number in file name. For an alphabetical reverse order it
rem would be necessary that all files have the same number of digits,
rem i.e. the files are *-01.pdf, *-02.pdf, ..., *-10.pdf, *-11.pdf as
rem otherwise the alphabetical order would be *-1.pdf, *-10.pdf, *-11.pdf,
rem *-2.pdf, ..., *-9.pdf and then last file would be determined wrong.
:FindLatestFile
for /F %%I in ('dir /O-D /TW /B "%FileName%-*.pdf" 2^>nul') do (
set "LastFileName=%%~nI"
goto FoundLatestFile
)
echo No file "%FileName%-*.pdf" found in "%CD%".
set "FileName=%FileName%-1.pdf"
goto ProcessFile
:FoundLatestFile
for /F "tokens=2 delims=-" %%I in ("%LastFileName%") do set "LastFileNumber=%%I"
set /A LastFileNumber+=1
set "FileName=%FileName%-%LastFileNumber%.pdf"
:ProcessFile
echo Next file is: %FileName%
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
dir /?
for /?
goto /?
if /?
rem /?
set /?

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.