2012-12-04 9 views
33

मैं एक node.js स्क्रिप्ट लिखने की कोशिश कर रहा हूं जो फ़ाइलों की निर्देशिका में परिवर्तनों को देखता है, और फिर बदली गई फ़ाइलों को प्रिंट करता है । मैं इस स्क्रिप्ट को कैसे संशोधित कर सकता हूं ताकि यह एक निर्देशिका (एक व्यक्तिगत फ़ाइल के बजाय) देख सके, और निर्देशिका में फ़ाइलों के नामों को प्रिंट के रूप में प्रिंट करता है?node.js का उपयोग करके परिवर्तनों के लिए फ़ोल्डर देखें, और जब वे बदले जाते हैं तो फ़ाइल पथ प्रिंट करें

var fs = require('fs'), 
    sys = require('sys'); 
var file = '/home/anderson/Desktop/fractal.png'; //this watches a file, but I want to watch a directory instead 
fs.watchFile(file, function(curr, prev) { 
    alert("File was modified."); //is there some way to print the names of the files in the directory as they are modified? 
}); 
+0

मुझे आश्चर्य है कि अगर यह प्रासंगिक है: http://stackoverflow.com/questions/12063266/how-do-you-watch-multiple -फाइल-लेकिन-केवल-रन-टास्क-ऑन-चेंज-फाइल-इन-ग्रंट-जेएस (मैं ग्रंटज से परिचित नहीं हूं, हालांकि - मुझे उम्मीद है कि अन्य समाधान भी हैं।) –

+0

शायद नोड-इनोटिफी-प्लसप्लस जैसे कुछ उपयोगी होगा: http://stackoverflow.com/questions/5877263/monitoring-directory-for-changes-potential-high-memory –

+0

आप किस ओएस चल रहे हैं? यह एक फ़ाइल देखने के लिए निम्न स्तर के तंत्र के रूप में महत्वपूर्ण है क्योंकि यूनिक्स/लिनक्स 'इनोटिफी' का उपयोग करता है, ओएसएक्स 'एफएसवॉच' का उपयोग करता है और मुझे विंडोज के बारे में कोई जानकारी नहीं है, लेकिन मुझे यकीन है कि Google आपको बता सकता है। – srquinn

उत्तर

65

Chokidar का प्रयास करें:

var chokidar = require('chokidar'); 

var watcher = chokidar.watch('file or dir', {ignored: /^\./, persistent: true}); 

watcher 
    .on('add', function(path) {console.log('File', path, 'has been added');}) 
    .on('change', function(path) {console.log('File', path, 'has been changed');}) 
    .on('unlink', function(path) {console.log('File', path, 'has been removed');}) 
    .on('error', function(error) {console.error('Error happened', error);}) 

वार्डर सिर्फ FS का उपयोग करके फ़ाइलें देख के साथ पार मंच मुद्दों में से कुछ को हल करती है।

+1

क्या यह उपफोल्डर को संभालता है? –

+0

हाँ! यह चेतावनी के –

9

कोशिश hound:

hound = require('hound') 

// Create a directory tree watcher. 
watcher = hound.watch('/tmp') 

// Create a file watcher. 
watcher = hound.watch('/tmp/file.txt') 

// Add callbacks for file and directory events. The change event only applies 
// to files. 
watcher.on('create', function(file, stats) { 
    console.log(file + ' was created') 
}) 
watcher.on('change', function(file, stats) { 
    console.log(file + ' was changed') 
}) 
watcher.on('delete', function(file) { 
    console.log(file + ' was deleted') 
}) 

// Unwatch specific files or directories. 
watcher.unwatch('/tmp/another_file') 

// Unwatch all watched files and directories. 
watcher.clear() 

यह निष्पादित करेंगे एक बार फ़ाइल परिवर्तन

5

था क्यों न सिर्फ पुराने fs.watch का उपयोग करें? यह बहुत सरल है।

fs.watch('/path/to/folder', (eventType, filename) => { 
console.log(eventType); 
// could be either 'rename' or 'change'. new file event and delete 
// also generally emit 'rename' 
console.log(filename); 
}) 

अधिक जानकारी और विकल्प परम के बारे में जानकारी के लिए, Node fs Docs

+0

संभालता है, मैंने अपने मैक पर इसका परीक्षण किया है, और यह कोड केवल उस फ़ोल्डर स्तर का पता लगाता है और परिवर्तनों के लिए कोई उपनिर्देशिका नहीं है, इसलिए सुनिश्चित करें कि दूसरे पैरामीटर के रूप में रिकर्सिवली देखने के लिए विकल्प को जोड़ना सुनिश्चित करें; – OzzyTheGiant

+0

से ऊपर दिए गए दस्तावेज़ देखें @ OzzyTheGiant's नोट पर जोड़ा गया नोट: रिकर्सिव विकल्प केवल मैकोज़ और विंडोज पर समर्थित है। –

संबंधित मुद्दे