简体   繁体   中英

Windows script to move files to folders based on names

I did a bit of research and I didn't find anything to suit/I can't alter any script.

I would like a script/link, to move a file/folder to a folder where the first characters match.

Like I want to move 'apple.txt' to the folder 'A' Same as I want to move the folder 'John' to the folder 'J'

Thanks for the help

I found your post while searching on another topic, but the following Powershell script does what you want. Place it in the parent directory containing your files. It will create the folder structure based on the first letter of the file name. It's case sensitive, so an 'a' is distinct from an 'A' in the file name.

$OrigFolder = ".\" $NewFolder = ".\_Sorted to Move" # Orphans folder, where files that return null in the regex match will be moved # Example: file "- title.pdf" # will be moved to ".\_Orphans" folder $Orphans = '_Orphans' # Use the underscore to sort the folder to the top of the window # First count the number of files in the $OrigFolder directory $numFiles = (Get-ChildItem -Path $OrigFolder).Count $i=0 # Tell the user what will happen clear-host; Write-Host 'This script will copy ' $numFiles ' files from ' $OrigFolder ' to _Sorted to Move' # Ask user to confirm the copy operation Read-host -prompt 'Press enter to start copying the files' # Regex to match filenames $Regex = [regex]"(?:([A-Za-z0-9]?)[A-Za-z0-9]?)" # Loop through the $OrigFolder directory, skipping folders Get-ChildItem -LiteralPath $OrigFolder | Where-Object {!$_.PsIsContainer} | ForEach-Object { if($_.BaseName -match $Regex){ $ChildPath = $_.BaseName -replace $Regex #Caluclate copy operation progress as a percentage [int]$percent = $i / $numFiles * 100 # If first part of the file name is empty, move it to the '_Orphans' folder if(!$Matches[1]){ $ChildPath = $Orphans} else { $ChildPath = $Matches[1] } # Generate new folder name $FolderName = Join-Path -Path $NewFolder -ChildPath ($ChildPath) # Create folder if it doesn't exist if(!(Test-Path -LiteralPath $FolderName -PathType Container)){ $null = New-Item -Path $FolderName -ItemType Directory} # Log progress to the screen Write-Host "$($_.FullName) -> $FolderName" # Move the file to the folder Move-Item -LiteralPath $_.FullName -Destination $FolderName # Tell the user how much has been moved Write-Progress -Activity "Copying ... ($percent %)" -status $_ -PercentComplete $percent -verbose $i++ } } Write-Host 'Total number of files in '$OrigFolder ' is ' $numFiles Write-Host 'Total number of files copied to '$NewFolder ' is ' $i Read-host -prompt "Press enter to complete..." clear-host;

Change the regular expression in line 22 to change the matching behavior.

Hope it works for you.

Also check out the program Directory Opus. It's a very mature, well-piolished product. It handles these kinds of tasks with ease. It's a little pricey for casual use but if you manage a lot of files on a daily basis it's a godsend. I don't know how I'd do without it. Lot's of support on their forums.

--Scott

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