2

Looking around the site and can't seem to find some answers for myself.

I'm looking to write a script, that will enable me to move files from one destination to another, based on the contents within the file.

To get into Specifics

Source Destination - V:\SW\FromSite Copy to Destination - V:\SW\ToSW FileType - .txt String - test

Ideally I'd also like to ONLY have the script search files that begin with 7. These are unique identifiers to a region.

Pulling my hair out a bit trying.

I was using the below, which runs without error, but does nothing.

$DestDir = "V:\SW\FromSite"
$SrcDir = "V:\SW\ToSW"
$SearchString = "test"

gci $SrcDir -filter 7*.txt | select-string $SearchString | select path | 
move-item -dest $DestDir -whatif

3 Answers 3

2

Here's what I would do, though I'm sure there's a more streamlined way to do it.

$files = gci $SrcDir -filter 7*.txt
$files | %{
    if ((select-string -path $_.FullName -pattern $SearchString) -ne $null) {
        move-item -path $_.FullName -dest $DestDir
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So put together the below, its running, but nothing seems to happen? (Sorry for formatting, this site wrecks my head when responding to comments, where return saves the comment instead of starting a new line $DestDir = "V:\SW\FromSite" $SrcDir = "V:\SW\ToSW" $SearchString = "test" $files = gci $SrcDir -filter 7*.txt $files | %{ if ((select-string -path $_.FullName -pattern $SearchString) -ne $null) { move-item -path $_.FullName -dest $DestDir } }
Try adding some more output. Perhaps write-host the $files, to make sure your filter is working, and maybe add a write-host line inside the IF statement.
1

So did some more messing around and the below is working perfectly for what I need

get-childitem "<SourceFolder>" -filter 7*.txt -
recurse | select-string -list -pattern "test" | move -dest "<DestinationFolder>"

Thanks all for the help

Comments

0

Not sure if I missed something (didn't tried it on my machine) - but as long as you pass the -whatif option, move-item "shows what would happen if the cmdlet runs. The cmdlet is not run."; see https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/move-item.

So probably it would have been sufficient to just remove the -whatif from the initial statement.

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.