简体   繁体   中英

How to backup these files into specific folders using powershell

I've finally have given up googling and come here out of desperation. Go easy on me I'm fairly new to Powershell.

So, the objective of the code below was to first look through the source folder, then read through each .zip file and move to the directory specified by the value in the hashtable. Unfortunately, this is not how they want it to work anymore.

Now I need to retain the parent folder from source: for example "DAL" and then create the proceeding folders based on the file names and finally move each .zip to its file specified folder. Also, it needs to go through each folder under source which will be at least 20 other folders with a unique 3 character names.

 $srcRoot = "C:\Cloud\source\dal"
    $dstRoot = "C:\Cloud\Destination"

    ##$map = @{}; dir -recurse | ? { !$_.psiscontainer} | % { ##$map.add($_.name,$_.PSChildName) }

# DAT and DEV will have to be excluded from folder creation 
    $map = {
    #AEODDAT_201901 = "AEOD\2019\01"
    #AEOMDEV_201902 = "AEOM\2019\01"
    #AEOYDAT_201902 = "AEOY\2019\01"
    }

    $fileList = Get-ChildItem -Path $srcRoot -Filter "*.zip*" -File -Force -Recurse
    foreach ($file in $fileList)
    {
        #Go through each file up to mapped string
        $key = $file.BaseName.Substring(0,14)
        if ($key -in $map.Keys)
        {
            $fileName = $file.Name
            $dstDir = Join-Path -Path $dstRoot -ChildPath $map[$key]
            #create direcotory if not in path
            if (-not (Test-Path -Path $dstDir))
            {
                mkdir -Path $dstDir
            }
            Write-Verbose "Moving $($file.FullName)"
            if (Test-Path -Path (Join-Path -Path $dstDir -ChildPath $fileName))
            {
            #Write error if name exists
                Write-Error -Message "File $fileName already exists at $dstDir"
            #move path
            } else {
                Move-Item -Path $($file.FullName) -Destination $dstDir 
            }
        }
    }

So C:\\Cloud\\source\\DAL\\AEODDAT20190101.zip should create folders in C:\\Cloud\\Destination\\DAL\\AEOD\\2019\\01\\AEODDAT20190101.zip would be my desired output.

Welcome, Matt! (no pun intended) One of the habits I have in similar situations with destination folders is to Set-Location $dstRoot and create folders from the relative path. You can execute New-Item with the relative path and the syntax is simpler. For example, your If statement could look like this and it would work the same way (with a slightly different error message):

if ($key -in $map.Keys){
  Set-Location $dstRoot
  New-Item -ItemType Directory $map[$key] -ErrorAction Ignore #won't raise an error if it exists
  Write-Verbose "Moving $($file.FullName)"
  #this will raise an error if the file already exists, unless you specify -Force
  Move-Item "$($file.FullName)" $map[$key]
}

EDIT: Found 2 issues.

  1. $map is a Hashtable literal that should be preceded with @ :
    $map = @{ AEODDAT20190101 = "AEOD\\2019\\01"

  2. You were missing the last character of the base file name by taking only the first 14 characters. AEODDAT2019010 didn't match AEODDAT20190101. This should fix it: $key = $file.BaseName.Substring(0,15)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM