1
votes

I've been working on a script in Powershell to get paths from a CSV file and move those files at the corresponding path to a new destination elsewhere. often with a different filename.

I am using Version 5.0

For example:

Source Destination : C:\1\2\3\File.pdf, D:\3\7\8\9\FILE1.pdf

Now I used the following script and it was initially able to move some of the files:

Import-CSV "R:\MoveFiles.csv" -Delimiter "," -ErrorAction Stop | ForEach-Object{Move-Item  -path $_.Source -Destination $_.Destination}

Although around half way through executing it started to return this error:

Move-Item : Could not find a part of the path. At line:1 char:238 + ... Each-Object{Move-Item -Literalpath $.Source -Destination $.Destina ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (Q:\RECORDS\PRIV...-4-20_N1969.pdf:FileInfo) [Move-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

As far as I can tell there are no special characters that would prevent the path being found. If I replace Move-Item for Copy-Item it returns the same error. I have also checked the paths to see if they are true or not.

I am at my wits end with this. Not sure what else to try. I am after all a complete novice.

Thank you

NB: I worked out a solution to this issue. It would appear that the Move-Item cmdlet does not like creating directories.

Instead I made the directories first with New-Item -directories, getting the content from a text document where every line represented a path (no headers).

After creating empty directories first the original script worked as intended.

For anyone interested here is the directories script:

 #CREATE DIRECTORIES FROM CSV

cd 

$name = Get-Content ".\Create_New_Directories\Move_Directories_Test.txt"
Foreach ($_ in $name)
{
    New-Item -Force -verbose -path $_ -Type Directory
} 

Out-File ".\Create_New_Directories\Newoutput.txt"

Thank you everyone for your help.

2
Post the line of the CSV that fails, please.Tomalak
@Tomalak Every row of the CSV fails, there are 13,050 of them. I am executing off a work computer on a shared network is there any chance that is the reason it won't move?DrDro
Oh, I thought you said "around half way through it started"Tomalak

2 Answers

0
votes

To debug such cases, consider Move-Item's -WhatIf parameter. Like so,

... | ForEach-Object{Move-Item -whatif  -path $_.Source -Destination $_.Destination}

This will print the intended operation, so you can double-check paths for any sheenigans.

What if: Performing the operation "Move File" on target "Item: C:\Temp\SomeFile.xml Destination: C:\Temp\Somewhere\SomeFile.xml".

0
votes

Not sure. But your error message indicates it's a write error of DirectoryNotFound. So perhaps you should be making sure you have the perms on the target side and are not exceeding any character limits in the length of the path.

Some other things to consider/try:

Your CSV file should be in the format (the first line must be the headers):

Source,Destination
C:\1\2\3\SomeFile.pdf,D:\1\2\3\SomeFile.pdf
C:\1\2\3\SomeFile2.pdf,D:\1\2\3\SomeFile2.pdf

Also you are not santizing your input so if you made the CSV file in Excel you might have leading or trailing spaces. In that case either clean the file editing in Notepad or try $_.Source.trim() and $_.Destination.trim()

And like the other guy said the -whatif switch is useful and so is -verbose.

You might also try Move-Item -Force and/or opening powershell as an Administrator.

Good Luck! ;-)