开发者

Moving files by starting letter in powershell, python or other scripting language running windows

I need a script than can recursively traverse c:\somedir\ an开发者_如何学编程d move files to c:\someotherdir\x\ - where x is the starting letter of the file.

Can anyone help?


Ended up with this one:

import os
from shutil import copy2
import uuid
import random

SOURCE = ".\\pictures\\"
DEST = ".\\pictures_ordered\\"

for path, dirs, files in os.walk(SOURCE):
    for f in files:
        print(f)
        starting_letter = f[0].upper()
        source_path = os.path.join(path, f)
        dest_path = os.path.join(DEST, starting_letter)

        if not os.path.isdir(dest_path):
            os.makedirs(dest_path)

        dest_fullfile = os.path.join(dest_path, f)

        if os.path.exists(dest_fullfile):            
            periodIndex = source_path.rfind(".")
            renamed_soruce_path = source_path[:periodIndex] + "_" + str(random.randint(100000, 999999))  + source_path[periodIndex:]
            os.rename(source_path, renamed_soruce_path)
            copy2(renamed_soruce_path, dest_path)            
            os.remove(renamed_soruce_path)
        else:
            copy2(source_path, dest_path)
            os.remove(source_path)`


Here's a simple script that does what you want. It doesn't tell you anything about what it's doing, and will just overwrite the old file if there are two files with the same name.

import os
from shutil import copy2

SOURCE = "c:\\source\\"

DEST = "c:\\dest\\"

# Iterate recursively through all files and folders under the source directory
for path, dirs, files in os.walk(SOURCE):
    # For each directory iterate over the files
    for f in files:
        # Grab the first letter of the filename
        starting_letter = f[0].upper()
        # Construct the full path of the current source file
        source_path = os.path.join(path, f)
        # Construct the destination path using the first letter of the
        # filename as the folder
        dest_path = os.path.join(DEST, starting_letter)
        # Create the destination folder if it doesn't exist
        if not os.path.isdir(dest_path):
            os.makedirs(dest_path)
        # Copy the file to the destination path + starting_letter
        copy2(source_path, dest_path)


I suspect this will work in PowerShell.

gci -path c:\somedir -filter * -recurse |
    where { -not ($_.PSIsContainer) } |
    foreach { move-item -path $_.FullName -destination $_.Substring(0, 1) }


ls c:\somedir\* -recurse | ? { -not ($_.PSIsContainer)} | mv -destination "C:\someotherdir\$($_.Name.substring(0,1))" } ... -whatif :P


Here's an answer in Python, note that warning message, you may want to deal with overwrites differently. Also save this to a file in the root directory and run it there, otherwise you have to change the argument to os.walk and also how the paths are joined together.

import os
import sys

try:
    letter = sys.argv[1]
except IndexError:
    print 'Specify a starting letter'
    sys.exit(1)

try:
    os.makedirs(letter)
except OSError:
    pass # already exists

for dirpath, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        if filename.startswith(letter):
            src = os.path.join(dirpath, filename)
            dst = os.path.join(letter, filename)
            if os.path.exists(dst):
                print 'warning, existing', dst, 'being overwritten'
            os.rename(src, dst)


sure, I'll help: look at os.path.walk in Python2, which I believe is simply os.walk in Python3.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