{"id":272,"date":"2019-01-09T07:23:06","date_gmt":"2019-01-08T19:23:06","guid":{"rendered":"https:\/\/www.berserkir.net\/wordpress\/?p=272"},"modified":"2019-01-09T07:23:15","modified_gmt":"2019-01-08T19:23:15","slug":"moving-files-to-subfolders","status":"publish","type":"post","link":"https:\/\/www.berserkir.net\/wordpress\/moving-files-to-subfolders\/","title":{"rendered":"Moving files to subfolders"},"content":{"rendered":"\n<p>It&#8217;s not uncommon for me to have to tidy up directories full of files by moving them to their own subfolders, named for each file. It&#8217;s become common enough that I wrote a little Powershell function to do it repeatedly.<\/p>\n\n\n\n<p>After saving the below script as a .psm1 file you&#8217;ll be able to open PS and import the file. I tend to save it to &#8220;C:\\Scripts\\MoveToSubfolders.psm1&#8221; &#8211; then I can open PS and run &#8220;Import-Module C:\\Scripts\\MoveToSubfolders.psm1&#8221;.<br><br>Once it&#8217;s been imported I can then use the function on whatever folder of files I need to tidy by simply running &#8220;MoveToSubfolders&#8221; and following the function with the folder I need to sort. It works well for making movies tidier.1`<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MoveToSubfolders([string]$Folder = \".\\\" ){\n# Convert relative folder paths to full paths\n$FullPath = Resolve-Path $Folder\n\n# Generate list of all files in the specified directory.\n$Files = Get-ChildItem -Path ((Get-Item -Path $FullPath -Verbose).FullName)\n\n# For each object in the directory, do the following:\n$Files | ForEach-Object {\n    # Generate the variables \n    $FileName = $_.FullName\n    $FileFolderName = $_.BaseName\n    $DestinationFolder = \"$FullPath\\$FileFolderName\"\n    $MoveDestination = \"$DestinationFolder\\$($_.Name)\"\n\n    # Check if the destination directory exists\n    if(!(Test-Path $DestinationFolder))\n    {\n        # Create the directory using FileName (minus file extension) if it does not exist\n        New-Item -Path $DestinationFolder -ItemType Directory\n    }\n    # Move the file to the new directory\n    Move-Item -ErrorAction SilentlyContinue $FileName $MoveDestination\n}\n}\n# Actually allow this module to have functions used outside via import\nExport-ModuleMember -Function *\nExport-ModuleMember -Variable *<\/code><\/pre>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s not uncommon for me to have to tidy up directories full of files by moving them to their own subfolders, named for each file. It&#8217;s become common enough that I wrote a little Powershell function to do it repeatedly. After saving the below script as a .psm1 file you&#8217;ll be able to open PS &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"https:\/\/www.berserkir.net\/wordpress\/moving-files-to-subfolders\/\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,20,16,1],"tags":[61,62],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-helpful-hints","category-scripts","category-software","category-uncategorized","tag-powershell","tag-script","item-wrap"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":2,"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/posts\/272\/revisions\/274"}],"wp:attachment":[{"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/media?parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/categories?post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.berserkir.net\/wordpress\/wp-json\/wp\/v2\/tags?post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}