Move-Script for pdf (Newspaper)

Come up with a useful post-processing script? Share it here!
Post Reply
crush89
Newbie
Newbie
Posts: 3
Joined: February 25th, 2013, 3:22 am

Move-Script for pdf (Newspaper)

Post by crush89 »

Hi

I need your help. I'm a complete Bash-script noob.

I want a script that moves file to a different folder, only pdf files.

For example:

SAb downloads a file and extract it to folder /volume1/Downloads. After the extraction it looks like this: /volume1/Downloads/NAMEofNewspaper/NAMEandDATE/newspaper.pdf

The script should move the pdf in a different folder like /volume1/Newspapers

Additional: It would be awesome if the script could automatically delete all the other stuff including the empty folders and rename the pdf file after the folder it was in. For example the pdf file above should be rename into NAMEandDATE.pdf.

Please help me!
crush89
Newbie
Newbie
Posts: 3
Joined: February 25th, 2013, 3:22 am

Re: Move-Script for pdf (Newspaper)

Post by crush89 »

I tryed a lot and i was successfull with the movement-part, but is still can't figure out how to rename it proberly. I'm still hoping for your help.

Here is my Python script to move files (recursiv) with specific suffix;

Code: Select all

import os, shutil, sys

srcDir = '/volume1/web/Library/' 
dstDir = '/volume1/dropbox/Kindle/'
extension = '.pdf'

print "Source Dir: ", srcDir, "n", "Destination Dir: ",dstDir, "n", "Extension: ", extension

for root, dirs, files in os.walk(srcDir):
    for file_ in files:
        if file_.endswith(extension):
            shutil.copy(os.path.join(root, file_), os.path.join(dstDir, file_))
User avatar
sander
Release Testers
Release Testers
Posts: 9263
Joined: January 22nd, 2008, 2:22 pm

Re: Move-Script for pdf (Newspaper)

Post by sander »

Cool! You've written your own script, and in python. Bravo. Oh: the script actually works within SAB?

If so, I will help you with the renaming:

Input: /volume1/Downloads/NAMEofNewspaper/NAMEandDATE/newspaper.pdf
Output: NAMEandDATE.pdf.

Correct? If so, I'll write it for you.
User avatar
sander
Release Testers
Release Testers
Posts: 9263
Joined: January 22nd, 2008, 2:22 pm

Re: Move-Script for pdf (Newspaper)

Post by sander »

Here it is:

Code: Select all

name ='/volume1/Downloads/NAMEofNewspaper/NAMEandDATE/newspaper.pdf'
print name
dirname = name.split('/')[-2]
newfilename = dirname + '.pdf'
print newfilename
Result:

Code: Select all

$ python rename.py 

/volume1/Downloads/NAMEofNewspaper/NAMEandDATE/newspaper.pdf
NAMEandDATE.pdf
Does this help?
crush89
Newbie
Newbie
Posts: 3
Joined: February 25th, 2013, 3:22 am

Re: Move-Script for pdf (Newspaper)

Post by crush89 »

thank you very much, this helps me a lot, but i still have one question. This path

Code: Select all

/volume1/Downloads/NAMEofNewspaper/NAMEandDATE/newspaper.pdf
is variable, because i download different newspapers, so is is possible to use something like this to channge the path of the file for every new file?

Code: Select all

name = Variable Path to pdf file
print name
dirname = name.split('/')[-2]
newfilename = dirname + '.pdf'
print newfilename
I tryed this: "os.path.dirname(os.path.realpath(__file__))" but this uses the path of my Sabnzbd installfolder.

Maybe i should mention that i use a synolgy nas and python 2.7.3.
User avatar
sander
Release Testers
Release Testers
Posts: 9263
Joined: January 22nd, 2008, 2:22 pm

Re: Move-Script for pdf (Newspaper)

Post by sander »

I don't understand what you don't understand.
I don't understand the purpose of the second piece of code you gave
I don't understand the purpose of the line "os.path.dirname"
Post Reply