{"id":864,"date":"2020-12-07T16:04:13","date_gmt":"2020-12-07T22:04:13","guid":{"rendered":"https:\/\/asberry.org\/blog_tech\/?p=864"},"modified":"2020-12-07T16:05:28","modified_gmt":"2020-12-07T22:05:28","slug":"powershell-deleting-empty-csv-files","status":"publish","type":"post","link":"https:\/\/asberry.org\/blog_tech\/?p=864","title":{"rendered":"Powershell &#8211; Deleting Empty CSV Files"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes in integration, you have jobs that run every 5 minutes or so, now these jobs run regardless if there&#8217;s data or not and they&#8217;ll produce an &#8217;empty&#8217; file. That&#8217;s to say there isn&#8217;t any usable data in it, just a header row. So you have one file every 5 minutes, that&#8217;s 12 an hour or 288 in a day. Some are going to have date, others not. While this can be done with a batch file (elsewhere on this site), Powershell offers a quicker way to do the same thing. Below is a Powershell script that looks through CSV&#8217;s and kills ones with only one line of data (header) leaving anything with real data in place.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n$path=&quot;C:\\onedrive_tt\\Client\\TCC_Touchpoints-Maintenance\\scripts\\20201207-CleanupTest\\response\\&quot;\nGet-ChildItem $path -Filter *.csv |\nForEach-Object {\n$content = $path+&quot;$_&quot;\n&#x5B;int]$LinesInFile = 0\n$reader = New-Object IO.StreamReader $content\n while($reader.ReadLine() -ne $null){ $LinesInFile++ }\n$reader.Close()\n#Write-Host($content+&quot; - &quot;+$LinesInFile)\nif ($LinesInFile -eq 1)\n{\n#Write-Host(&quot;File: &quot;+$content+&quot; with &quot;+$LinesInFile+&quot; line will be deleted&quot;)\nRemove-Item -Path $content -Force\n}\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Sometimes in integration, you have jobs that run every 5 minutes or so, now these jobs run regardless if there&#8217;s data or not and they&#8217;ll produce an &#8217;empty&#8217; file. That&#8217;s to say there isn&#8217;t any usable data in it, just a header row. So you have one file every 5 minutes, that&#8217;s 12 an hour [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[23],"tags":[],"class_list":["post-864","post","type-post","status-publish","format-standard","hentry","category-powershell","author-aron"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4bBkH-dW","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts\/864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=864"}],"version-history":[{"count":2,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts\/864\/revisions"}],"predecessor-version":[{"id":866,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts\/864\/revisions\/866"}],"wp:attachment":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}