YouTube2Text

SQL Server Integration Services (SSIS) Part 11 - Looping Over Files — Transcript

by WiseOwlTutorials · 3,822 words · 498 segments · language en · Watch on YouTube

Full transcript

  1. 0:00welcome to this wise our tutorial on
  2. 0:02leaping over files in sequel server
  3. 0:04integration services here's what you'll
  4. 0:07learn in the tutorial we'll begin with
  5. 0:10explaining what the problem is that
  6. 0:12we're trying to resolve and then show
  7. 0:14how to delete any old rows and sequel
  8. 0:16server tables and how to delete any old
  9. 0:18files on your hard disk so that you can
  10. 0:20rerun the package again and again then
  11. 0:23we'll get into the meat of the problem
  12. 0:24we'll look at how to loop over files
  13. 0:26using a for each container and how to
  14. 0:28add the file names via a variable into a
  15. 0:31sequel server table we'll get then going
  16. 0:35to look at conditional copying so we'll
  17. 0:37copy only certain files into it a
  18. 0:40separate folder using something called
  19. 0:42an expression constraint and then we'll
  20. 0:44set a variable to count how many files
  21. 0:46have been copied finally we'll finish
  22. 0:49off the package by counting up the
  23. 0:51number of rows in the sequel server
  24. 0:52table and displaying the two variable
  25. 0:55counts we've accumulated in a message
  26. 0:57box using a script task so let's get
  27. 1:00started in order to be able to create a
  28. 1:04package you need to know what is going
  29. 1:06to do so let's have a careful look at
  30. 1:08what we're trying to achieve in this
  31. 1:09more ambitious package I've got a folder
  32. 1:12here called folder containing files and
  33. 1:15it contains some files in the media
  34. 1:17folder and also if you drill down plenty
  35. 1:20of files in subfolders to what everyone
  36. 1:24to do is write a package which
  37. 1:25recursively loops over all the other
  38. 1:27files in that folder and its subfolders
  39. 1:29doing two things to them the first thing
  40. 1:33we'll do is put the names of the file in
  41. 1:35a sequel server table like this the
  42. 1:39second thing it will do is it will copy
  43. 1:41the files to this folder but it will
  44. 1:44only copy those files which have got a
  45. 1:46txt extension and finally I want to
  46. 1:50display a message saying how many files
  47. 1:52were in the sequel server table and how
  48. 1:54many files have been copied to this
  49. 1:55folder now I've got a package which I
  50. 2:00created earlier here so let's see what
  51. 2:02the final output is going to look like
  52. 2:03before we create it the first thing this
  53. 2:06package will do is get rid of any old
  54. 2:09rose from the sequel server table and
  55. 2:11get rid of any files from the
  56. 2:13read folder so that's just setting up
  57. 2:15our star conditions what the package
  58. 2:18will then do is leap over all the files
  59. 2:20in the folder and its subfolders doing
  60. 2:23three things to reach firstly it will
  61. 2:26add a record to the sequel server table
  62. 2:28giving the name of the file secondly it
  63. 2:32will copy the file to the folder but
  64. 2:35only if it ends with the extension txt
  65. 2:37so we'll use a precedence constraint to
  66. 2:39do that and thirdly we'll increase the
  67. 2:43number of files we found by one in a
  68. 2:45variable after leaping over all the
  69. 2:48files will then be in a position to
  70. 2:50count up how many rows are in the sequel
  71. 2:52server table and finally show a message
  72. 2:55displaying what we've discovered if I
  73. 2:57now run this package to show what it
  74. 3:00does this is the final message is going
  75. 3:02to display 14 files found of which nine
  76. 3:05text files were copied to the new folder
  77. 3:08and sure enough if we go into sequel
  78. 3:11server you'll see that there are 14
  79. 3:13files found that's why it goes up to 14
  80. 3:15and if we go back into Windows Explorer
  81. 3:17and have a look at our copy files you'll
  82. 3:20see there's nine files in this folder so
  83. 3:23that's what we aiming to achieve it's
  84. 3:25time to get started to make this video
  85. 3:30shorter than it might have otherwise
  86. 3:31been I set up some things to begin with
  87. 3:34so we've already got a table called TBL
  88. 3:36file in sequel server which contains a
  89. 3:39column called file name which is a
  90. 3:41string or text type it's NVR car 200 and
  91. 3:44in integration services I've already
  92. 3:47created a package I've already created
  93. 3:50the connection manager to sequel server
  94. 3:52and already created an execute SQL task
  95. 3:55and what this does is delete all the old
  96. 3:58rows from the table so that it doesn't
  97. 3:59keep accumulating more and more rows and
  98. 4:02if we just double click on there to edit
  99. 4:04it you can see that it uses the
  100. 4:06connection I've really created two
  101. 4:07sequel server an SQL statement is to
  102. 4:10truncate the table that is to remove any
  103. 4:12records from it but leaving the design
  104. 4:14of the table intact what I now need to
  105. 4:17do is to make sure that the copied files
  106. 4:20folder is empty at the moment it
  107. 4:23contains the nine files I've copied over
  108. 4:25the previous time I'm
  109. 4:26and the package so what I'm going to do
  110. 4:28is to create a file system task to
  111. 4:31delete all those rows or all those files
  112. 4:33so I can do that by adding a file system
  113. 4:36task I'll rename it to say empty folder
  114. 4:42a short description and then what I need
  115. 4:46to do is configure it to say what it's
  116. 4:47going to do with a file system task the
  117. 4:50best place to start is with the
  118. 4:52operation in this case it's going to be
  119. 4:55to delete a directory content you can
  120. 4:57see all of the standard file operations
  121. 4:59you might choose to pick from are listed
  122. 5:02so we'll delete the directory content
  123. 5:04what I now need to do say which
  124. 5:06directory sadly I haven't got a variable
  125. 5:10containing the path to the source so
  126. 5:13what I'm going to do instead is to have
  127. 5:14to create a connection I can do that by
  128. 5:18clicking on the drop arrow and because I
  129. 5:19haven't already created a connection I'm
  130. 5:21going to create one on the fly now so
  131. 5:23I'll choose new connection it's an
  132. 5:26existing folder which I want to work
  133. 5:28with and I can click on the Browse
  134. 5:31button to choose that folder and I'll
  135. 5:35just go to it and then choose ok if I
  136. 5:40then choose ok again I've now got a
  137. 5:42package which I can run so let's try it
  138. 5:45out it works successfully it's run these
  139. 5:51two tasks in probably in different
  140. 5:52threads because there's no constraint
  141. 5:54between them or no flow between them but
  142. 5:56it will still have run both the two
  143. 5:57tasks and if I now end that I can have a
  144. 6:01look at sequel server and if I have a
  145. 6:03look at my file table and look at the
  146. 6:05rows within it you can see it's empty
  147. 6:07and if I go to my folder and Windows
  148. 6:10Explorer you can see that empty too so
  149. 6:14that's a good start I've set up my my
  150. 6:16position I've got an empty table and an
  151. 6:18empty folder what I now need to do is to
  152. 6:20loop over all the files adding the rows
  153. 6:23into the sequel server table and copying
  154. 6:25the files into the folder in Windows
  155. 6:27Explorer
  156. 6:30so what I'm going to do is loop over all
  157. 6:33the files in the folder and to do that I
  158. 6:35need a container and specifically a for
  159. 6:38each loop container so I'll add that
  160. 6:40from the SSIS toolbox and rename it to
  161. 6:43say what is going to do which is to loop
  162. 6:45over files before I can configure this
  163. 6:49what I need to do is do two things the
  164. 6:52first is I need to feed the output from
  165. 6:54the old tasks into it to make sure
  166. 6:55everything happens in the correct order
  167. 6:57and secondly I need to create a variable
  168. 7:00to hold the name of each file as we go
  169. 7:03around the loop so I'm going to call my
  170. 7:05variable file name and it's going to be
  171. 7:09of type string we'll see later in this
  172. 7:11tutorial I'll actually regret leaving
  173. 7:13that value blank but for the moment I'll
  174. 7:16leave it as it is so now I've created my
  175. 7:19variable what I can do is configure my
  176. 7:21file leapin tasks by double-clicking the
  177. 7:23icon as usual if I get to the collection
  178. 7:27tab I can choose a number of different
  179. 7:29enumerators and you can see you can loop
  180. 7:32over rows items XML files some of those
  181. 7:35will come in later packages later
  182. 7:37tutorials rather but for the moment
  183. 7:39we'll choose to loop over all files what
  184. 7:42I then need to do is say which folder
  185. 7:44I'm leaping over so I can click on the
  186. 7:45Browse button and select the folder and
  187. 7:51having done that I can choose ok I'm
  188. 7:54going to choose all files if I wanted to
  189. 7:57I could choose txt files at this point
  190. 8:00but what I wouldn't then be able to do
  191. 8:01is to put the names of all the files in
  192. 8:04a separate sequel server table it would
  193. 8:05just leap over the text files its
  194. 8:09importantly the file name is fully
  195. 8:10qualified because I want to pick up not
  196. 8:13only the name of the file but also the
  197. 8:15folder within it so that I can easily
  198. 8:16use that for copying files later and
  199. 8:19finally in this dialog box I want to
  200. 8:22make sure that I traverse subfolders so
  201. 8:24that I don't just leap over all the
  202. 8:25files in the main folder one core folder
  203. 8:28containing files but also in any
  204. 8:30subfolders contained within that and
  205. 8:31their subfolders too and so on
  206. 8:34recursively the final thing I need to do
  207. 8:37in this dialog box is to go to variable
  208. 8:39mappings and say that in the file name
  209. 8:42folder that I've created
  210. 8:43I wanted to store the first and only
  211. 8:48filename returned from leaping over the
  212. 8:51files it seems rather strange that I can
  213. 8:54choose an index here and for the moment
  214. 8:56I would just assume that you will always
  215. 8:57store the first and only item in the
  216. 8:59collection returned which of course will
  217. 9:01be index number 0 in the file name so
  218. 9:05having done all that I can choose ok and
  219. 9:08if I now run this package I'm going to
  220. 9:12ask you to believe that it stored the
  221. 9:14name of all 14 files in a variable and
  222. 9:17finished I could set ups and debugging
  223. 9:20to check that but I'm a trusting person
  224. 9:22so I'm going to assume that's what's
  225. 9:24occurring what we now need to do is to
  226. 9:27store the names of the files in a sequel
  227. 9:29server table and to do that I need to
  228. 9:31create a separate task so having
  229. 9:37successfully I hope looped over all the
  230. 9:39files what I now need to do is add the
  231. 9:42name of each file into a sequel server
  232. 9:43table and to do that as you can see I'll
  233. 9:46add an execute SQL task which will add
  234. 9:49the file name to a table I need to
  235. 9:54configure this task to say which
  236. 9:56connection is going to use it's going to
  237. 9:58use the one which rather misleadingly I
  238. 9:59called x-factor which will connect the
  239. 10:02SSIS tutorial database and it needs to
  240. 10:06use a sequel statement so if I click on
  241. 10:10the three dots to enter the statement
  242. 10:11I've really put this in the clipboard so
  243. 10:13what it's going to do is insert into the
  244. 10:15table called TBL file in the column
  245. 10:17called filename the value as specified
  246. 10:22by a parameter which are now create so
  247. 10:25if I choose ok to confirm that what I
  248. 10:27can now do is go to parameter mapping
  249. 10:29and add a parameter now what I want to
  250. 10:33do is say that the variable I've created
  251. 10:36called filename which will hold the name
  252. 10:40of each file as I go around the loop
  253. 10:42will which is of type it's just just
  254. 10:47writing that over me it's a string
  255. 10:50variable and bar car and what it will do
  256. 10:53is become parameter number
  257. 10:56zero so what will happen as I go around
  258. 10:59the loop is SSAS will automatically put
  259. 11:03the name of the file as the first
  260. 11:04parameter to the sequel statement I'm
  261. 11:06attempting to run so that when I come to
  262. 11:09run this statement the name of the file
  263. 11:11will be substitute substituted as the
  264. 11:14first and only parameter in this equals
  265. 11:16statement let's see if it works if I now
  266. 11:20run this package it seems to work
  267. 11:22successfully if I now go to sequel
  268. 11:25server and have a look at the table you
  269. 11:29can see sure enough it's got the
  270. 11:30four-team rosen so it's worked
  271. 11:32successfully so we've achieved about
  272. 11:38half of what we set out to we've added
  273. 11:40the file name to a sequel server table
  274. 11:42what I now want to do is to add a task
  275. 11:44which will automatically copy the file
  276. 11:45if it's a text file to the relevant
  277. 11:48folder and to do that I need a
  278. 11:50filesystem task so I'll drag that in and
  279. 11:53make sure it follows on directly from
  280. 11:55the previous task and I'll rename it to
  281. 11:58say copy txt files only what I now need
  282. 12:04to do is to configure this task to say
  283. 12:06what it's going to do so I can double
  284. 12:07click on the icon to the left of it the
  285. 12:10destination path when copying it to is
  286. 12:13going to be the connection I've already
  287. 12:16set up called copy files if I could just
  288. 12:19come out of there for a second you can
  289. 12:20see that when I created a new connection
  290. 12:22in the previous filesystem tasks up
  291. 12:24there it automatically added that
  292. 12:26connection as part of this package so I
  293. 12:28can refer to it again when I'm copying
  294. 12:29my file so going back into that so it's
  295. 12:33going to copy the files it's going to
  296. 12:35overwrite the files in the destination
  297. 12:36seems to make sense if there are any
  298. 12:38it's going to copy a file fortunately
  299. 12:41that was a default already set up so I
  300. 12:43didn't need to change that as to where
  301. 12:45it's going to get the file from the
  302. 12:47answer to that is the file name is
  303. 12:49already held in a variable so what I can
  304. 12:53do is choose our variable from the list
  305. 12:54and it will automatically copy the name
  306. 12:58of the file which this pass around the
  307. 13:00loop is held in a variable called file
  308. 13:02name into the folder called copy files
  309. 13:05so I can choose ok
  310. 13:08there's two problems the first is quite
  311. 13:10easily solved if I let my Mouse linger
  312. 13:13over that you can see that it says the
  313. 13:15filename is used as the variable is used
  314. 13:17as a source or destination as empty now
  315. 13:20this is a bit of a silly message because
  316. 13:22as I loop over the files each time
  317. 13:24around the loop the farm name variable
  318. 13:25will automatically be populated so in
  319. 13:28fact the fact that it's empty at the
  320. 13:29moment isn't shouldn't be a problem but
  321. 13:33because it's an annoying error message
  322. 13:34I'm just going to type in something
  323. 13:36ridiculous like test in there and just
  324. 13:39to clear the error message and
  325. 13:40eventually that message will actually go
  326. 13:42it takes a while to catch up but where I
  327. 13:45can speed that up is to double click on
  328. 13:47it and choose ok the second problem I
  329. 13:50alluded to is that at the moment is
  330. 13:52going to copy all files and not just txt
  331. 13:55ones so what I need to do is put a
  332. 13:57precedence constraint on this arrow so
  333. 14:01that it only goes onto this task
  334. 14:03if the file is as a txt extension now
  335. 14:09there's two types of constraints you can
  336. 14:10set you can set constraints or
  337. 14:13expressions constraints is when you want
  338. 14:17something Dierker if the previous task
  339. 14:19succeeded failed or was completed you
  340. 14:22can set an expression task when what you
  341. 14:24want to do is slightly more complicated
  342. 14:25as in this case so I can click on the 3
  343. 14:29dots to create my expressions and what I
  344. 14:31want to do is say with a file name
  345. 14:32engine txt all string comparisons in SSS
  346. 14:36ok sensitive so the first thing I'm
  347. 14:38going to do is go to the list of string
  348. 14:40functions and find the upper function
  349. 14:42and drag that in the second thing I'm
  350. 14:47going to do is to use the write function
  351. 14:51which drug-to-drug that in there to say
  352. 14:54I'd pick our characters from the
  353. 14:55rightmost edge of a string of text the
  354. 15:00place I'm getting the characters from is
  355. 15:02the file name so I can substitute that
  356. 15:04in there and the number of characters
  357. 15:07I'm picking out is 4 dot txt so taking
  358. 15:13the last four characters in the file
  359. 15:14name if they equal txt
  360. 15:19making sure that I type that in
  361. 15:20uppercase because I've used the upper
  362. 15:22function to convert it on the left hand
  363. 15:24side now there's one error in that I
  364. 15:26then if you spotted it which is because
  365. 15:28this is a c-sharp type language when I'm
  366. 15:31doing a comparison I need to use equals
  367. 15:33equals otherwise when I click on the
  368. 15:35evaluate expression button I'll get an
  369. 15:37error message so that's my expression if
  370. 15:40I choose okay twice when I run this
  371. 15:42package what it should do is copy the
  372. 15:45files only where they're text files so
  373. 15:48if I now come out of that and go and
  374. 15:50have a look in Explorer you can see that
  375. 15:52it's copied just text files there's 14
  376. 15:54files but only nine of them will be
  377. 15:56copied across so all looking very good
  378. 16:03what I now want to do is to count up the
  379. 16:06number of text files copied and to do
  380. 16:08that I can add an expression task here
  381. 16:10which will add one to a variable which I
  382. 16:13now need to create so let's go into
  383. 16:14variables and what I'll do is I'll add a
  384. 16:16new variable which I'll call file count
  385. 16:19and it's going to be of type integer
  386. 16:21crucially its initial value is going to
  387. 16:24be zero and each time I find a new text
  388. 16:26file I'll add one to it so what I can
  389. 16:29now do is add an expression to ask into
  390. 16:31my loop over files container and make
  391. 16:33sure that it follows on from the
  392. 16:35previous task and that way it will only
  393. 16:37run if this expression constraint is
  394. 16:40true ie F the file ends in txt so what
  395. 16:43this will do is increment number of
  396. 16:46files as we'll call it and what I now
  397. 16:51need to do is go in and configure that
  398. 16:52to say what is going to do so what it's
  399. 16:55going to do is set the variable called
  400. 16:57file count equal to the previous value
  401. 17:02of the variable called fire account plus
  402. 17:041 so we turn around the loop the
  403. 17:06variable value will be increased by 1
  404. 17:08just check that works it does and choose
  405. 17:12ok and if I now run that again
  406. 17:16there won't be any proof of this but my
  407. 17:21expectation is by the time it have
  408. 17:23finished this variable called file count
  409. 17:26held the number 9 what I now need to do
  410. 17:29is finish up by reporting on to values
  411. 17:32one is the number of text files copied
  412. 17:34and one number and one is the number of
  413. 17:37rows added to the sequel server table
  414. 17:41what I now want to do is to calculate
  415. 17:44the total number of files which are
  416. 17:46added to this table now I could have
  417. 17:49done that by using a variable just as I
  418. 17:51did with a number of text files copied
  419. 17:53but what I'm going to do instead is
  420. 17:55create a data flow task after the loop
  421. 17:58over files containers finished and use
  422. 18:01this the count excuse me the count at
  423. 18:05one more time to count the number of
  424. 18:08rows and if I double click on this to go
  425. 18:13into the data flow the source for this
  426. 18:16I'm going to use a sources system to
  427. 18:18help me is going to come directly from
  428. 18:22my table of files so I'll call that
  429. 18:29total of files and the destination for
  430. 18:32it is going to be a row count which if
  431. 18:35you remember is a handy way of counting
  432. 18:37up how many rows are on a table and I'll
  433. 18:39feed the one into the other the problem
  434. 18:42is that the row count needs a variable
  435. 18:44to hold the information in and we can't
  436. 18:46use a file count because that's been
  437. 18:47used for another purpose so I need to
  438. 18:49create a third variable which I'll call
  439. 18:51row count and again it will be an
  440. 18:55integer variable so what I can now do is
  441. 18:58configure the row count to store the
  442. 19:00information in the row count variable so
  443. 19:03the results of running this data flow
  444. 19:04task will be a variable holding the
  445. 19:06number of rows added into the sequel
  446. 19:08server table I can send the output from
  447. 19:13the container into their row count task
  448. 19:14and then finally what I can do is
  449. 19:17display the information using a script
  450. 19:19task so what I'll do is run the count
  451. 19:22number of rows into my script tasks and
  452. 19:24rename this to say show output I'm going
  453. 19:30to go through this relatively quickly
  454. 19:31because we've done this in previous
  455. 19:32tutorials if I now double click on that
  456. 19:35I need to pass in two variables into my
  457. 19:38script tasks the first one will be the
  458. 19:41row count and the second one will be the
  459. 19:44file count
  460. 19:46if I then choose to edit the script and
  461. 19:48I'm going to use visual C sharp as my
  462. 19:50language just because that's the default
  463. 19:52what I can now do is to display those
  464. 19:59two variables so I'm going to create a
  465. 20:01variable an integer variable which I'll
  466. 20:04call fire count and set that equal to
  467. 20:07the value of the variable called file
  468. 20:09count as passed in but I need to convert
  469. 20:12that into an integer so that it will
  470. 20:16work I'll just complete that and what
  471. 20:23I'm hoping is that that line will
  472. 20:25automatically store the file count
  473. 20:26variable in a variable called file count
  474. 20:28and I'll just do a very similar thing
  475. 20:30for the row count copying and pasting
  476. 20:33the line to make it a bit easier and
  477. 20:35finally I can display the results so I
  478. 20:39can use a message box to share the
  479. 20:41results and I'm going to keep it quite
  480. 20:45short I'm going to display the file
  481. 20:48count and then the com Rin and then
  482. 20:54display the row count oops and with a
  483. 21:02bit of luck that will do it
  484. 21:03I've got no underlining so I'm going to
  485. 21:05just close out down choose ok to confirm
  486. 21:07it and then run the package let's see
  487. 21:10what happens so I've run the package I
  488. 21:14get the file count as 9 and the row
  489. 21:16count is 14 so I've copied nine text
  490. 21:18files into the copy files folder but
  491. 21:20I've added 14 rows into the sequel
  492. 21:22server table which is pretty not for me
  493. 21:24that everything works successfully we
  494. 21:28hope you've enjoyed this tutorial on
  495. 21:30integration services leaping over files
  496. 21:32you can find lots more resources on net
  497. 21:36sequel server and Microsoft Office
  498. 21:38training at ww-why UK

About this transcript

This page contains the full transcript of SQL Server Integration Services (SSIS) Part 11 - Looping Over Files by WiseOwlTutorials, generated from the public captions YouTube serves with the video. The transcript has 3,822 words across 498 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.