Home > Software design >  Powershell - Using ROBOCOPY to iterate the file paths and open this files in new folder
Powershell - Using ROBOCOPY to iterate the file paths and open this files in new folder

Time:05-04

I am trying to iterate the file path in the notepad (New_Test_File.txt) and by using the script I will open all these 5 files in a new file called "file_2"

My "New_Test_File.txt" is -

enter image description here

My Expectation Output is -

My Expectation Output is

My script is -

 $paths = Get-Content -Path C:\Users\JamesAlam\Desktop\New_Test_File.txt 
 foreach($path in $paths) {
 Write-Host $path
 ROBOCOPY /ZB /XO /R:3 /W:10 $path C:\Users\JamesAlam\Desktop\File_2
 }

I am a newbie in PowerShell and I am not sure how to use the ROBOCOPY method to copy these file paths and open them in the new folder. Please help me with this. Thanks in advance

CodePudding user response:

For each robocopy action you should have, in your case - "source", "destination", and "file" (optional - arguments).

So... You will need to specify the source folder as the directory only, and the file name.

Try to run this, it should work:

$paths = Get-Content -Path C:\Users\JamesAlam\Desktop\New_Test_File.txt

 foreach($path in $paths) {
    $directory = [System.IO.Path]::GetDirectoryName($path)
    $fileName = Split-Path $path -leaf
 
 robocopy $directory C:\Users\JamesAlam\Desktop\File_2 $fileName
 }
  • Related