0
Powershell을 아는 사람에게는 간단한 질문 일 것입니다. 나는 이미 these과 these과 같은 비슷한 질문에 대한 몇 가지 답변의 제안을 구현하려고 시도했다. 폴더를 모니터링하고 변경 사항이 발생할 경우 로그 파일을 작성하는 파일 공유기가 있습니다. 이것은 잘 작동합니다.Powershellscript의 FileSystemWatcher를 사용하여 Python 파일을 실행하십시오.
내가 할 수 없었던 것은 추가 파이썬 스크립트를 실행하는 것입니다. 어떤 조언을 해주셔서 감사합니다.
$folder = 'F:\myPath\myFolder' # Enter the root path you want to monitor.
$filter = '*.*' # You can enter a wildcard filter here.
# In the following line, you can change 'IncludeSubdirectories to $true if required.
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
# Registered events:
Register-ObjectEvent $fsw Renamed -SourceIdentifier FileRenamed -Action {
$OldName = $Event.SourceEventArgs.OldName
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore white
Out-File -FilePath F:\myPath\myLogFile.txt -Append -InputObject "The file $name was $changeType at $timeStamp"
python F:\myPath\myPythonScript.py}
Powershell ISE로 어떻게하면됩니까?
맞습니다 : 콘솔에'python F : \ myPath \ myPythonScript.py '명령 만 입력하면 파이썬 스크립트가 실행됩니다. 그러나 PowerShell 콘솔에서 전체 스크립트 (파일 시스템 워처 포함)를 실행할 때 감시 폴더에서 파일을 변경하면 Python 스크립트가 실행되지 않습니다. 그렇다면 감시 된 폴더에서 파일이 변경된 경우 Python 스크립트를 실행하려면 어떻게해야합니까? – Chris
EventSubscriber는'-Action' 다음의 중괄호로 코드를 실행합니다. 거기에 코드가 올바르게 실행됩니까? 예를 들어 로그 파일에 기록합니까? –
예, 코드가 올바르게 실행되고 로그 파일을 작성합니다. 그러나 콘솔에서 python 스크립트를 실행하지 않습니다. 내가 사용하고있는 질문과 코드를 편집했습니다. – Chris