Easy way to make files 0 KB with a button?

I've brought up needing to make empty files before by generating them on the second side of the Lister but this has caused so much confusion of which files go where and it has in general wasted so much time and I'm not sure if all the files are in the right spot at this point. Sorry for the long sentence :confused:

Is there an easy way to make a button that can do this? Maybe a button that copies the selected files names, deletes the files, then recreates them with the names it saved?

Which do you want to do, create the empty files over the top of the selected files? Or create empty files in the destination with the same names as the selected files in the source? Or something else?

What about folders, or files in folders, if they are selected?

I'm trying to replace the selected files in the current folder with empty ones with the same names. I'm doing this so i can get rid of the files without certain programs regenerating them. I only need to replace files in the current folder and not subfolders. If it this is possible and it doesn't require any extra time or work it would also be useful if the files could be marked with the hidden and system attributes.

Here's a quick and easy way to do it:

Truncate Files.dcf (1.3 KB)

To use the .dcf file, see the Button .dcf Files section of How to use buttons and scripts from this forum

Note that this has no undo. It truncates the files in-place, so you cannot undo it as the data is overwritten. A more complex script could send the old files to the recycle bin and then create new files where they were.

Script code that is in the .dcf file, for reference:

function OnClick(clickData)
{
	clickData.func.command.deselect = false;

	var fileCount = clickData.func.sourcetab.selected_files.count;
	if (fileCount == 0) return;
	var choice = clickData.func.Dlg.Request("Really truncate " + fileCount + " file" + (fileCount>1?"s":"") + "?\n\nThere is no undo!", "Truncate|Cancel", "Truncate Files");
	if (choice == 0) return;

	var tab = clickData.func.sourcetab
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext())
	{
		var file = eSel.item().Open("wt", tab);
		file.Close();		
	}
}

Oops, I missed that part. One extra line to add that:

Truncate Files.dcf (1.4 KB)

function OnClick(clickData)
{
	clickData.func.command.deselect = false;

	var fileCount = clickData.func.sourcetab.selected_files.count;
	if (fileCount == 0) return;
	var choice = clickData.func.Dlg.Request("Really truncate " + fileCount + " file" + (fileCount>1?"s":"") + "?\n\nThere is no undo!", "Truncate|Cancel", "Truncate Files");
	if (choice == 0) return;

	var tab = clickData.func.sourcetab
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext())
	{
		var file = eSel.item().Open("wt", tab);
		file.SetAttr("hs");
		file.Close();		
	}
}

Thank you :smiley:
It works great :slight_smile:
I don't think I've ever accidentally deleted a file so no way to undo shouldn't be a problem :slight_smile:

Improving the last version so that it does not set the HS attributes if it fails to clear the file (e.g. because the file is already read-only):

Truncate Files.dcf (1.5 KB)

function OnClick(clickData)
{
	clickData.func.command.deselect = false;

	var fileCount = clickData.func.sourcetab.selected_files.count;
	if (fileCount == 0) return;
	var choice = clickData.func.Dlg.Request("Really truncate " + fileCount + " file" + (fileCount>1?"s":"") + "?\n\nThere is no undo!", "Truncate|Cancel", "Truncate Files");
	if (choice == 0) return;

	var tab = clickData.func.sourcetab
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext())
	{
		var file = eSel.item().Open("wt", tab);
		if (file.error == 0) {
			file.SetAttr("hs");
			file.Close();		
		}
	}
}

Could I get a version of this without the popup? I wouldn't click the button accidentally and I use it quite a lot so it would make things a lot easier.

Just remove these two lines:

	var choice = clickData.func.Dlg.Request("Really truncate " + fileCount + " file" + (fileCount>1?"s":"") + "?\n\nThere is no undo!", "Truncate|Cancel", "Truncate Files");
	if (choice == 0) return;