Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created June 18, 2011 00:57
Show Gist options
  • Select an option

  • Save jonalter/1032686 to your computer and use it in GitHub Desktop.

Select an option

Save jonalter/1032686 to your computer and use it in GitHub Desktop.
Android: delete all files from the sdcard that match a regex
var win = Ti.UI.createWindow({
backgroundColor: '#fff',
layout: 'vertical'
});
var createButton = Ti.UI.createButton({
title: "create files",
top: 20,
height: 50,
width: 250,
});
createButton.addEventListener('click', function(e){
if (Titanium.Filesystem.isExternalStoragePresent()) {
var sdcardDir = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory).getParent();
for (var i = 12 - 1; i >= 0; i--){
var newFile = Titanium.Filesystem.getFile(sdcardDir.nativePath,'newfile'+i+'.txt').write('test');
Ti.API.info('Created: '+ newFile);
};
alert('files created');
}else{
alert('no sdcard present');
};
});
var deleteButton = Ti.UI.createButton({
title: "delete files",
top: 20,
height: 50,
width: 250,
});
deleteButton.addEventListener('click', function(e){
if (Titanium.Filesystem.isExternalStoragePresent()) {
var sdcardDir = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory).getParent();
var files = sdcardDir.getDirectoryListing();
Ti.API.info('ALL FILES '+files);
var qty = files.length;
i=0;
while (i < qty)
{
Ti.API.info('ROW '+files[i]);
if(files[i].match(/newfile*/)){
var thefile = Titanium.Filesystem.getFile(sdcardDir.nativePath,files[i]);
thefile.deleteFile();
Ti.API.info('Deleted '+files[i]);
}
i++;
}
alert('files deleted');
}else{
alert('no sdcard present');
};
});
win.add(createButton);
win.add(deleteButton);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment