Bricktop
Bricktop

Reputation: 153

How to move non-temp files to dedicated subdirectories?

I need help building a script that goes through each file in the root of the current directory, comparing their filename.ext against a blacklist of masks to exclude (*.!qb, *.parts, etc). The unmatched files are sent to their dedicated directories using syntax filename\filename.ext

My old batch code is not powershell ready but may provide some clarity.

for %%s in (*.*) do if not "%%~xs"==".!qB" if not "%%~xs"==".parts" (
md "%%~ns" & move "%%s" "%%~ns\")

Please help me understand the commands needed to replace these.

Upvotes: 0

Views: 54

Answers (1)

user6811411
user6811411

Reputation:

Slightly changed command from comment.

gci * -file | ? Extension -notin '.!qb','.parts' | % {md $_.BaseName ;move $_.FullName -Destination ("{0}\" -f $_.BaseName) -force}

More verbose variant:

Get-ChildItem * -File | 
  Where-Object Extension -notin '.!qb','.parts' | ForEach-Object {
    New-Item  $_.BaseName -ItemType Directory | Out-Null
    Move-Item -LiteralPath $_.FullName -Destination ("{0}\" -f $_.BaseName) -force
  }

sample tree after running script:

> tree /f
A:.
│   filename.!qb
│   filename.parts
│
└───filename
        filename.mkv

Upvotes: 1

Related Questions