Question
"I'd like to work with a number of .sav files but they are scattered over different folders. All file names are unique. Is there any easy way to search through a number of folders for .sav files and move these into some root directory?"
SPSS Python Syntax Example
*1. Create random test folders and files.
begin program.
rdir = 'd:/temp' # Specify (empty) test folder.
import spss
for cnt,sdir in enumerate(['','f1','f2','f1/f1_1','f1/f1_2','f1/f1_2/f1_2_1']):
tdir = os.path.join(rdir,sdir)
if not os.path.exists(tdir):
os.mkdir(tdir)
spss.Submit('data list free/id.\nbegin data\n1\nend data.\nsav out "%s".'%(tdir + '/file_' + str(cnt) + '.sav'))
spss.Submit('new fil.')
end program.
*2. Move all .sav files from subfolders into root directory.
begin program.
rdir = 'd:/temp' # Specify root directory to be searched for .sav files.
filelist = []
for tree,fol,fils in os.walk(rdir):
filelist.extend([os.path.join(tree,fil) for fil in fils if fil.endswith('.sav')])
for fil in filelist:
os.rename(fil,os.path.join(rdir,fil[fil.rfind('\\') + 1:]))
end program.
begin program.
rdir = 'd:/temp' # Specify (empty) test folder.
import spss
for cnt,sdir in enumerate(['','f1','f2','f1/f1_1','f1/f1_2','f1/f1_2/f1_2_1']):
tdir = os.path.join(rdir,sdir)
if not os.path.exists(tdir):
os.mkdir(tdir)
spss.Submit('data list free/id.\nbegin data\n1\nend data.\nsav out "%s".'%(tdir + '/file_' + str(cnt) + '.sav'))
spss.Submit('new fil.')
end program.
*2. Move all .sav files from subfolders into root directory.
begin program.
rdir = 'd:/temp' # Specify root directory to be searched for .sav files.
filelist = []
for tree,fol,fils in os.walk(rdir):
filelist.extend([os.path.join(tree,fil) for fil in fils if fil.endswith('.sav')])
for fil in filelist:
os.rename(fil,os.path.join(rdir,fil[fil.rfind('\\') + 1:]))
end program.
Description
- Note this syntax uses Python so you need to have the SPSS Python Essentials installed in order to run it.
- The first program block will create some random test folders and files in some root directory called "rdir".
- The actual solution in the second program block will search all subfolders for any .sav files and move them directly into the root directory. The result is that all .sav files will be located in a single folder.
What if File Names aren't Unique?
"I can't simply move all files into a single folder because their file names are not unique. I can't have two files with identical names in a single folder. In order to solve this, I'd like to assign unique prefixes to all filenames. How can I do that?"
SPSS Python Syntax Example
begin program.
rdir = 'd:/temp' #Please specify root directory to be searched for .sav files.
filelist = []
for tree,fol,fils in os.walk(rdir):
filelist.extend([os.path.join(tree,fil) for fil in fils if fil.endswith('.sav')])
for cnt,fil in enumerate(filelist):
os.rename(fil,os.path.join(rdir,str(cnt + 1).zfill(2) + '_' + fil[fil.rfind('\\') + 1:]))
end program.
rdir = 'd:/temp' #Please specify root directory to be searched for .sav files.
filelist = []
for tree,fol,fils in os.walk(rdir):
filelist.extend([os.path.join(tree,fil) for fil in fils if fil.endswith('.sav')])
for cnt,fil in enumerate(filelist):
os.rename(fil,os.path.join(rdir,str(cnt + 1).zfill(2) + '_' + fil[fil.rfind('\\') + 1:]))
end program.
Delete Everything in Root Directory Except Data Files
"The .sav files were the only thing I needed from the root directory. Is there an easy way to delete everything else?"
SPSS Python Syntax Example
*1. Optionally: delete everything in root directory except .sav files.
begin program.
rdir = 'd:/temp' # Specify root directory.
import shutil
for tree in [path for path in os.listdir(rdir) if not path.endswith('.sav')]:
try:
shutil.rmtree(os.path.join(rdir,tree))
except:
os.remove(os.path.join(rdir,tree))
end program.
begin program.
rdir = 'd:/temp' # Specify root directory.
import shutil
for tree in [path for path in os.listdir(rdir) if not path.endswith('.sav')]:
try:
shutil.rmtree(os.path.join(rdir,tree))
except:
os.remove(os.path.join(rdir,tree))
end program.