Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Move-item error after copying

$
0
0

Here’s the bit of relevant code, and the error: http://imgur.com/a/LEtb2

## Get a listing of the files in the Copy folder and move them to the appropriate subfolder $copyListing = Get-ChildItem Win213copy $count = 0 foreach ($dir_File in $copyListing) { if ($dir_File -like "*lab*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Labs -Force $count++ } if ($dir_File -like "*lecture*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Lectures -Force $count++ } if ($dir_File -like "*assignment*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Assignments -Force $count++ } if ($dir_File -like "*script*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Scripts -Force $count++ } } 

Error: Move-Item : Cannot find path 'C:UsersMishaDocumentsWin213mkhoussid_Lab4_LogFile.log' because it does not exist.

I have a couple of questions, which Ill ask in one post, instead of clogging up main /r/powershell page. Ill also post the whole script at the very bottom of this post, for reference.

First, why am I getting this error? I think the logic is correct, but I may be overlooking some crucial element.

Second, why did I need to add a $count variable? Im going by my lab, and it doesnt really explain much about it, just says to create one.

And lastly, would a SWITCH statement be a better option? How would I go about typing out the SWITCH statement? I havent learned that type yet, but would love to get ahead.

Thanks! Here’s the whole script:

cls ## Create 2 command line variables $workingDirectory = $ARGS[0] $directoryName = $ARGS[1] ## Check if variables are empty and get user input if necessary if("$workingDirectory" -eq "") { Write-Warning "Parameter Required" $workingDirectory = Read-Host } if("$directoryName" -eq "") { Write-Warning "Parameter Required" $directoryName = Read-Host } ## Test to see if PWD is equal to WorkingDirectory and if not move the user if ("$pwd" -ne "$workingDirectory") { Write-Host "You are not in the $workingDirectory. Do you wish to move? Press CTRL + C to exit or" pause } cd $workingDirectory ## Test if directory exists in the working directory if ( Test-Path $directoryName) {Write-Host "Directory $directoryName exists"} else {Write-Host "Directory $directoryName does not exist, would you like to create it?" pause New-Item -Path $directoryName -ItemType directory } if ( (gci $directoryName).count -ne 0) {Write-Host "Folder $directoryname is not empty, and will be renamed. Press CTRL + C or" pause Rename-Item $directoryName "Win213x.OLD" } ## Create variables for subfolders $sub = @("Lectures", "Labs", "Assignments", "Scripts") ## Loop through collection to create subfolders foreach ($item in $sub) {New-Item -ItemType directory -Name $item -Force} ## Search Documents folder recursively for files only and save them to a new directory called Win213Copy New-Item -path $homedocumentswin213 -ItemType Directory -Name Win213copy -Force $files = Get-ChildItem -Recurse -file $homedocuments $files | Copy-Item -destination "C:UsersMishaDocumentsWin213Win213copy" -Force ## Get a listing of the files in the Copy folder and move them to the appropriate subfolder $copyListing = Get-ChildItem Win213copy $count = 0 foreach ($dir_File in $copyListing) { if ($dir_File -like "*lab*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Labs -Force $count++ } if ($dir_File -like "*lecture*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Lectures -Force $count++ } if ($dir_File -like "*assignment*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Assignments -Force $count++ } if ($dir_File -like "*script*") { Move-Item $dir_File -Destination C:UsersMishaDocumentsWin213Scripts -Force $count++ } } 

submitted by /u/chy_vak
[link] [comments]

The post Move-item error after copying appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles