问题
In my previous question, Batch if statements based on opening a program, I asked how to make a batch if statement based on opening a program.
Can I make a batch-file IF
statement based off:
File Paths in explorer: e.g. "C:\USERS\admin\desktop\"
Files: e.g. "James.csv"
Websites: e.g. "http://www.google.com"
To make my question a bit clearer:
@echo off
Set WinProcess1=D:\PFI.vbs
Set WinProcess2=c:\
Set WinProcess3=http://www.google.com
:props
tasklist /FI "IMAGENAME eq %WinProcess1%" | find /C /I "%WinProcess1%" >nul
if not ErrorLevel 1 (taskkill /f /im %WinProccess1% & echo a )
tasklist /FI "IMAGENAME eq %WinProcess1%" I find /C /I "%WinProcess1%" >nul
if not ErrorLevel 1 (echo b & timeout /T 1 /NOBREAK >NUL)
tasklist /FI "IMAGENAME eq %WinProcess2%" | find /C /I "%WinProcess2%" > nul
if not ErrorLevel 1 (echo destroy app & timeout /T 1 /NOBREAK >nul )
tasklist /FI "IMAGENAME eq %WinProcess3%" | find /C /I "%WinProcess3%" > nul
if not ErrorLevel 1 ( echo c & timeout /T 1 /NOBREAK >nul )
goto props
This loop would simply check if winprocess1,2, or 3
were running and echo a,b, or c
if the file/website/path were open. If not, then it would run again.
EDIT: by open, I mean a program is using it. For example, if you try to rename a document via explorer while it is open, it will give you a message saying that a program (e.g) Microsoft word is using it.
回答1:
For the 2nd case download Microsoft's handle utility, then you can check if the file is opened:
anywhere:
handle "James.csv"
in a specific program:
handle -p excel.exe "James.csv"
Example of conditional handling:
handle "James.csv" | find "pid:" >nul && (echo Found) || (echo Not found)
来源:https://stackoverflow.com/questions/32641203/batch-if-statements-based-on-files-websites-open