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!
Simple Extract File from Folder
Re: Simple Extract File from Folder
Can you post the python script here?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.
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 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
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.
I would do this: put print statements in your code that print the values of the variables.
Re: Simple Extract File from Folder
Also looks fine to me, you can add print() statements everywhere to detect what for example `walker` contains.
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate
Re: Simple Extract File from Folder
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)
Thanks again for looking @sander and @safihre!
Re: Simple Extract File from Folder
Nice!
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate
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?
Re: Simple Extract File from Folder
In the Scripts-folder, that you define in Config > Folders.
If you like our support, check our special newsserver deal or donate at: https://sabnzbd.org/donate