SPSS Tutorials

BASICS REGRESSION T-TEST ANOVA CORRELATION

Copy all Files from Subfolders to Main Folder

Question

"I'd like to work with some data files that are scattered over a number of (sub)folders. All file names are unique. How can I search all folders for .sav files and copy those into some root directory?"

SPSS Python Syntax Example

begin program.
rdir = 'd:/temp' #Please specify an 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. Find all .sav files in subfolders and copy them to root directory.

begin program.
rdir = 'd:/temp' #Please specify root directory.
from shutil import copyfile
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):
    if not os.path.exists(os.path.join(rdir,fil[fil.rfind('\\') + 1:])):
        copyfile(fil,os.path.join(rdir,fil[fil.rfind('\\') + 1:]))
end program.

Description

What if File Names aren't Unique?

"I can't simply copy 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

*1. Find all .sav files in subfolders and prefix and copy them to root directory.

begin program.
rdir = 'd:/temp' #Please specify root directory.
from shutil import copyfile
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):
    if os.path.exists(os.path.join(rdir,fil[fil.rfind('\\') + 1:])):
        os.rename(fil,os.path.join(rdir,str(cnt + 1).zfill(2) + '_' + fil[fil.rfind('\\') + 1:]))
    else:
        copyfile(fil,os.path.join(rdir,str(cnt + 1).zfill(2) + '_' + fil[fil.rfind('\\') + 1:]))
end program.

Previous tutorial: Move all Files from Subfolders to Main Folder

Next tutorial: Export Output to Different Folders

Let me know what you think!

*Required field. Your comment will show up after approval from a moderator.