{"id":1425,"date":"2014-01-15T10:47:07","date_gmt":"2014-01-15T04:47:07","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1425"},"modified":"2016-01-07T11:31:03","modified_gmt":"2016-01-07T05:31:03","slug":"powershell-script-copy-files-one-folder-another-based-part-file-name","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1425","title":{"rendered":"A powershell script to copy files from one folder to another based on part of file name"},"content":{"rendered":"<p>A requirement came up for me to copy thousands of file to another drive with directories named depending on part of the file name. \u00c2\u00a0The file name has _ on it and every time there is an _ a subdirectory needs to be created . For example :<\/p>\n<p>if the file name is :<\/p>\n<p>C:\\query\\ RCP_DEV_orderlookup.sql \u00c2\u00a0 needs to be copied to D:\\query\\RCP\\DEV\\orderlookup.sql<\/p>\n<p>C:\\query \\RCP_PROD_orderlookup.sql \u00c2\u00a0 needs to be copied to D:\\query\\RCP\\PROD\\orderlookup.sql<\/p>\n<p>and so on<\/p>\n<p>First I thought of writing a script in perl, but since it was in a windows system , I decided to write it in powershell. \u00c2\u00a0I am fascinated with the power of powershell !<\/p>\n<p>Here is the script I wrote to accomplish that :<\/p>\n<p><span style=\"font-size: small;\"><span style=\"color: #ff421e;\">$SourceFolder<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">=<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8220;C:\\query\\&#8221;<\/span><br \/>\n<span style=\"color: #ff421e;\">$targetFolder<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">=<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8220;D:\\query\\&#8221;<\/span><br \/>\n<span style=\"color: #ff421e;\">$numFiles<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">=<\/span>\u00c2\u00a0(<span style=\"color: blue;\">Get-ChildItem<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Path<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$SourceFolder<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Filter<\/span>\u00c2\u00a0<span style=\"color: #8242ff;\">*.TXT<\/span>)<span style=\"color: #a2a2a2;\">.<\/span>Count<br \/>\n<span style=\"color: #ff421e;\">$i<\/span><span style=\"color: #a2a2a2;\">=0<\/span><\/span><\/p>\n<p><span style=\"color: blue;\">clear-host<\/span>;<br \/>\n<span style=\"color: blue;\">Write-Host<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216;This script will copy &#8216;<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$numFiles<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216; files from &#8216;<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$SourceFolder<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216; to &#8216;<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$targetFolder<\/span><br \/>\n<span style=\"color: blue;\">Read-host<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-prompt<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216;Press enter to start copying the files&#8217;<\/span><br \/>\n<span style=\"font-size: small;\"><span style=\"color: blue;\">Get-ChildItem<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Path<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$SourceFolder<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Filter<\/span>\u00c2\u00a0<span style=\"color: #8242ff;\">*.TXT<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">|<\/span>\u00c2\u00a0<span style=\"color: blue;\">%<\/span>{\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0<span style=\"color: #a2a2a2;\">[<\/span><span style=\"color: #008080;\">System.IO.FileInfo<\/span><span style=\"color: #a2a2a2;\">]<\/span><span style=\"color: #ff421e;\">$destination<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">=<\/span>\u00c2\u00a0(<span style=\"color: blue;\">Join-Path<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Path<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$targetFolder<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-ChildPath<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$_<\/span><span style=\"color: #a2a2a2;\">.<\/span>Name<span style=\"color: #a2a2a2;\">.<\/span>replace(<span style=\"color: #800000;\">&#8220;_&#8221;<\/span><span style=\"color: #a2a2a2;\">,<\/span><span style=\"color: #800000;\">&#8220;\\&#8221;<\/span>))\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0<span style=\"color: #000080;\">if<\/span>(<span style=\"color: #a2a2a2;\">!<\/span>(<span style=\"color: blue;\">Test-Path<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Path<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$destination<\/span><span style=\"color: #a2a2a2;\">.<\/span>Directory )){\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0<span style=\"color: blue;\">New-item<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Path<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$destination<\/span><span style=\"color: #a2a2a2;\">.<\/span>Directory<span style=\"color: #a2a2a2;\">.<\/span>FullName\u00c2\u00a0<span style=\"color: #000080;\">-ItemType<\/span>\u00c2\u00a0<span style=\"color: #8242ff;\">Directory<\/span>\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0 }\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0<span style=\"color: #a2a2a2;\">[<\/span><span style=\"color: #008080;\">int<\/span><span style=\"color: #a2a2a2;\">]<\/span><span style=\"color: #ff421e;\">$percent<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">=<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$i<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">\/<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$numFiles<\/span>\u00c2\u00a0<span style=\"color: #a2a2a2;\">*<\/span>\u00c2\u00a0<span style=\"color: #800080;\">100<\/span>\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0<\/span><br \/>\n<span style=\"font-size: small;\">\u00c2\u00a0 \u00c2\u00a0\u00c2\u00a0<span style=\"color: blue;\">copy-item<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Path<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$_<\/span><span style=\"color: #a2a2a2;\">.<\/span>FullName\u00c2\u00a0<span style=\"color: #000080;\">-Destination<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$Destination<\/span><span style=\"color: #a2a2a2;\">.<\/span>FullName<br \/>\n<span style=\"color: blue;\">Write-Progress<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-Activity<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8220;Copying &#8230; (<\/span><span style=\"color: #ff421e;\">$percent<\/span><span style=\"color: #800000;\">\u00c2\u00a0%)&#8221;<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-status<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$_<\/span>\u00c2\u00a0\u00c2\u00a0<span style=\"color: #000080;\">-PercentComplete<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$percent<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-verbose<\/span><br \/>\n<span style=\"color: #ff421e;\">$i<\/span><span style=\"color: #a2a2a2;\">++<\/span><br \/>\n}<br \/>\n<span style=\"color: blue;\">Write-Host<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216;Total number of files read from directory &#8216;<\/span><span style=\"color: #ff421e;\">$SourceFolder<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216; is &#8216;<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$numFiles<\/span><br \/>\n<span style=\"color: blue;\">Write-Host<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216;Total number of files that was copied to &#8216;<\/span><span style=\"color: #ff421e;\">$targetFolder<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8216; is &#8216;<\/span>\u00c2\u00a0<span style=\"color: #ff421e;\">$i<\/span><br \/>\n<span style=\"color: blue;\">Read-host<\/span>\u00c2\u00a0<span style=\"color: #000080;\">-prompt<\/span>\u00c2\u00a0<span style=\"color: #800000;\">&#8220;Press enter to complete&#8230;&#8221;<\/span><br \/>\n<span style=\"color: blue;\">clear-host<\/span>;\u00c2\u00a0<\/span><\/p>\n<p>As always test it before you implement it on your system.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A requirement came up for me to copy thousands of file to another drive with directories named depending on part of the file name. \u00c2\u00a0The file name has _ on it and every time there is an _ a subdirectory needs to be created . For example : if the file name is : C:\\query\\ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[358,1],"tags":[876,878,881,875,880,879,877],"class_list":["post-1425","post","type-post","status-publish","format-standard","hentry","category-how-to","category-misc","tag-copy-files","tag-get-childitem","tag-new-item","tag-powershell","tag-read-host","tag-write-progress","tag-write-host"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1425"}],"version-history":[{"count":3,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1425\/revisions"}],"predecessor-version":[{"id":1428,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1425\/revisions\/1428"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}