Page 1 of 1

Move-Script for pdf (Newspaper)

Posted: March 5th, 2013, 5:13 am
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!

Re: Move-Script for pdf (Newspaper)

Posted: March 6th, 2013, 4:13 am
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_))

Re: Move-Script for pdf (Newspaper)

Posted: March 6th, 2013, 2:31 pm
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.

Re: Move-Script for pdf (Newspaper)

Posted: March 6th, 2013, 2:35 pm
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?

Re: Move-Script for pdf (Newspaper)

Posted: March 7th, 2013, 3:37 am
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.

Re: Move-Script for pdf (Newspaper)

Posted: March 7th, 2013, 1:28 pm
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"