A
A
Albert Zinkevich2020-12-20 08:26:00
PowerShell
Albert Zinkevich, 2020-12-20 08:26:00

Transferring files to year\month folders?

Hello. I am an admin, I need help from programmers, I can’t create a script on powershell / bat, it turns out to be nonsense.
Essence of the question:
All files from folder A (for example) should be moved to folder B (for example). In folder B, a folder should be automatically created by the date the file was created with the year (2020, for example), or in the already created folder of the year, folders of months (01, 02, etc.) should be created, or files should be transferred to already created folders of months by the date of creation . If there is a similarity in the file name, add _1, _2, etc. If possible, please tell me the same conditions, but folders and movements occur not by the date of creation, but by the date the file was modified

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alhaos, 2020-12-21
@demon_alik

param (
    [System.IO.DirectoryInfo]$sourseDirectory = "C:\Program Files\Far Manager",
    [System.IO.DirectoryInfo]$destinationDirectory = "D:\tmp",
    [int]$useCreationOrModifiedFileDateTimeSwitch = 0 # [0] (Use Creation DateTime) or [1] (Use Modified DateTime)
)

enum CreationOrModifiedFileDateTimeSwitch{
    UseCreationDateTime = 0
    UseModifiedDateTime = 1
}

class FileCopyer{
    hidden [System.IO.DirectoryInfo]$sourseDirectory
    hidden [System.IO.DirectoryInfo]$destinationDirectory
    hidden [CreationOrModifiedFileDateTimeSwitch]$useCreationOrModifiedFileDateTimeSwitch

    FileCopyer([System.IO.DirectoryInfo]$sourseDirectory, [System.IO.DirectoryInfo]$destinationDirectory, [CreationOrModifiedFileDateTimeSwitch]$useCreationOrModifiedFileDateTimeSwitch){
        $this.sourseDirectory = $sourseDirectory
        $this.destinationDirectory = $destinationDirectory
        $this.useCreationOrModifiedFileDateTimeSwitch = $useCreationOrModifiedFileDateTimeSwitch
    }

    [void] CopyFiles() {
        Get-ChildItem -Path $this.sourseDirectory -File | ForEach-Object {
            if ($useCreationOrModifiedFileDateTimeSwitch -eq [CreationOrModifiedFileDateTimeSwitch]::UseModifiedDateTime){
                $year = [int]($_.LastWriteTime.ToString('yyyy'))
                $month = [int]($_.LastWriteTime.ToString('MM'))
            } else {
                $year = [int]($_.CreationTime.ToString('yyyy'))
                $month = [int]($_.CreationTime.ToString('MM'))
            }
            $pathWithYear = Join-Path $this.destinationDirectory -ChildPath $year
            if (!(Test-Path $pathWithYear)) {New-Item $pathWithYear -ItemType Directory}
            $pathWithYearMonth = Join-Path $pathWithYear -ChildPath $month
            if (!(Test-Path $pathWithYearMonth)) {New-Item $pathWithYearMonth -ItemType Directory}
            Copy-Item $_.FullName -Destination $pathWithYearMonth
        }
    }   
}

$FileMoverInstance = [FileCopyer]::New($sourseDirectory, $destinationDirectory, $useCreationOrModifiedFileDateTimeSwitch)
$FileMoverInstance.CopyFiles()

M
MaxKozlov, 2020-12-20
@MaxKozlov

The information you get with the Get-Item, Get-ChildItem commands is 'these are FileInfo objects.
https://docs.microsoft.com/en-us/dotnet/api/system...
It has CreationTime, LastWriteTime properties, which are DateTime objects, they in turn have Year, Month, Day properties
. compose strings
Copy Copy-Item, check for existence of Test-Path
Without your code, it will not be possible to say more because it will already be a task. But even here you can find something close by searching.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question