How to check the existence of a Windows service in a batch file - windows-services

How can I check the existence of a Windows service in a batch file?
The user is inputting a service name, and I want to check that the service does exist before continuing with my script.

Try this:
>NET START | FIND "Workstation"
where "Workstation" is the name of the service

#echo off
color 1F
SET KEYS=HKLM\SYSTEM\CurrentControlSet\services\ACPI
for /f "tokens=3" %%i in ('REG QUERY "%KEYS%" ^| find "Start"') do set START=%%i
IF "%START%" == "%START%" ECHO %START% | find /I "%START%" && IF "%START%" NEQ "0x3" REG ADD %KEYS% /v "Start" /t REG_DWORD /d 3 /f >> %COMPUTERNAME%_MODIFIER.TXT
IF ERRORLEVEL 1 ECHO %KEYS% >> %COMPUTERNAME%_SERVICE_MISSING.TXT
OR
#echo off
color 1F
#sc query >%COMPUTERNAME%_START.TXT
ECHO REPORT MISSING INSTALL SERVICES >%COMPUTERNAME%.TXT
find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul
IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"
IF ERRORLEVEL 1 ECHO AcPrfMgrSvc >>%COMPUTERNAME%.TXT

Related

How to delete the whole line in batch file and retranslate it to the output file?

As I am new here, I have some problems "coding" in batch file.
I have an input-output file file.txt with some data (Name Surname Sex Age) and also have some functions that should be made with this file, e.g.:
add a person,
delete a person,
sort people,
exit,
show the list
The main problem happens when I choose the function to delete a person. I have tried to overwrite the file with type %~f1 | findstr /v %name% > %~f1, but it deletes everything from the input-output file. I have also tried type %~f1 | findstr /v %name% >> %~f1, but it adds some lines that I do not need...
I need a function that would delete the whole line, which contains the entered name (or surname) and would show me the output file without that line.
#echo off
:PAT
cls
color 4f
echo.
if exist %~f1 (
rem goto PRA
) else (
echo ERROR
goto END
)
:PRA
echo Menu:
echo 1) Add a person
echo 2) Delete a person
echo 3) Sort
echo 4) Exit
echo 5) Show the list
echo.
choice /c 12345 /m "Choice: "
if errorlevel 5 goto AAA
if errorlevel 4 goto END
if errorlevel 3 goto SOR
if errorlevel 2 goto DEL
if errorlevel 1 goto ADD
:EOF
pause
:END
choice /m "Continue?"
if errorlevel 2 goto NE
if errorlevel 1 goto TAI
:TAI
cls
goto PAT
:NE
Exit
:SOR
echo Which sort?
echo 1: A-Z
echo 2: Z-A
choice /c 12 /m "Choice: "
if errorlevel 2 goto ZTA
if errorlevel 1 goto ATZ
:ATZ
type %~f1 | sort /+1 /o %~f1
cls
goto PRA
:ZTA
type %~f1 | sort /+1 /r /o %~f1
cls
goto PRA
:DEL
echo Enter the name:
set /p name=
type %~f1 | findstr /v /i %name% > %~f1
goto PRA
:ADD
set /p name=Enter the name:
set /p surname=Enter the surname:
set /p sex=Enter the sex:
set /p age=Enter the age:
echo %vardas% %surname% %sex% %age%>>%~f1
echo.
cls
goto PRA
:AAA
cls
echo _________________________
more %~f1
echo _________________________
goto PRA

Youtube-dl OUTPUT TEMPLATE %(title)s.%(ext)s is not working with set /p in windows

I used the batch file with commands :-
set /p ytlink="Enter the link of Youtube Video:- "
youtube-dl -f "bestvideo[height<=1080]+bestaudio/best[height<=1080]" -o "D:\Videos\%(title)s.%(ext)s" %ytlink%
pause
but the output file name is (ext)s.webm ,it seems cmd is treating %(title)s.% in -o "D:\Videos\%(title)s.%(ext)s" as variable.So how to get video title?
OS=Windows 10 64bit 1909
youtube-dl=2020.01.24
You have to use double % in a batch file.
set /p ytlink="Enter the link of Youtube Video:- "
youtube-dl -f "bestvideo[height<=1080]+bestaudio/best[height<=1080]" -o "D:\Videos\%%(title)s.%%(ext)s" %ytlink%
pause
Maybe someone will need it. Based on 1957classic answer. Download video and audio in the best quality to your desktop with the original name with the url taken from the clipboard.
setlocal enabledelayedexpansion
for %%I in (powershell.exe) do if "%%~$PATH:I" neq "" (
set getclip=powershell "Add-Type -AssemblyName System.Windows.Forms;$tb=New-Object System.Windows.Forms.TextBox;$tb.Multiline=$true;$tb.Paste();$tb.Text"
)
for /f "delims=" %%I in ('%getclip% ^| findstr /n "^"') do (
set "line=%%I" & set "line=!line:*:=!"
)
set "psCommand="[Environment]::GetFolderPath('DesktopDirectory')""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "Desktop=%%I"
%~d0"%~p0"youtube-dl.exe -f "bestvideo[height<=1080]+bestaudio/best[height<=1080]" -o "!Desktop!\%%(title)s.%%(ext)s" !line!

How to print multiple PDF files in different folders?

