I am relatively new to batch programing and programing altogether, but after searching for hours I found to no avail, this is the script I'm having problems with
set /p filename=What is the file name?:
cls
set /p lines=how many lines are there? (one number):
cls
set line=1
:batch_start
for /f "skip=%line% delims=" %%i in (%filename%.txt) do set "ip=%%i"& for /f "tokens=1,2 delims=[]" %%A in ('ping -a %ip% ^| find "Pinging"') do set ip=%%B& if '%lines%'=='%line%' goto done& echo %ip%& set line==%line%+1
pause
:done
echo Done!
pause
Please help as the code is ony setting %ip% and doing nothing else.
#echo off
set /p filename=What is the file name?:
cls
set /p lines=how many lines are there? (one number):
cls
set line=1
for /f "skip=%line% delims=" %%i in (%filename%.txt) do set "ip=%%i"
setlocal enableDelayedExpansion
for /f "tokens=1,2 delims=[]" %%A in ('ping -a %ip% ^| find "Pinging"') do (
set ip=%%B
if '%lines%'=='!line!' goto done
echo !ip!
set line==!line!+1
)
pause
:done
echo Done!
pause
Is this working as you expect?
Related
I was trying this, it'll count file's line after I copy the file's path (Shift+right click >copy as path) and put it in batch file, but.... how do I fix it??
the last \ in %path% is causing problem.
#echo off
Setlocal EnableDelayedExpansion
set /p ifilename=Enter file name:
for %%f in (%ifilename%) do (
set paath=%%~df%%~pf
set ifilename=%%~nf%%~xf
)
echo %paath%
echo %ifilename%
for /f "usebackq" %%a in (`dir /b /s %1 "%paath%"`) do (
for /f "usebackq" %%b in (`type %ifilename% ^| find "" /v /c`) do (
set lines= %%b
)
)
echo %lines%
pause
>> the last \ in %path% is causing problem
It's easy to solve this , the code is :
set TempDir=C:\0TEMP
#echo off
md %TempDir%
cd /d %TempDir%
::------
#echo off
#echo on
Setlocal EnableDelayedExpansion
::set /p ifilename=Enter file name:
SET DUMMYexe=%TempDir%\DUMMY.exe
IF EXIST "%DUMMYexe%" goto ll123
ECHO ---------writing
pause
(
ECHO pause1
ECHO pause2
ECHO pause3
) > %DUMMYexe%
:ll123
SET ifilename=%DUMMYexe%
for %%f in (%ifilename%) do (
set fpath=%%~df%%~pf
set ifilename=%%~nf%%~xf
)
echo %fpath%
echo %ifilename%
SET v=asdf1234
SET vv=\%v%
SET vvv=%fpath%%vv%
CALL SET v=%%vvv:\%vv%=%%
echo 111---%v%
pause
set fpath=%v%
for /f "usebackq" %%a in (`dir /b /s %1 "%fpath%"`) do (
for /f "usebackq" %%b in (`type %ifilename% ^| find "" /v /c`) do (
set lines= %%b
)
)
echo %lines%
echo on
pause
goto
But of course, if I use the var 'path', my win10 will report :
'find' is not recognized as an internal or external command, operable program or batch file.
BTW, Maybe you'd be interested in the code below :
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`dir `) DO (
SET var!count!=%%F
SET /a count=!count!+1
)
ECHO -------------- %count%
ECHO
ECHO %var1%
ECHO %var2%
ECHO %var3%
ENDLOCAL
pause
which I tested after copying from:
How to set commands output as a variable in a batch file
Very useful info about "Setlocal EnableDelayedExpansion" can be found in:
How do SETLOCAL and ENABLEDELAYEDEXPANSION work?
trying to get a batch to output network adapter names for another batch file. so far this works...
#echo off
set ignore=true
for /F "delims=" %%a in ('netsh interface show interface')do call :Sub %%a
goto :eof
:sub
if not "%adapter1%" EQU "" goto :2
set Line=%*
if "%Line:~0,10%" EQU "----------" (set ignore=false & goto :eof)
if %ignore% EQU true goto :eof
for /F "tokens=4*" %%b in ('echo %*') do set Adapter1=%%b
echo %Adapter1%
goto :eof
)
:2
for /F "tokens=4*" %%c in ('echo %*') do set Adapter2=%%c
echo %adapter2%
pause
But is there a way to loop the second part so the output would continue with
Adapter#="Adapter Name" until there are no adapters left.
I've tried to use..
set /a c=1
:sub
for /F "tokens=4*" %%c in ('echo %*') do (
set /a c=c+1
Set Adapter%c%=%%b
echo %adapter2%
)
couple of issues here trying to call a variable made of Variables ie.%adapter%c%%
and the other being i have no idea how to loop this back to the next line.
I realise i could keep expanding this but it would be horrendous.
#echo off
set ignore=true
for /F "delims=" %%a in ('netsh interface show interface')do call :Sub %%a
goto :eof
:sub
if not "%adapter2%" EQU "" goto :3
if not "%adapter1%" EQU "" goto :2
set Line=%*
if "%Line:~0,10%" EQU "----------" (set ignore=false & goto :eof)
if %ignore% EQU true goto :eof
for /F "tokens=4*" %%b in ('echo %*') do set Adapter1=%%b
echo %Adapter1%
goto :eof
)
:2
for /F "tokens=4*" %%c in ('echo %*') do set Adapter2=%%c
echo %adapter2%
pause
:3
for /F "tokens=4*" %%c in ('echo %*') do set Adapter3=%%c
echo %adapter3%
pause
Frustrated >.< , can't get my head around how its supposed to work. Any help appreciated. Thanks
It's much easier to skip the first 3 lines of the output
As the adapter name could contain spaces your approach with tokens=4 can't work,
use an asterisk to catch the remainder of the parsed line in the next for var.
:: Q:\Test\2019\03\15\SO_55189424.cmd
#Echo off&SetLocal EnableDelayedExpansion
Set Cnt=0
for /f "skip=3 tokens=1-3*" %%A in ('
netsh interface show interface
') Do (
Set /A Cnt+=1
Set "Adapter!Cnt!=%%D"
)
Set Adapter
Sample Output:
> Q:\Test\2019\03\15\SO_55189424.cmd
Adapter1=VirtualBox Host-Only Network
Adapter2=Ethernet
Adapter3=Ethernet 2
Im making a cool hidden folder passworded USB drive. I have a little image that opens and I want to play music using this code.
set "file=music.mp3"
( echo Set Sound = CreateObject("WMPlayer.OCX.7"^)
echo Sound.URL = "%file%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000) >sound.vbs
start /min sound.vbs
I want to end the music after the user types the password. Or just the ability to tell it to stop.
I found in another post on here a solution by a user but he doesnt explain how to use it. Here is the link to that post. Link to the post Im new to batch so just giving me a script wont help I kind of need to know how to use it.
This is the script I dont understand how to use that will stop the music. According to the post I linked.
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
Here is the password code im using.
#echo off
set pass= 123abc
echo Enter Password
set /p ui=
if %ui%==%pass% (goto open)
echo Wrong Password
pause
exit
:open
start folder
How to stop the music
Note taskkill command is echoed merely... Remove echo when debugged.
see instruction by #JosefZ here : https://stackoverflow.com/a/29271203/9222942
remove the 'echo' word from this line :
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID
will becomes :
for /F "tokens=2 delims==" %%H in ("%%~G") do taskkill /T /F /PID
#ECHO OFF >NUL
for /F "usebackq tokens=*" %%G in (
`wmic process where "CommandLine like '%%sound.vbs%%' AND Caption like '%%script.exe%%'" get ProcessID/value ^|find /I "="`
) do (
rem echo %%G
for /F "tokens=2 delims==" %%H in ("%%~G") do echo taskkill /T /F /PID %%H
)
For the program I am currently working on I am taking a value, which is a proxy in ip:port format, I need to be able to split the ip and port to different variables so that a different program that needs ip and port separate will be able to work. The program is basically an automated ip/proxy switcher for minecraft, just for in game reasons, I have all the code working except for the part that actually changed the proxy. I am not getting any error message, only that I don't actually know what code to write. Anyways, here is my code.
#echo off
color b
title minecraft proxy switcher
set nLine=0
echo input full path to text file containing proxies
set /P "filepath=>"
echo end >> %filepath%
:top
cls
set /A nLine=%nLine%+1
echo now at proxy number %nLine%
CALL :ReadNthLine "%filepath%" %nLine%
PAUSE >NUL & goto:top
GOTO :EOF
::***************************************************************************************
:ReadNthLine File nLine
FOR /F %%A IN ('^<"%~1" FIND /C /V ""') DO IF %2 GTR %%A (ECHO Error: No such line %2. 1>&2 & EXIT /b 1)
FOR /F "tokens=1* delims=]" %%A IN ('^<"%~1" FIND /N /V "" ^| FINDSTR /B /C:"[%2]"') DO set http_proxy=%%B
goto finish
::***************************************************************************************
:finish
if %http_proxy%==end (
cls
echo all proxies have been used
echo will return to top of list in 5 seconds
TIMEOUT /T 5 /NOBREAK
set nLine=0
goto top
)
java -DsocksProxyHost=ip -DsocksProxyPort=port -Xmx800m -jar MinecraftLauncher.exe
echo New ip is %http_proxy%
echo waiting for user input
echo press any key for a new ip
pause
goto top
Any help is greatly appreciated, also if you notice something else that's badly written or incorrect in my code please tell me.
split the string with a for, using proper tokens and delimiters:
set "line=192.168.1.1:8080"
for /f "tokens=1,2 delims=:" %%a in ("%line%") do (
set server=%%a
set port=%%b
)
echo Server %server% Port %port%
here is a basic code skeleton which processes the file line after line (your way works, but this is way easier):
#echo off
set /p "filepath=File: "
:top
set n=0
for /f "tokens=1,2 delims=:" %%a in (%filepath%) do call :process %%a %%b
timeout 5
goto :top
:process
echo trying %n%
set /a n+=1
echo host: %1
echo port: %2
pause
goto :eof
How can I add time stamp in the beginning of batch file results?
Here's my current code:
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%
ping www.google.com -t -l 1 >PingLog_%dt%-%tm%.txt
I want to the result to be similar to this:
[hh:mm:ss] Reply from 120.89.12.38: bytes=1 time=1ms TTL=59
Small edit to add formatting:
It's tested and works well here - though the time format on this PC is 24 hour time, which makes it work well.
#echo off
setlocal enableDelayedExpansion
set dt=%date:~10,4%%date:~4,2%%date:~7,2%
set tm=%time:~0,2%%time:~3,2%%time:~6,2%
for /l %%# in (1,1,100) do (
set t=!time:~0,2!:!time:~3,2!:!time:~6,2!
for /f "tokens=* delims=" %%# in ('ping www.google.com -t -l 1 -n 1^|find /i "reply"') do (
echo [!t!] %%#
)
)
pause & goto :EOF
You can try like this :
#echo off
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
pause
Set TmpLog=TmpLog.txt
Set Log=PingLog_%MM%-%DD%-%YYYY%-%HH%-%Min%-%Sec%.txt
If exist %TmpLog% Del %TmpLog%
If exist %Log% Del %Log%
echo %Log%
(
for /l %%# in (1,1,100) do (
set t=!time:~0,2!:!time:~3,2!:!time:~6,2!
for /f "tokens=* delims=" %%# in ('ping www.google.com -t -l 1 -n 1^|find /i "TTL"') do (
echo [!t!] %%#
)
)
)>>%TmpLog%
cmd /u /c type %TmpLog% > %Log%
pause & Start "" %Log%