Author Topic: Create an empty file  (Read 37926 times)

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Create an empty file
« on: September 06, 2017, 15:43:51 »
This scripts creates an empty file (if provided name is not already a file) and defaults to .txt if extension is not typed. Selects the file after that.

Code: [Select]
// Ask user for file name
@var $answer = AskText("Enter file name", "New file.txt", 0);

// If user canceled, abort
if ( $answer == 0 )
{
break;
}

// Full path name
@var $file_name = GetSourcePath() ^ $answer;

// If answer doesn't have any extension, default to .txt
@var $ext_size = StrLen( PathGetFileExtPart($file_name) );
if ($ext_size == 0)
{
$file_name = $file_name + ".txt";
}

// File exists?
if (FileExists($file_name) > 0)
{
MessageBox("Error", "File already exists", 0);
}
else
{
// Create empty file
SaveStringToFile($file_name, "", 0);

// It's not possible to select the created file because the script doesn't recognize new items until the script ends. Only when it finishes is that new items are available and refreshed.
// But found a way using native CTRL+V feature
SetClipboardText($file_name);
    MC.Explorer.Selection.UnselectAll
    MC.RunCmd ID="Core.1312"
}

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4400
    • View Profile
    • Multi Commander
Re: Create an empty file
« Reply #1 on: September 08, 2017, 13:49:18 »
You can type "cf <filename>"  (without ") in the commandline field to create a file.. 

cf myfile.txt

and the 0byte file named myfile.txt is created in the panel that are in focus

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Re: Create an empty file
« Reply #2 on: September 09, 2017, 21:58:00 »
Good point. Thanks

pncdaspropagandas

  • Contributor
  • Active Member
  • *****
  • Posts: 93
    • View Profile
Re: Create an empty file
« Reply #3 on: September 09, 2017, 22:35:22 »
I added the possibility to create multiple empty files at once, just like folder creation where you write folders separated by semi-colon:

Code: [Select]
@var $n;
@var $file;
@var $file_name;

// Ask user for file name
@var $answer = AskText("Enter file name", "New file.txt", 0);

// If user canceled, abort
if ( $answer == 0 )
{
break;
}

@var $arr_files = StrTokenize2Array($answer, ";");
for( $n=0; $n<arrayCount($arr_files); $n++ )
{
$file = $arr_files[$n];

// Full path name
$file_name = GetSourcePath() ^ $file;

// If answer doesn't have any extension, default to .txt
@var $ext_size = StrLen( PathGetFileExtPart($file_name) );
if ($ext_size == 0)
{
$file_name = $file_name + ".txt";
}

// File exists?
if (FileExists($file_name) > 0)
{
MessageBox("Error", "File already exists. Stopping script.", 0);
break;
}
else
{
// Create empty file
SaveStringToFile($file_name, "", 0);
}
}

// Select created file
if( arrayCount($arr_files) == 1 )
{
// It's not possible to select the created file because the script doesn't recognize new items until the script ends. Only when it finishes is that new items are available and refreshed.
// But found a way using native CTRL+V feature
SetClipboardText($file_name);
MC.Explorer.Selection.UnselectAll
MC.RunCmd ID="Core.1312"
}