Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts

    [SOLVED][POWERSHELL] Move-Item and Subfolders

    Hi guys!

    I've got this easy problem. Suppose you've got two Folders F1 and F2, both containing the same file F.ext.
    I want to move both copies of the file F.ext in the folder F3. So i thought a script like this: foreach (F.ext in Folders) | move-item -Destination F3 (only to give an idea XD)
    The problem is this: when the script move the first copy the second can't be moved because the name is the same.
    The best solution I've found is to move from a situation like F3>F.ext to F3>F1>F.ext &F3>F2>F.ext but i don't know how to write it in a script.
    I don't mean to move the entire folders to C3 but rather create two subfolders with the same name as the source folder of files

    Thanks

    A
    Last edited by Albiz; 2013-11-28 at 09:18. Reason: [SOLVED]

  2. #2
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    Did you solve this? If so, could you post the solution so that if others run across your post they will also have the solution. If you haven't solved this, let me know; I've dealt with a similar issue in one of the scripts I wrote to manage my mp3 files.

  3. #3
    New Lounger
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Here the code:
    Code:
    $Move=@() 
    $Et=@("0*","1*","8*","9*","Scansioni")
    $Today = Get-Date -Format "dd-MMM"
    $Folder= "C:\Estinto\$Today\EmptyFolders"
    $Search= "C:\Test"
    
    if(!(Test-Path $Folder)){
        New-Item -Path $Folder -ItemType directory
    }
    
    Get-ChildItem $Search -Recurse | 
    Where-Object {(Get-ChildItem -Path $_.FullName) -eq $null -and $_.PSISContainer} | 
    foreach($_){ 
        $Sav=Split-Path($_) -leaf 
        if($Sav -match "^[0-1-8-9].*"){"Can't Move Boss"}
        elseif($Sav -match "Scansion"){"Can't Move Boss"}
        else{
            $Mem=$_
            $Move=$Move+$Mem.fullname 
            $Divide=$Mem.Parent.fullname -split('')
            $Impera=""
            foreach($count in $divide){
                if($count -match ':'){
                    $count="$" 
                }
                $Impera += $count
            }
            $Dest=$Folder,$impera -join('\')
            if(!(Test-Path $Dest)){
                New-Item -Path $Dest -ItemType directory
            }
            
       
            Move-Item -Path $Mem.FullName -Destination $Dest -force 
        }
    }
    
    $Move= $Move | sort{Split-Path($_) -leaf}
    
    $drives = foreach ($path in $Move) {
        $prophash = @{
            Nome= split-path($path) -leaf
            Percorso = $path
        }
    
        #create a new object from the property hash
        New-Object PSObject -Property $prophash
        }
    
    #embed the style in the html header
    $head = @'
    <Title>Deleting Empty Folder Report</Title>
    <style>
    body 
    { 
     background-color:#FFFFFF;
     font-family:Verdana;
     font-size:12pt; 
    }
    td, th 
    { 
     border:1px solid blue; 
     border-collapse:collapse; 
    }
    th 
    {
     color:white;
     background-color:green; 
    }
    table, tr, td, th { padding: 5px; margin: 0px }
    table { margin-left:50px; }
    </style>
    '@
    
    #create an xml document from the HTML fragment
    [xml]$html = $drives | ConvertTo-Html -fragment
    
    
    #check each row, skipping the TH header row
    for ($i=1;$i -le $html.table.tr.count-1;$i++) {
      $class = $html.CreateAttribute("class")
    }
    
    #create the final report from the innerxml which should be html code
    $body = @"
    <H1>Deleting Empty Folder Report for $Search</H1>
    $($html.innerxml)
    "@
    
    #put it all together
    $Log = "Deleting Empty" + $Today
    
    
    ConvertTo-HTML -head $head  -PostContent "<br><i>$(Get-date)</i> Folders will be moved to $Folder" -body $body | 
    Out-File "C:\Log\$Log.html" -Encoding ascii
    
    Invoke-Item "C:\Log\$Log.html"
    Have a good day!
    A

  4. #4
    New Lounger
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The short answer is to use the "-force" parameter for move-item. That will move even if the target already exists.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •