3

Hey there i need to move a lot of files using a powershell script and i kinda need help on this one.

The root is as following

> tree .
\
├───Folder1
│   ├───Sub1
│   │   └───Stockfile
│   └───Sub2
└───Folder2
    ├───Sub1
    │   └───Stockfile
    └───Sub2

The script needs to find the stockfiles and need to move them to the Sub2 folder of each mainfolder.

I made the following script to show all the stockfiles in a list:

$var = get-childitem -Filter *stock* -recurse
foreach ($file in $var) {
write-host ($file.FullName)
}
2
  • 2
    Is the name of the Sub2 folder consistent? How do you 'know' the name of the destination folder? Is it explicitly named Sub2 always? Commented Nov 10, 2016 at 16:52
  • The folders aren't named Sub1 and Sub2. There are like 400 folders starting with TM and then 6 random numbers Commented Nov 11, 2016 at 7:26

3 Answers 3

2

Try the following:

Get-ChildItem -File -Recurse -Filter *_stock.csv |
  Move-Item -WhatIf -Destination { $_.FullName -replace '^(.*)\\Sub1\\','$1\Sub2\' }

The approach extracts the path prefix up to ...\Sub1 from the full path of each stock file and makes ...\Sub2 the destination (directory) path.

That way, stock files are moved from anywhere in the ...\Sub1 subtree directly into ...\Sub2.

Note:

  • -WhatIf shows you what would happen first; remove it to perform actual moving.

  • The assumption is that the stock file paths contain only one Sub1 component, and that there are no stock files with duplicate names in the Sub1 subtree.

  • -replace interprets its first operand as a regular expression, which is why the \ chars. must be escaped as \\ in order to be recognized as literals; characters in the actual folder name could also require escaping (but don't in this case).

    • If you don't know the folder name in advance, you can safely escape it with [regex]::Escape($folderName)
Sign up to request clarification or add additional context in comments.

5 Comments

Ok i removed the -WhatIf, because it seems to work. But without the -WhatIf statement i get the following message:
Move-Item : Could not find a part of the path. [Move-Item], DirectoryNotFoundException + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
Ok it works now, the next challange is to move files from folders starting with TM*. So the folder names are like TM123456, TM456789 and so on. In those folders are the 2 subfolders. How to move files from folders starting with TM?
I just realized it can be done without a loop or ForEach-Object altogether, using a ScriptBlock parameter - please see my update.
As for the follow-up question re TM* folders: please post it as a new question (and feel free to let me know here once you've done so).
2

Assume folders are all called sub1 and sub2.

$var = get-childitem -Filter *stock* -recurse
foreach ($file in $var) {
    $parent = Split-Path $file.fullname -parent
    Move-Item $file.FullName -destination $parent\..\sub2\1.txt
}

I know this is not pretty, but it works ;)

Comments

0

I made it work this way, thanks a lot.

    $var = get-childitem -Filter *_stock.csv -recurse
foreach ($file in $var)
 { Move-Item $file.FullName ($file.FullName -replace '^(.*)\\Sub1\\','$1\Sub2\') }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.