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.
foreachin powershell which would iterate each file one by one and before iterating you can arrange them in order however you like to.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-WhatIfswitch 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.