Batch- If statements based on files/websites open

◇◆丶佛笑我妖孽 提交于 2020-01-07 03:03:20

问题


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:

  1. File Paths in explorer: e.g. "C:\USERS\admin\desktop\"

  2. Files: e.g. "James.csv"

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!