An example would be:
 Folder 1:
  a.pdf
  b.pdf
   Folder11
   c.pdf
  Folder 2:
  a.pdf
  b.pdf
   Folder21:
   c.pdf
printing all files between folders
And the cmd would have a way to find the file only putting part of the words?
Example
TEXT : ABC*.PDF
PRINT ABCDF.PDF
1. To loop over multiple files recursively:
FOR /f "tokens=*" %%F in ('dir /s /b *.pdf') DO echo "%%F"
dir /s /b *.pfd finds all pdfs (*.pdf), in all subdirectories (/s), in bare format - ie just the path name (/b)
DO echo "%%F" just echo's the result to the console.
"tokens=*" adds the whole line into %%F regardless of white spaces / other tokens
/F makes it run the ('dir ...') command
2. To print from command line use: From this question
AcroRd32.exe /t "C:\Folder\File.pdf" "Brother MFC-7820N USB Printer" "Brother MFC-7820N USB Printer" "IP_192.168.10.110"
Note: Path to AcroRd32.exe must be in your path environment variable
3. Putting it all together -- edit -- 'I've added taskkill to close acrord32 after printing
FOR /f "tokens=*" %%F in ('dir /s /b *.pdf') DO AcroRd32.exe /t "%%~F" "Brother MFC-7820N USB Printer" "Brother MFC-7820N USB Printer" "IP_192.168.10.110" & taskkill /IM AcroRd32.exe

CMD - get MAC address of iPhone

Im trying to get the MAC address of my iPhone from my windows machine.
GETMAC /s 10.30.114.14
And it returns:
ERROR: The RPC server is unavailable
But if I try to get a MAC address of any other device (laptops and what not), I get a good address within milliseconds.
So Im asking: Is there a way to get a MAC address of an iDevice from CMD?
**I know I can look at the device settings, but that is realy not what I'm after.
ping it and then grab the MAC from the arp cache:
#echo off
set IP=10.30.114.14
ping -n 1 %IP% >nul
for /f "tokens=2" %%a in ('arp -a ^| find "%IP%"') do set MAC=%%a
echo MAC: %MAC%
pause
Suppose that you a have the inputfile with computers or ip address list, you can give a try with batch file :
#echo off
Title Get IP and MAC address for remote PCs over the network using batch
Set "Copyright=by Hackoo 2021"
Title %~nx0 %Copyright%
Mode con cols=90 lines=12
cls & color 0A & echo.
echo ********************************************************************************
echo Get IP and MAC address for remote PCs over the network by %Copyright%
echo ********************************************************************************
echo(
if _%1_==_Main_ goto :Main
:getadmin
echo %~nx0 : self elevating
set vbs=%temp%\getadmin.vbs
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "%~s0", "Main %~sdp0 %*", "", "runas", 1
)> "%vbs%"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
goto :eof
::-------------------------------------------------------------------------------------
:Main
set "InputFile=%~dp0Hosts.txt"
set "OutPutFile=%~dp0IP-MAC.txt"
If Exist "%OutPutFile%" Del "%OutPutFile%"
If Not Exist "%InputFile%" (
color 0C & echo "%InputFile%" does not exist. Please check it first !
Timeout /T 8 /NoBreak>nul & Exit
)
Netsh interface ip delete arpcache >nul 2>&1
#for /f "tokens=* delims=" %%H in ('Type "%InputFile%"') do (
Ping -n 1 %%H>nul
#for /f "tokens=2" %%M in ('arp -a %%H ^| find "%%H"') do (
echo %%H : %%M
echo %%H : %%M>>"%OutPutFile%"
)
)
If Exist "%OutPutFile%" Start "" "%OutPutFile%" & Timeout /T 1 /NoBreak>nul & Exit

FINDSTR cannot open path with spaces

Drag and drop .bat, it takes the files,
puts it in 2 lists and process both list simultanously.
I passed a path : X:\folder\folder number\begin.txt into %1
it's working fine if i have foldernumber
PUSHD %~dp0
:loop
IF ["%~f1"] EQU [""] goto :out
echo %~f1>>list
set /a count+=1
SHIFT
goto :loop
POPD
:out
if %count% LEQ 1 (copy list list1 && goto :START)
set /a count2=%count%/2
more /e +%count2% list > list2
set count=0
setlocal enabledelayedexpansion
for /F "eol=; tokens=* delims=," %%i in (list) do (
set /a count+=1
if !count! leq !count2! echo %%i >>list1
)
endlocal
:: Create the 2nd .bat for multiprocessing and start multiprocessing
more /e +88 mybat.bat > temp.bat
START "2nd Process mybat2" temp.bat
:start
for /f "eol=; tokens=* delims==," %%i in (list1) do call :SEARCH %%i
goto :END
:: 88th line here
for /f "eol=; tokens=* delims==," %%k in (list2) do call :SEARCH %%k
goto :END
:search
setlocal enabledelayedexpansion
for %%A in (jan feb mar apr mai etc...) DO (
findstr /m "%%A" "%~f1" > NUL
if !ERRORLEVEL! == 0 (
call :theend %~1 %%A
goto :EOF
)
)
endlocal
I get this error message "FINDSTR: Cannot open X:\folder\folder number\begin.txt"
Is there a way to fix this findstr problem?
thanks
To let us see if your file dropping is OK:
Can you create a batch file with these contents
echo %*
set /p dummy=press return
drop a file, with spaces in the name, onto it and post the results?

Resources