how to convert CSV file to TXT file in mid server folder
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:44 AM
Hello Team,
I have a requirement for converting the CSV file exported from the export set copied to mid server path after which we want to convert the CSV file to TXT file.
Thanks in advance.
Regards
- Labels:
-
Architect
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2023 02:51 PM
Hi Chandan,
To convert an Export Set CSV file to a TXT file in ServiceNow and then export it to a mid server location, you can use a scheduled job with a script. Here's an example/reference script that you can use:
Create a new scheduled job in your ServiceNow instance and name it "ConvertExportSetCSVToTXT".
Set the "Run" field to "Manually".
Paste the following code into the script field:
// Set the path to the Export Set CSV file
var csvFile = "/path/to/export_set_csv_file.csv";
// Set the path to the output TXT file
var txtFile = "/path/to/txt_file.txt";
// Read the Export Set CSV file
var csvData = new GlideSysAttachment().read(csvFile);
// Convert the CSV data to TXT data
var txtData = csvData.replace(/,/g, "\t");
// Write the TXT data to a new file
new GlideSysAttachment().write(txtFile, txtData);
// Export the TXT file to the mid server
var midServer = gs.getProperty("mid.server.name");
var exportPath = "/path/on/mid/server/to/txt_file.txt";
new SNC.MidServerUtils().uploadFile(midServer, exportPath, txtData, true);
Replace "/path/to/export_set_csv_file.csv" with the path to your Export Set CSV file.
Replace "/path/to/txt_file.txt" with the path where you want to save the TXT file.
Replace "/path/on/mid/server/to/txt_file.txt" with the path on the mid server where you want to export the TXT file.
Save the scheduled job.
Run the scheduled job manually to convert the Export Set CSV file to a TXT file, and export it to the specified mid server location.
This script reads the Export Set CSV file using the GlideSysAttachment API, replaces commas with tabs in the CSV data, and writes the new TXT data to a new file using the same API. Then, it exports the TXT file to the mid server using the SNC.MidServerUtils API. You can modify this script to fit your specific requirements.
Please mark my reply as a solution if it helps move to next step of your desired ServiceNow goals.
Thanks,
Punit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2023 01:39 PM
It's not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2023 07:11 AM
Do you confirm the paths are valid for these rows?
// Set the path to the Export Set CSV file var csvFile = "/path/to/export_set_csv_file.csv"; // Set the path to the output TXT file var txtFile = "/path/to/txt_file.txt";