testSHFileOperation.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from win32com.shell import shell, shellcon
  2. import win32api
  3. import os
  4. def testSHFileOperation(file_cnt):
  5. temp_dir=os.environ['temp']
  6. orig_fnames=[win32api.GetTempFileName(temp_dir,'sfo')[0] for x in range(file_cnt)]
  7. new_fnames=[os.path.join(temp_dir,'copy of '+os.path.split(orig_fnames[x])[1]) for x in range(file_cnt)]
  8. pFrom='\0'.join(orig_fnames)
  9. pTo='\0'.join(new_fnames)
  10. shell.SHFileOperation((0, shellcon.FO_MOVE, pFrom, pTo, shellcon.FOF_MULTIDESTFILES|shellcon.FOF_NOCONFIRMATION))
  11. for fname in orig_fnames:
  12. assert not os.path.isfile(fname)
  13. for fname in new_fnames:
  14. assert os.path.isfile(fname)
  15. shell.SHFileOperation((0, shellcon.FO_DELETE, fname, None, shellcon.FOF_NOCONFIRMATION|shellcon.FOF_NOERRORUI))
  16. def testSHNAMEMAPPINGS(file_cnt):
  17. ## attemps to move a set of files to names that already exist, and generated filenames should be returned
  18. ## as a sequence of 2-tuples created from SHNAMEMAPPINGS handle
  19. temp_dir=os.environ['temp']
  20. orig_fnames=[win32api.GetTempFileName(temp_dir,'sfo')[0] for x in range(file_cnt)]
  21. new_fnames=[win32api.GetTempFileName(temp_dir,'sfo')[0] for x in range(file_cnt)]
  22. pFrom='\0'.join(orig_fnames)
  23. pTo='\0'.join(new_fnames)
  24. rc, banyaborted, NameMappings=shell.SHFileOperation((0, shellcon.FO_MOVE, pFrom, pTo,
  25. shellcon.FOF_MULTIDESTFILES|shellcon.FOF_NOCONFIRMATION|shellcon.FOF_RENAMEONCOLLISION|shellcon.FOF_WANTMAPPINGHANDLE))
  26. for old_fname, new_fname in NameMappings:
  27. print('Old:',old_fname, 'New:', new_fname)
  28. assert len(NameMappings)==file_cnt
  29. testSHFileOperation(10)
  30. testSHFileOperation(1)
  31. testSHNAMEMAPPINGS(5)