trigger that creates folder | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

trigger that creates folder

Does anyone know if it is possible (and if yes, how) to create a trigger that creates a folder for every inserted record?
In a specific root folder, I want a subfolder for every record in the table. How does the trigger have to look like to make the folder automatically with every insert of a new record??? thanks!!

You really shouldn’t do this. <img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ /> If you are going to though, this code should give you a start. You will need to add logic to INSERT all the rows from the "inserted" table of the trigger. You will then need to get the EXEC master..xp_cmdshell ‘dir c: /A<img src=’/community/emoticons/emotion-2.gif’ alt=’:D‘ />’ for whatever directory you are looking at. You can then compare the results and use a loop to create/delete necessary directories. This code should get you started. It ONLY creates one directory, so you still have quite a bit of work to do. Give it a shot though, and let us know if you need help.<br /><br /><pre id="code"><font face="courier" size="2" id="code"><br />EXEC master..xp_cmdshell ‘dir c: est'<br />GO<br /><br />CREATE TABLE test_trigger1(<br />test VARCHAR(50))<br /><br />GO<br />CREATE TRIGGER ins_test_trigger1<br />ON test_trigger1<br />FOR INSERT<br /><br />AS<br /><br />CREATE TABLE #test(shell VARCHAR(255))<br /><br />INSERT #test(shell)<br />EXEC master..xp_cmdshell ‘dir c: est'<br /><br />IF (SELECT shell FROM #test WHERE shell = ‘ Directory of c: est’) IS NULL<br />BEGIN<br />EXEC master..xp_cmdshell ‘md c: est'<br />END<br />GO<br /><br />INSERT test_trigger1(test)<br />SELECT ‘test'<br /><br />EXEC master..xp_cmdshell ‘dir c: est'<br />–EXEC master..xp_cmdshell ‘rd c: est'<br />GO<br />DROP TABLE test_trigger1<br />GO<br /></font id="code"></pre id="code"><br /><br />MeanOldDBA<br />[email protected]<br /><br />When life gives you a lemon, fire the DBA.