Page 1 of 1

Simple Extract File from Folder

Posted: January 12th, 2018, 10:13 am
by noja
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!

Re: Simple Extract File from Folder

Posted: January 12th, 2018, 11:01 am
by sander
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?

Re: Simple Extract File from Folder

Posted: January 12th, 2018, 11:18 am
by noja

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

Re: Simple Extract File from Folder

Posted: January 13th, 2018, 4:55 am
by sander
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.

Re: Simple Extract File from Folder

Posted: January 15th, 2018, 2:37 am
by safihre
Also looks fine to me, you can add print() statements everywhere to detect what for example `walker` contains.

Re: Simple Extract File from Folder

Posted: January 15th, 2018, 9:24 am
by noja
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

os.chdir('C:\\path\\to\\my\\download\\folder')
 
# 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!

Re: Simple Extract File from Folder

Posted: January 16th, 2018, 3:53 pm
by safihre
Nice!

Re: Simple Extract File from Folder

Posted: January 5th, 2019, 1:45 pm
by gess24
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?

Re: Simple Extract File from Folder

Posted: January 6th, 2019, 4:48 am
by safihre
In the Scripts-folder, that you define in Config > Folders.