1

I have an arrangement that has 3 lines, they are file paths to copy them:

  • Configurations/config1.json
  • Configurations/config2.json
  • Configurations/config3.json

The instruction I use to copy the files is the following:

Foreach ($i in $changes) { Copy-Item $i "C:\file\" -Recurse -Container }

The files have been copied successfully. My question is how to automatically create the directory Configurations/ inside of file/ and files inside of Configurations/.

1 Answer 1

1

You could use to New-Item to create the directories/files

$changes | foreach-object {
    # remove file name from path
    $pathToDir = $_.Substring($_.Length - ($_ -split "/")[-1].Length)
    
    New-Item -ItemType Directory -Force -Path $pathToDir
    
    # I'm not sure if you are trying to create empty files to copy from or if the files 
    # you are creating are copies, but below is where your would create/copy the files, if 
    # you clarify your question, I can update the code below to something relevant
    
    # your code
    Copy-Item $_ "C:\file\" -Recurse -Container
}

Sign up to request clarification or add additional context in comments.

2 Comments

I have tested the code, as a result it has copied 2 files into the directory when they have to be 3. I don't know what change I should make so that all 3 can be copied into the same directory. Thanks for the collaboration.
That seems like a weird problem, would you mind updating the question with the code you currently have.

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.