## Simple Extract File from Folder

Come up with a useful post-processing script? Share it here!
noja
Newbie
Posts: 3
Joined: January 12th, 2018, 8:34 am

### Simple Extract File from Folder

Hello,

I'm having a hell of a time figuring out how to write a script that takes a downloaded folder with usually one file in it and then extracts that file and deletes the leftover folder.

SAB is on Windows and I've tried a couple versions with a batch file and with python. I managed to get one in python that does exactly what I want when using in a test folder, but when I use it through SAB it plays reallllly funky and tries to delete folders out of the SAB install.

So I'm back to square one and I'm not doing well. It seems like this should be simple but I can't seem to find anything that seems close.

Use case is that I have my category pulling in the folder that contains one folder with one file in it (never the same name), then I want that file extracted and moved to a watch folder which imports that file automatically (but the program can't handle folders - hence my problem).

Thanks for any help!

sander
Release Testers
Posts: 6831
Joined: January 22nd, 2008, 2:22 pm

### Re: Simple Extract File from Folder

noja wrote:
January 12th, 2018, 10:13 am
SAB is on Windows and I've tried a couple versions with a batch file and with python. I managed to get one in python that does exactly what I want when using in a test folder, but when I use it through SAB it plays reallllly funky and tries to delete folders out of the SAB install.
Can you post the python script here?

noja
Newbie
Posts: 3
Joined: January 12th, 2018, 8:34 am

### Re: Simple Extract File from Folder

Code: Select all

import shutil
import os

# copy all the files in the subfolders to main folder

# The current working directory
dest_dir = os.environ['SAB_COMPLETE_DIR']
# The generator that walks over the folder tree
walker = os.walk(dest_dir)

# the first walk would be the same main directory
# which if processed, is
# redundant
# and raises shutil.Error
# as the file already exists

rem_dirs = walker.next()[1]

for data in walker:
for files in data[2]:
try:
shutil.move(data[0] + os.sep + files, dest_dir)
except:
shutil.Error
# still to be on the safe side
continue

# clearing the directories
# from whom we have just removed the files
for dirs in rem_dirs:
shutil.rmtree(dest_dir + os.sep + dirs)

# Success code
sys.exit(0)
I'm sure I've got something in there that's pooching it. Since it can't delete folders in the SAB config folder it just fails with this error - WindowsError: [Error 5] Access is denied: 'C:\\Program Files\\SABnzbd\\email\\badfetch-en.tmpl'

I know that means I have more problems than just what I'm trying to do, but c'est la vie

sander
Release Testers
Posts: 6831
Joined: January 22nd, 2008, 2:22 pm

### Re: Simple Extract File from Folder

I had a glance at it, but I don't see the problem right away.

I would do this: put print statements in your code that print the values of the variables.

safihre
Posts: 3507
Joined: April 30th, 2015, 7:35 am
Location: Switzerland
Contact:

### Re: Simple Extract File from Folder

Also looks fine to me, you can add print() statements everywhere to detect what for example walker contains.

noja
Newbie
Posts: 3
Joined: January 12th, 2018, 8:34 am

### Re: Simple Extract File from Folder

safihre wrote:
January 15th, 2018, 2:37 am
Also looks fine to me, you can add print() statements everywhere to detect what for example walker contains.
So I finally managed to get it figured out.

I tried putting print statements all over and making sure they were done correctly, but SAB kept throwing syntax errors at me.

So I went back to the start of where I found the script (essentially exactly what's in the code below, minus the os.chdir)

Then it tried to tell me again that I was trying to delete files in my SAB config directory. Finally it hit me once I dove into the individual python commands - os.getcwd() meant that the script was trying to operate in the very specific SAB directory and not in the destination folder I wanted it to.

So it was a quick jump to find a way to cd into that directory.

Here is my working script for anyone that is wanting the same thing:

Code: Select all

import shutil
import os

# change to the directory I want to use

# copy all the files in the subfolders to main folder

# The current working directory
dest_dir = os.getcwd()
# The generator that walks over the folder tree
walker = os.walk(dest_dir)

# the first walk would be the same main directory
# which if processed, is
# redundant
# and raises shutil.Error
# as the file already exists

rem_dirs = walker.next()[1]

for data in walker:
for files in data[2]:
try:
shutil.move(data[0] + os.sep + files, dest_dir)
except:
shutil.Error
# still to be on the safe side
continue

# clearing the directories
# from whom we have just removed the files
for dirs in rem_dirs:
shutil.rmtree(dest_dir + os.sep + dirs)


Additionally, if someone is new to python like me, in the folder path, make sure to include the double '\\' if using Windows.

Thanks again for looking @sander and @safihre!

safihre
Posts: 3507
Joined: April 30th, 2015, 7:35 am
Location: Switzerland
Contact:

### Re: Simple Extract File from Folder

Nice!

gess24
Newbie
Posts: 1
Joined: January 5th, 2019, 1:41 pm

### Re: Simple Extract File from Folder

Hi looking at the script I dont want to sound daft but were are you putting the script in a directory or in the sorting feild?

safihre