ServiceNow技术问询:如何修改Mid Server中导出文件的文件名格式
Hey there! Let's sort out this file naming issue for your ServiceNow Mid Server export. I’ve got two reliable approaches to get your files into that datetime_file_name format your client needs—either renaming after export or setting up the export to use the right format straight away.
Approach 1: Rename the File After Export
If you can’t adjust the export logic directly, renaming post-export is a straightforward fallback. You can run this script on the Mid Server (either via a scheduled script execution tied to your export job or as part of the export completion workflow):
// Replace these values with your actual export details var originalFileName = "incident_list_20240520143000"; // Example original file name var exportDirectory = "C:/ServiceNow/MidServer/agent/work/export_files/"; // Mid Server export folder // Split the original name to extract components (handles file names with underscores) var nameSegments = originalFileName.split("_"); var dateTimeSegment = nameSegments.pop(); // Pulls the datetime suffix var baseFileName = nameSegments.join("_"); // Reconstructs the original file name (if it had underscores) // Build the new file name var newFileName = dateTimeSegment + "_" + baseFileName; // Use Java's File class to handle the rename (Mid Server supports Java calls) var originalFile = new java.io.File(exportDirectory + originalFileName); var targetFile = new java.io.File(exportDirectory + newFileName); if (originalFile.exists()) { var renameSuccess = originalFile.renameTo(targetFile); if (renameSuccess) { gs.info("Successfully renamed file to: " + newFileName); } else { gs.error("Failed to rename file—check Mid Server permissions for the export directory."); } } else { gs.error("Original export file not found at: " + exportDirectory + originalFileName); }
Key Notes for This Approach:
- Handle underscores: The script uses
pop()andjoin()to safely split names even if your base file name has underscores (e.g.,employee_onboarding_list_20240520becomes20240520_employee_onboarding_list). - Permissions: Make sure the Mid Server service account has read/write access to the export directory—without this, the rename will fail.
Approach 2: Configure Export to Use the Desired Format Directly
This is the cleaner, more efficient option since it avoids an extra rename step. How you set this up depends on how you’re running the export:
For Scheduled Exports (Export Sets)
- Navigate to System Export Sets > Scheduled Exports in your ServiceNow instance.
- Open your existing scheduled export task.
- In the File name field, replace the default format (usually
${table_name}_${datetime}) with${datetime}_${table_name}. - If you need a custom datetime format (e.g.,
YYYY-MM-DD_HH-MM-SSinstead of the default), use a script to generate the file name before export:// Generate a custom formatted datetime var currentDT = new GlideDateTime(); // Example: Convert to YYYYMMDDHHMMSS (no separators) var formattedDT = currentDT.getDisplayValueInternal().replace(/[-:\s]/g, ""); // Set the export file name exportFile.setName(formattedDT + "_" + "incident_list"); // Replace with your base file name
For Mid Server Scripted Exports (ECC Queue)
If you’re triggering exports via the ECC Queue (common for custom integrations), specify the desired file name directly when creating the queue record:
var eccQueueRec = new GlideRecord("ecc_queue"); eccQueueRec.initialize(); eccQueueRec.setValue("agent", "mid.server.your_mid_server_name"); // Replace with your Mid Server name eccQueueRec.setValue("topic", "custom_export"); // Match your export topic // Generate formatted datetime var currentDT = new GlideDateTime(); var formattedDT = currentDT.getDisplayValueInternal().replace(/[-:\s]/g, ""); // Set the final file name here eccQueueRec.setValue("name", formattedDT + "_" + "employee_data"); // Replace with your base file name // Add other export parameters (e.g., table name, filters) as needed eccQueueRec.insert();
Key Notes for This Approach:
- Datetime consistency: Double-check that the datetime format matches what the client’s other system expects—adjust the regex or use
GlideDateTimemethods likeformat()to tweak the output. - Test first: Validate the export in a non-production environment to ensure the file name is generated correctly and the file is accessible to the target system.
内容的提问来源于stack exchange,提问作者Frank Silveira

