Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a txt. file, copy the content into a new file and replace certain content

I have a .txt file online filled with text such as:

"Type": "Internal",
"Category": 1,
"Presentation": 3,
...

I want to use Apps Script to read out the content of that file and ultimately save it in a new file with modified content. Specifically, I need to remove the underscore of some content but keep the rest intact.

like image 719
user3767465 Avatar asked Jun 23 '14 12:06

user3767465


People also ask

How do I replace text in multiple text files?

Remove all the files you don't want to edit by selecting them and pressing DEL, then right-click the remaining files and choose Open all. Now go to Search > Replace or press CTRL+H, which will launch the Replace menu. Here you'll find an option to Replace All in All Opened Documents.

What is the command to copy the contents of one file to another file?

1 Answer. Show activity on this post. If you want a content of a file in another file you can use type command to display the content and > to redirect the output to a file. You could just us copy command like @copy x.


1 Answers

Apps Script uses the JavaScript programming language. For Apps Script code that runs on Google's servers, JavaScript code will be in a .gs "script" file. JavaScript was originally created for "client side" use, which means it runs in the browser that you have open on your computer (Not Google's server). In client side JavaScript, the code is put into HTML <script> tags. But Apps Script also uses JavaScript for server side code, it's used for both.

If the text file is in your Google Drive, you can access it in various ways. One way is to use Drive Service

Here is some sample code that can get a file from Google Drive:

var allFilesInFolder,cntFiles,docContent,fileNameToGet,fldr,
    thisFile,whatFldrIdToUse;//Declare all variable at once

whatFldrIdToUse = '123ABC_Put_Your_Folder_ID_here';
fileNameToGet = 'myText.txt';//Assign the name of the file to get to a variable

//Get a reference to the folder    
fldr = DriveApp.getFolderById(whatFldrIdToUse);

//Get all files by that name. Put return into a variable
allFilesInFolder = fldr.getFilesByName(fileNameToGet);
Logger.log('allFilesInFolder: ' + allFilesInFolder);

if (allFilesInFolder.hasNext() === false) {
  //If no file is found, the user gave a non-existent file name
  return false;
};

cntFiles = 0;
//Even if it's only one file, must iterate a while loop in order to access the file.
//Google drive will allow multiple files of the same name.
while (allFilesInFolder.hasNext()) {
  thisFile = allFilesInFolder.next();
  cntFiles = cntFiles + 1;
  Logger.log('File Count: ' + cntFiles);

  docContent = thisFile.getAs('text/plain');
  Logger.log('docContent : ' + docContent );
};

To see values generated by the code, go into the Logs dialog box. Click the "View" menu, and then choose "Logs" from the menu. The Logger.log() statements print content to the log.

The content you gave for an example looks like JSON. You can convert a string that is in JSON format to an object with JSON.parse(). From there you can add or change values. If you want to convert the JSON back to a string before putting it back into a text file, you can use JSON.stringify()

JSON reference Mozilla

In order to find specific characters in a string, you'll need to learn JavaScript string functions.

To replace all occurrences of a certain piece of text, you can use replace:

Information about replace()

like image 117
Alan Wells Avatar answered Oct 23 '22 02:10

Alan Wells