3

I have a text file that contains a list of files in different directories, e.g

  • C:\FolderOne\Testing.jpg
  • C:\FolderTwo\Test.jpg

I'd like to turn those names into folders. And if two files exist in one folder, e.g FolderOne, then both those files should go into the same folder.

I understand that the backslashes will also have to be replaced by underscores and the colon replaced by an underscore too.

Thus far, I'm able to get the contents from the file list and copy it into a specified folder. I would like to create a folder for each file in the file list, but the folder name needs to include C_FolderOne and C_FolderTwo and so on....

Any help would be appreciated!

What I have so far:

Param (
    [String]$model
)

# Debug line
$model = "DEV2";

[String]$drive = "C:";

#Go to the PG_dump directory
CD "C:\Program Files\PostgreSQL\9.4\bin"

##Execute PG_dump with no password
.\pg_dump.exe --host localhost --port 5432 --username "postgres" --role "postgres" --no-password  --format custom --blobs --verbose --file     "C:\Test\$date-DEV-$X.backup" "DEV"

#Get the content from the file list
$file_list = Get-Content "$drive\Testing\DEV33.txt" 
foreach ($file in $file_list)
{    
    Copy-Item  $file -Destination "C:\Testing"
}
10
  • 1
    Your question isn't clear. What exactly are you trying to do? Commented Nov 15, 2017 at 14:14
  • Could the paths being created under the "work" folder (e.g. - "C:\Testing") contain more than one segment (e.g. - C:\Folder A\Folder A - Sub Folder 0\...\filename.jpg")? Commented Nov 15, 2017 at 14:34
  • 1
    @TheIncorrigible1 Basically, trying to create a folder for each file in the file list. The folder created should be named with the path to the folder e.g if a file called "Winter.jpg" is located at "C:\TestFolder\Winter.jpg" then a folder called C_TestFolder should be created. Commented Nov 15, 2017 at 15:22
  • @codemaker Hello, yes the paths being created under the "work" folder can contain more than one segment. Commented Nov 15, 2017 at 15:24
  • (Split-Path -Path $Path -Parent) -replace ':\\','_' @TheManBehindTheMan working out the logic to capture the rest of the `\\` Commented Nov 15, 2017 at 15:27

1 Answer 1

4

Here's a method to do what you're asking. It'll grab the parent path for the file and create the destination folder.

## Get the content from the file list.
foreach ($file in Get-Content -Path "$drive\Testing\DEV33.txt") {
    $name = (Split-Path -Path $file -Parent) -replace '\\', '_' -replace ':'
    New-Item -Path "path\to\$name" -ItemType Directory
}
Sign up to request clarification or add additional context in comments.

4 Comments

I owe you chicken wings my friend or whatever you like lol. With some tweaking, it does exactly what i wanted it to do! Thanks very much.
@TheManBehindTheMan Great! Kindly upvote/mark as answer. What tweaks were necessary?
I had to add in an if statement so that if a folder doesn't exist that's when it should be created. And i have upvoted you, however, as i have a reputation less than 15 as i've only joined, i'am shown a message that says it has been recorded but will not change the publicly displayed score. Kind Regards.
@TheManBehindTheMan If you add -Force to New-Item, it'll automatically create the folder structure.

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.