简体   繁体   中英

Using powershell to move/make folders/subfolders based on filename

I don't really have much experience in powershell but I have files that I need to organize. The files are all pdf and will have a format similar to "Dept123_Name_year.pdf".

I want to move the documents into a folder based on "Dept123" and a sub folder "Name". If the folder is yet to exist I would like it to create the folder/subfolder.

To make it easier I was thinking of using creating an "Organize" folder on the desktop and running the program on that. If you think it'd be easier some other way, let me know.

Thanks in advance.

You can use a regular expression to match the different components of the filenames, then generate a directory structure based on that. The -Force parameter of mkdir lets you ignore whether or not the directory already exists:

$list = ls
for ($i=0; $i -le $list.Length; $i++) {
    if ($list[$i].Name -match '([A-Za-z0-9]+)_([A-Za-z]+)_.*\.pdf') {
        $path = Join-Path $matches[1] $matches[2]
        mkdir -Force -Path $path
        cp $list[$i] "$path\."
    }
}

The regular expression part is in the quotes; you might need to modify it to suit your specific needs. Note that the sections in round brackets correspond to the different parts of the name being extracted; these parts are loaded, in order, into the $matches variable, which is generated automatically. Eg '([A-Za-z0-9]+)\\.txt' will match any text file with only letters or numbers in the name, and will stick the actual name - minus the extension - into $matches[1] .

Using regex and full-form Powershell:

# using ?<name> within a () block in regex causes powershell to 'name' the property 
# with the given name within the automatic variable, $matches, object.
$Pattern = "(?<Dept>.*)_(?<Name>.*)_(?<Year>.*)\.pdf"

# Get a list of all items in the current location.  The location should be set using
# set-location, or specified to the command by adding -Path $location
$list = Get-ChildItem

# Foreach-Object loop based on the list of files
foreach ($file in $list) {
    # send $true/$false results from -matches operation to $null
    $File.Name -matches $Pattern 2> $Null

    # build destination path from the results of the regex match operation above
    $Destination = Join-Path $matches.Dept $matches.Name

    # Create the destination if it does not exist
    if (!(Test-Path $Destination) ) { 
        New-Item -ItemType Directory -Path $destination 
    }

    # Copy the file, keeping only the year part of the name
    Copy-Item $file "$destination\$($matches.year)"+".pdf"
}

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