Is it possible to input the amount of characters to remove from the beginning and the end of a variable using other pre-defined variables? or did i just make a code mistake? because i've tried something like this:
#echo off
CLS
#setlocal enabledelayedexpansion enableextensions
set /p "word=Enter a Word> "
echo.
set /p "numb1=Suffix Amount Removed> "
echo.
set /p "numb2=Prefix Amount Removed> "
echo.
set new=%word:~!numb1!,-!numb2!%
echo.
echo Before: !word!
echo.
echo After: !new!
echo.
pause >nul
exit
And instead of removing characters from the beginning and end of the batch with a pre defined variable, for example if input 3 for numb1 and numb2 when it echo's !new! instead of removing the characters it shows After: word:~3,-3 Some help would be appreciated please, and thanks for anyone who does help.
#echo off
CLS
#setlocal enabledelayedexpansion enableextensions
set /p "word=Enter a Word> "
echo.
set /p "numb1=Suffix Amount Removed> "
echo.
set /p "numb2=Prefix Amount Removed> "
echo.
set new=!word:~%numb1%,-%numb2%!
echo.
echo Before: !word!
echo.
echo After: !new!
echo.
pause >nul
exit
Related
Originally I had this file while it did work it required alot of extra work and was not the most effective or efficient way of doing so
#echo off
set /P x="Percentage:"
set /P y="Price:"
set /P Z="Fixed Rate:"
cscript //nologo calculate.vbs %x% %y% %z% > results.txt
set /P charge= < results.txt
pause
del results.txt
echo charge Fee Total: %charge%
cscript //nologo calculate2.vbs %y% %charge% > results2.txt
set /P total= < results2.txt
pause
del results2.txt
echo sale total amount: %total%
pause
after several steps in getting the results I was looking for a Stackoverflow member Gerhard was kind enough to help me with a more efficient and better way of coding what I was looking to achieve. However I am still missing one line of code that I can not figure out I am sure its simple but I do not fully understand how to do so ...
here is the code he was gracious enough to assist with
#echo off
if not exist "calculations.txt" call :setVars
if exist "calculations.txt" for /f "usebackq delims=" %%i in ("calculations.txt") do set "%%i"
echo fixed rate = %Z%
echo Percentage = %x%
choice /c CR /M "Continue or re-set?"
if errorlevel 2 call :setVars
for /f "usebackq delims=" %%i in ("calculations.txt") do set "%%i"
set /P "y=Price: "
for /F "delims=" %%i in ('powershell ((%x% / 100^)*%y% + %z%^)') do echo %%i
goto :EOF
:setVars
set /P "x=Percentage: "
set /P "Z=Fixed Rate: "x
(echo x=%x%
echo z=%z%
)>"calculations.txt"
the final piece of the puzzle I am looking for is how to also give the results of the equation at the end before the EOF and the original price ... so basically the value of do echo %%i + %y% to echo to the screen after the results of the equation before the EOF if anyone can assist or direct me on how I can add these I would be most grateful and Thank you in advance
best user,
I tried to assign variables from a txt file in batch. I have a text file called "Final.txt".
Final.txt:
0216027232411405
02160272b6172505
1115fb9c6f423305
1645fb9c6f423305
0000fb9c6f423305
script that I tested till now:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (Final.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
set var
(
set /p var1=
set /p var2=
set /p var3=
)<Final.txt
#echo off
setlocal enabledelayedexpansion
set Counter=1
for /f %%x in ("Final.txt") do (
set "Line_!Counter!=%%x"
set /a Counter+1
if Counter== 0 goto :EOF
)
set /a NumLines=Counter - 1
echo %Line_1%
pause
:EOF
Echo please connect a device
pause
exit
The problem is that the code works, but it only assign the first line of the text file. I want to assign every line in the text file to a variable.Now I only have 5 devices,but in the future iw till expand to more lines. I will do more research, but if you have a solution. Your answers are always welcome!. If I can figure it out I am going to use it in my other script so you are a life saver. I only want to use batch for this.
I'm new in Windows batch programming and I have found problems in the variable assignment. This is my code:
#echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
set /a Video=%Video%+1
#echo Video
set file=%%a
#echo file
)
If FILEMEDIA has two lines I would like to obtain Video=2 and the line in file variable. However, at the end I obtain Video=1 and an error when I tried to print file (echo is off).
Some kind of duplicate with How do I increment a DOS variable in a FOR /F loop?
Variables that should be delay expanded are referenced with !VARIABLE! instead of %VARIABLE%.
#echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
set /a Video+=1
#echo !Video!
set file=%%a
#echo file
)
endlocal
I am trying to create a countdown timer that is in min:sec format which uses a variable taken from a text document and uses it as the finish time and uses the current time (time the .bat was started) as the start time.
Currently I have this code which works and gets the time from the text document but I can't seem to figure out how to use get it to work.
Code:
#echo off
set CurrentTime=%time:~0,2%.%time:~3,2%
set /p StartTime=<"ResponseTime.txt"
echo.
echo %CurrentTime% %StartTIme%
echo.
#echo off
setlocal
rem Get end time
REM for /F "tokens=1,2 delims=:" %%a in ("ResponseTime.txt") do set /A endH=10%%a%%100, endM=1%%b%%100
REM Just for testing:
set endH=14
set endM=58
title Timer
mode con cols=16 lines=2
:synchronize
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set /A "minutes=(endH*60+endM)-(%%a*60+1%%b-100)-1, seconds=159"
:wait
timeout /T 1 /NOBREAK > NUL
echo Timer: %minutes%:%seconds:~-2%
set /A seconds-=1
if %seconds% geq 100 goto wait
set /A minutes-=1, seconds=159, minMOD5=minutes %% 5
if %minutes% lss 0 goto :buzz
if %minMOD5% equ 0 goto synchronize
goto wait
:buzz
pause
What I would do is create a timeout function such as timeout /t xx where xx equals the time in seconds. If that is what you're looking for.
You can also do timeout /t xx >nul so that it won't display a message saying how many seconds you have left. If that is what you want then use the first one I showed. I hope this helped
Works perfect
#echo off
timeout /t 10>NUL
start chrome "https://google.com"
Try this loop:
#echo off
set SecondsLeft=10
call :LOOP %SecondsLeft%
goto END
:LOOP
cls
set /a SecondsLeft=%SecondsLeft%-1
if %SecondsLeft% == 0 goto :eof
call echo Wait %SecondsLeft% seconds
ping -n 2 127.0.0.1 >nul
goto LOOP
:END
pause
I've question about this variables.How can I store result of a command into a variable? For example: saving drive serial into location variable.
thanks.
E.g:
#ECHO OFF
SET location=vol
ECHO We're working with %location%
for /f "delims=" %%x in ('your command and parameters') do set "var=%%x"
where the 'single quotes' are required around the command from which you wish to save output.
Edit - now we know which command.
Comment - one variable can contain up to ~8,000 characters.
Here's a routine that will set the values in $1...$whatever and the entire set in $all with each field enclosed in angle-brackets.
#ECHO OFF
SETLOCAL
SET "$all="
FOR /f "tokens=1*delims=:" %%a IN (
'systeminfo 2^>nul^|findstr /n /r "$"'
) DO (
SET "$%%a=%%b"
FOR /f "tokens=*" %%c IN ("%%b") DO CALL SET "$all=%%$all%%<%%c>"
SET /a max=%%a
)
SET $
pause
FOR /l %%z IN (1,1,%max%) DO CALL ECHO %%$%%z%%
GOTO :EOF
thank you...please see my batch file contents:
for /f "delims=" %%x in ('systeminfo') do set "var=%%x"
command_line.exe %var%
only last line of 'systeminfo' saved in var variable and it is:
"data execution perevention available: yes"
I want to store all contetnt of systeminfo into variable!
thank you.I'm too basic
do you mean result of systeminfo is into all variable? at the end of your code can I add this command:
start command_line.exe $%all%
is this true?
can you put complete code for my batch file?