Link to home
Create AccountLog in
Powershell

Powershell

--

Questions

--

Followers

Top Experts

Avatar of Sander Stad
Sander Stad🇳🇱

Move-Item cmdlet doesn't force the move
I have been puzzling with this problem for too long and I hope someone can help. I created a script that at a certain moment copies a directory to another directory.

The values of the directories are ok, the rights on the directories are ok. I added the code of the entire function.
The $oldDir and $newDir contain the right values at execution of the Move-Item cmdlet but the force command doesn't work.
Powershell isn't case-sensitive but I even tried to change the -Force to - force but as I expected that doesn't solve the problem.

This is the error I receive:
VERBOSE: Performing operation "Move Directory" on Target "Item: E:\TEST\Display\ZORG\6300159 Destination:
E:\TEST\Display\ZORG\630\6300159".
Move-Item : Cannot create a file when that file already exists.
At E:\TEST\ImageConversion1.1.ps1:433 char:16
+                         Move-Item  <<<< $oldDir $newDir -Force -Verbose
Function Reorganise-Folders($s, $d){
	If($s.length -and $d.length -gt 0){
		For($i = 0; $i -lt $s.length; $i++){
			If((Check-Directories $s[$i]) -ne 0){ exit 0}
			If((Check-Directories $d[$i]) -ne 0){ exit 0}
		
			$arraySourcedirs = Get-ChildItem $s[$i] | Where { $_.attributes -eq "Directory" }
			
			If($arraySourcedirs.length -gt 0){
				Foreach($item in $arraySourcedirs){
					# Converteren van arrayitem naar string
					[string] $dir = $item
					
					If($dir.length -gt 3){
 
						$newName = $dir.Substring(0,3);
						
 
						$oldDir = $s[$i] + "\" + $dir
						$newDir = $d[$i] + "\" + $newName
						
 
						If(!(Test-Path $newDir)){
							# Als deze niet bestaat maak het nieuwe pad aan
							New-Item $newDir -type directory
						}
						
# THIS IS THE PROBLEM AREA
						Move-Item $oldDir $newDir -Force
					}
				}
			}
		}
	}
}

Open in new window

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Joe KlimisJoe Klimis🇬🇧

Hi
I am not sure why you are breaking the folder down into seperate items , see if the following fiunction would for work for you.
function moveFolder 
{
param( 
    [string] $from = $(throw "SourceFolder  required."),
    [string] $to = $(throw "Destination Folder required.")
    ) 
move-item $From $to -force
}
# the following will move all files and folders from folder t1  create folder t2 and put them all in it 
#########################################
# usage
#movefolder c:\test1\t1\ c:\test1\t2\ 
#########################################
movefolder c:\t1\ c:\test1\t1\

Open in new window


Avatar of Joe KlimisJoe Klimis🇬🇧

I think yours may work if you add a tailing "\" on the end of each folder path

Avatar of Sander StadSander Stad🇳🇱

ASKER

I've tried to add the trailing slashes but that doesn't work either.
The thing I find strange is that the force command doesn't do anything because in  every article it should instruct Powershell to ignore the errors and overwrite the file or directory.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Avatar of Sander StadSander Stad🇳🇱

ASKER

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account
Powershell

Powershell

--

Questions

--

Followers

Top Experts

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.