0

Let me first say I am very bad at everything that involves coding language. So I need some help.

The goal:

I have a source with million of files but they need to be copied in a sequential order from A to B.

  • The source looks like this.

\\shareA\Files

\\shareA\Files\file1.jpg

\\shareA\Files\file2.jpg

\\shareA\Files\1\file3.jpg

\\shareA\Files\1\file4.jpg

\\shareA\Files\1\412\file5.jpg

\\shareA\Files\1\412\file6.jpg

  • The destination should look like this. So folder structure must remain the same.

\\ShareB\Files

\\ShareB\Files\file1.jpg

\\ShareB\Files\file2.jpg

\\ShareB\Files\1\file3.jpg

\\ShareB\Files\1\file4.jpg

\\ShareB\Files\1\412\file5.jpg

\\ShareB\Files\1\412\file6.jpg

The .txt file looks like this:

file1.jpg

file2.jpg

1\file3.jpg

1\file4.jpg

1\412\file5.jpg

1\412\file6.jpg

Relative Paths are included in the .txt file.

This my PowerShell script:

`

# Enter the source folder
$Source = "c:\FileA\Files"

# Enter the archiving destination folder
$destination = "C:\FileB\Files"

# Enter the tape export txt file
$txtPath = "C:\Tools\Files.txt"

# Read the file paths from the text file
$filePaths = Get-Content $txtPath

# Copy the files from the text file to the destination folder
foreach ($file in $filePaths) {
   Copy-Item "$Source\$file" -Destination "$destination" -Recurse -Verbose -PassThru
}

`

    Directory: C:\FileB\Files


> Mode                 LastWriteTime         Length Name                                                                                                                                                                                                          
> 
> VERBOSE: Performing the operation "Copy File" on target "Item: C:\FileA\Files\file1.jpg Destination: C:\FileB\Files\file1.jpg".
> -a----        2022-12-09   6:16 PM              0 file1.jpg                                                                                                                                                                                                     
> VERBOSE: Performing the operation "Copy File" on target "Item: C:\FileA\Files\file2.jpg Destination: C:\FileB\Files\file2.jpg".
> -a----        2022-12-09   6:16 PM              0 file2.jpg                                                                                                                                                                                                     
> VERBOSE: Performing the operation "Copy File" on target "Item: C:\FileA\Files\1\file3.jpg Destination: C:\FileB\Files\file3.jpg".
> -a----        2022-12-09   6:16 PM              0 file3.jpg                                                                                                                                                                                                     
> VERBOSE: Performing the operation "Copy File" on target "Item: C:\FileA\Files\1\file4.jpg Destination: C:\FileB\Files\file4.jpg".
> -a----        2022-12-09   6:16 PM              0 file4.jpg                                                                                                                                                                                                     
> VERBOSE: Performing the operation "Copy File" on target "Item: C:\FileA\Files\1\412\file5.jpg Destination: C:\FileB\Files\file5.jpg".
> -a----        2022-12-09   6:16 PM              0 file5.jpg                                                                                                                                                                                                     
> VERBOSE: Performing the operation "Copy File" on target "Item: C:\FileA\Files\1\412\file6.jpg Destination: C:\FileB\Files\file6.jpg".
> ```
> 

As you can see, the files are all copied in the root folder "\\C:\FileB\Files" and not in the respective subfolders in the destination.

Thanks in advance for the help.

2
  • Welcome to SO. Please post your piece of effort whatever you have tried so far. Also go through How to Ask. To begin with you can start looking for foreach in powershell which would iterate each file one by one and before iterating you can arrange them in order however you like to. Commented Dec 9, 2022 at 18:20
  • As for this Let me first say I am very bad at everything that involves coding language. So I need some help.. That's fine and we've all been there. Yet, computers/software will only do what they are told to, and that is what your code is doing. Back-up, take it one step at a time, and make sure you are getting the results you'd expect before the next step. Also, get in the habit of using the -WhatIf switch to protect yourself and see what will happen before you actually execute your script. Rea the source and destination from the file, don't use those static variables. Commented Dec 10, 2022 at 10:18

1 Answer 1

0

Make sure to create the parent directory for each destination file, if not already exists. Also specify the full file path for both the -Path and the -Destination argument of Copy-Item. The Copy-Item command knows nothing about the directory structure when copying single files, so you need to be explicit.

foreach ($file in $filePaths) {
   # using Join-Path is more robust than "$Source\$file" as it appends "\" only if necessary
   $sourceFilePath = Join-Path $Source $file

   # create the full destination file path
   $destinationFilePath = Join-Path $Destination $file
   
   # get the parent directory of the destination file path...
   $destinationDirPath = Split-Path $destinationFilePath
   # ...and create it if it doesn't exist (-Force)
   $null = New-Item $destinationDirPath -ItemType Directory -Force
   
   # Copy file with full source and destination path
   Copy-Item $sourceFilePath -Destination $destinationFilePath -Verbose -PassThru
}
Sign up to request clarification or add additional context in comments.

2 Comments

This makes sense when I see and read the code. It's understandable when you write down the solution to my problem but it's just so frustrating why my brain cannot come up with the solution itself. I am going to try some other use cases to get a hang on these basic things. Thanks for the answer!
@suckateverything We all started like this. Some things in PowerShell appear to be easy at first glance, but are actually more complicated (sometimes more complicated than it should be). Just keep on it, read examples and most importantly try out stuff by yourself. To master PowerShell it takes some effort, but it pays off in the long run!

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.