cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
lala
Level VII

How to delete all files in a folder?

A folder contains no folders but multiple files.
How can I delete these files using JSL?


Because this folder is occupied by other levels, cannot be achieved by deleting the folder.

rd1=Delete Directory(dir);rc1=Create Directory(dir);

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: How to delete all files in a folder?

If you want to just delete the files, here is a modification on @jthi code to do that

Names Default To Here( 1 );

dir_path = "$TEMP\test\";

If( !Directory Exists( dir_path ),
	Create Directory( dir_path );
	new_file = Save Text File(
		dir_path || "DeleteMe.txt",
		"The quick brown fox"
	);
	new_file = Save Text File(
		dir_path || "DeleteMe2.txt",
		"The quick brown fox"
	);
);

Wait( 1 );

// Get the files in the folder
the_Files = Files In Directory( dir_path );

For Each( {file}, the_files, rc = Delete File( dir_path || file ) );

Show( Files In Directory( dir_path ) );
Jim

View solution in original post

3 REPLIES 3
jthi
Super User

Re: How to delete all files in a folder?

For me the directory gets deleted even if it has files. Maybe some of your files are still being used?

Names Default To Here(1);

dir_path = "$TEMP\test\";

If(!Directory Exists(dir_path),
	Create Directory(dir_path);
	new_file = Save Text File(dir_path || "DeleteMe.txt", "The quick brown fox");
	new_file = Save Text File(dir_path || "DeleteMe2.txt", "The quick brown fox");
);

wait(1);
Show(Directory Exists(dir_path));
Show(Files In Directory(dir_path));
Show(File Exists(new_file));

Delete Directory(dir_path);

wait(1);

Show(File Exists(new_file));
Show(Directory Exists(dir_path));

-Jarmo
txnelson
Super User

Re: How to delete all files in a folder?

If you want to just delete the files, here is a modification on @jthi code to do that

Names Default To Here( 1 );

dir_path = "$TEMP\test\";

If( !Directory Exists( dir_path ),
	Create Directory( dir_path );
	new_file = Save Text File(
		dir_path || "DeleteMe.txt",
		"The quick brown fox"
	);
	new_file = Save Text File(
		dir_path || "DeleteMe2.txt",
		"The quick brown fox"
	);
);

Wait( 1 );

// Get the files in the folder
the_Files = Files In Directory( dir_path );

For Each( {file}, the_files, rc = Delete File( dir_path || file ) );

Show( Files In Directory( dir_path ) );
Jim
lala
Level VII

Re: How to delete all files in a folder?

Thanks!

 

I can't use Windows 7 anymore.

Be sure to use JMP 16.

Powerful For Each()

For Each( {file}, the_files, rc = Delete File( dir_path || file ) );