Grouping Files by Metadata
Use Copilot to intelligently organize files in a folder based on their metadata.
Prerequisites
Install JBang to run these examples:
# macOS (using Homebrew)
brew install jbangdev/tap/jbang
# Linux/macOS (using curl)
curl -Ls https://sh.jbang.dev | bash -s - app setup
# Windows (using Scoop)
scoop install jbang
Example scenario
You have a folder with many files and want to organize them into subfolders based on metadata like file type, creation date, size, or other attributes. Copilot can analyze the files and suggest or execute a grouping strategy.
Example code
Usage:
# Use with a specific folder (recommended)
jbang ManagingLocalFiles.java /path/to/your/folder
# Or run without arguments to use a safe default (temp directory)
jbang ManagingLocalFiles.java
Code:
//DEPS io.github.copilot-community-sdk:copilot-sdk:1.0.9
import com.github.copilot.sdk.*;
import com.github.copilot.sdk.events.*;
import com.github.copilot.sdk.json.*;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
public class ManagingLocalFiles {
public static void main(String[] args) throws Exception {
try (var client = new CopilotClient()) {
client.start().get();
// Create session
var session = client.createSession(
new SessionConfig().setModel("gpt-5")).get();
// Set up event handlers
var done = new CountDownLatch(1);
session.on(AssistantMessageEvent.class, msg ->
System.out.println("\nCopilot: " + msg.getData().getContent())
);
session.on(ToolExecutionStartEvent.class, evt ->
System.out.println(" → Running: " + evt.getData().getToolName())
);
session.on(ToolExecutionCompleteEvent.class, evt ->
System.out.println(" ✓ Completed: " + evt.getData().getToolCallId())
);
session.on(SessionIdleEvent.class, evt -> done.countDown());
// Ask Copilot to organize files - using a safe example folder
// For real use, replace with your target folder
String targetFolder = args.length > 0 ? args[0] :
System.getProperty("java.io.tmpdir") + "/example-files";
String prompt = String.format("""
Analyze the files in "%s" and show how you would organize them into subfolders.
1. First, list all files and their metadata
2. Preview grouping by file extension
3. Suggest appropriate subfolders (e.g., "images", "documents", "videos")
IMPORTANT: DO NOT move any files. Only show the plan.
""", targetFolder);
session.send(new MessageOptions().setPrompt(prompt));
// Wait for completion
done.await();
session.close();
}
}
}
Grouping strategies
By file extension
// Groups files like:
// images/ -> .jpg, .png, .gif
// documents/ -> .pdf, .docx, .txt
// videos/ -> .mp4, .avi, .mov
By creation date
// Groups files like:
// 2024-01/ -> files created in January 2024
// 2024-02/ -> files created in February 2024
By file size
// Groups files like:
// tiny-under-1kb/
// small-under-1mb/
// medium-under-100mb/
// large-over-100mb/
Dry-run mode
For safety, you can ask Copilot to only preview changes:
String prompt = String.format("""
Analyze files in "%s" and show me how you would organize them
by file type. DO NOT move any files - just show me the plan.
""", targetFolder);
session.send(new MessageOptions().setPrompt(prompt));
Custom grouping with AI analysis
Let Copilot determine the best grouping based on file content:
String prompt = String.format("""
Look at the files in "%s" and suggest a logical organization.
Consider:
- File names and what they might contain
- File types and their typical uses
- Date patterns that might indicate projects or events
Propose folder names that are descriptive and useful.
""", targetFolder);
session.send(new MessageOptions().setPrompt(prompt));
Interactive file organization
//DEPS io.github.copilot-community-sdk:copilot-sdk:1.0.9
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class InteractiveFileOrganizer {
public static void main(String[] args) throws Exception {
try (var client = new CopilotClient();
var reader = new BufferedReader(new InputStreamReader(System.in))) {
client.start().get();
var session = client.createSession(
new SessionConfig().setModel("gpt-5")).get();
session.on(AssistantMessageEvent.class, msg ->
System.out.println("\nCopilot: " + msg.getData().getContent())
);
System.out.print("Enter folder path to organize: ");
String folderPath = reader.readLine();
String initialPrompt = String.format("""
Analyze the files in "%s" and suggest an organization strategy.
Wait for my confirmation before making any changes.
""", folderPath);
session.send(new MessageOptions().setPrompt(initialPrompt));
// Interactive loop
System.out.println("\nEnter commands (or 'exit' to quit):");
String line;
while ((line = reader.readLine()) != null) {
if (line.equalsIgnoreCase("exit")) {
break;
}
session.send(new MessageOptions().setPrompt(line));
}
session.close();
}
}
}
Safety considerations
- Confirm before moving: Ask Copilot to confirm before executing moves
- Handle duplicates: Consider what happens if a file with the same name exists
- Preserve originals: Consider copying instead of moving for important files
- Test with dry-run: Always test with a dry-run first to preview the changes
