Powershell find and move items -


hey there need move lot of files using powershell script , kinda need on one.

the root following

> tree . \ ├───folder1 │   ├───sub1 │   │   └───stockfile │   └───sub2 └───folder2     ├───sub1     │   └───stockfile     └───sub2 

the script needs find stockfiles , need move them sub2 folder of each mainfolder.

i made following script show stockfiles in list:

$var = get-childitem -filter *stock* -recurse foreach ($file in $var) { write-host ($file.fullname) } 

try following:

get-childitem -file -recurse -filter *_stock.csv |   move-item -whatif -destination { $_.fullname -replace '^(.*)\\sub1\\','$1\sub2\' } 

the approach extracts path prefix ...\sub1 full path of each stock file , makes ...\sub2 destination (directory) path.

that way, stock files moved anywhere in ...\sub1 subtree directly ...\sub2.

note:

  • -whatif shows would happen first; remove perform actual moving.

  • the assumption stock file paths contain one sub1 component, , there no stock files duplicate names in sub1 subtree.

  • -replace interprets first operand regular expression, why \ chars. must escaped \\ in order recognized literals; characters in actual folder name require escaping (but don't in case).

    • if don't know folder name in advance, can safely escape [regex]::escape($foldername)

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -