How to make "URL Protocol" to launch application from its own directory instead of launching from c:\windows\system32? - url

I have registered a URL protocol in my system using below script to launch a batch file "showPath.bat".
#echo off
reg add HKEY_CLASSES_ROOT\ProtoTest /t REG_SZ /d "My Description" /f
reg add HKEY_CLASSES_ROOT\ProtoTest /v "URL Protocol" /t REG_SZ /d "" /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open /f
reg add HKEY_CLASSES_ROOT\ProtoTest\shell\open\command /t REG_SZ /d "C:\TestFolder\showPath.bat" /f
pause
Content of "showPath.bat" is just to display the current working directory. ie.,
#echo off
SET var=%cd%
ECHO %var%
pause
If I run the batch file directly by double clicking it, I can see its path correctly. However if I launch the batch file using the URL protocol registered above. ie, from Chrome, browsing "ProtoTest://", the batch file runs, however display's the path "C:\Windows\system32" instead of the batch file's directory. So, I believe applications launched using URL protocol runs with system32 as working directory. Now How can I get the batch file run from its own directory when launched from browser using URL protocol - without modifying the batch file itself. Only URL protocol possible to be changed from my end.

Below code worked for me. I had my batch file in a folder that had spaces in it, so added "" along with escape char. However it gets added to registry as below in image without escape characters. Answer credit to #aschipfl
#echo off
reg add HKEY_CLASSES_ROOT\ProtoTest2 /t REG_SZ /d "My Description" /f
reg add HKEY_CLASSES_ROOT\ProtoTest2 /v "URL Protocol" /t REG_SZ /d "" /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell\open /f
reg add HKEY_CLASSES_ROOT\ProtoTest2\shell\open\command /t REG_EXPAND_SZ /d "%ComSpec% /C \"cd /D \"C:\Source\For Ref\URL Protocol\BatchTest\" ^& showPath.bat\"" /f
pause

Related

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

Nested Token search batch file

I have a batch file I am working on trying to use to achieve up IIS server logs. Here is the details of all files involved and the final output I am seeing
Batch file:
setlocal
set zipfolder=D:\Statscheck\zipped\
set statsdir=D:\Statscheck\
set statsfolder=Statscheck
d:
cd\
cd\%statsfolder%
dir w3* /s /b > d:\server-scripts\temp\process.txt
set THECSVFILE=d:\server-scripts\temp\process.txt
::read %THECSVFILE% and loop through each line
for /F "usebackq tokens=* delims=\" %%A in (%THECSVFILE%) do (
set dirname=%%A
)
for /F "usebackq tokens=3 delims=\" %%A in (%THECSVFILE%) do (
set filename=%%A
call :process_line
)
:process_line
set buildfile= %zipfolder%stats-%filename%-archive.zip
echo filename %buildfile% >>D:\server-scripts\temp\output.txt
echo directory %dirname% >>D:\server-scripts\temp\output.txt
Process.txt:
D:\Statscheck\dphs92\W3SVC1532191319
D:\Statscheck\kcnw\W3SVC232480661
D:\Statscheck\master\W3SVC84257322
D:\Statscheck\warlords\W3SVC1913965975
output.txt:
filename D:\Statscheck\zipped\stats-dphs92-archive.zip
directory D:\Statscheck\warlords\W3SVC1913965975
filename D:\Statscheck\zipped\stats-kcnw-archive.zip
directory D:\Statscheck\warlords\W3SVC1913965975
filename D:\Statscheck\zipped\stats-master-archive.zip
directory D:\Statscheck\warlords\W3SVC1913965975
filename D:\Statscheck\zipped\stats-warlords-archive.zip
directory D:\Statscheck\warlords\W3SVC1913965975
As you can see from the output it is building the filename option correctly however the directory that is being pulled is the last one in the list. Is it not looping.

Text parsing a file path in batch script

I'd like to set a file's name to a variable in a batch file. I'm already pulling the file name by pulling the total path length. Is there a way I can parse the path to only give me back the file name?
The particular script that I use to pull the path looks like this:
for /f "delims=_" %%J IN ('forfiles /p "%%F" /m *.extension /c "cmd /c echo #path"')
DO start "Program" /D "c:\fullpath" /Wait program -r %%J
Also, if there's an easier way to pull the file name and put it into a variable I'm open to that as well.

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

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

Resources