I have hundreds of HTML pages in my local drive, which are maintained in nested folder (can be upto 7 levels). My requirement is that the code should read these from my local drive and create these nested folders and read content from local html file to create a modern site page.
MyFolder
Folder1
File1
File2
Subfolder1
subfile1
subfile2
subfile3
subsubfolder1
subsubfile1
subsubfile2
subsubfolder2
subsubsubfolder1
The ClientSidePage method create modern SitePages at root folder of Library, whereas I want to create them in nested folder structure.
I have tried to move files from SitePages to one folder level. But I am not sure, how to take this to 7 levels. Below is the code I have used using CSOM.
string text = System.IO.File.ReadAllText("\\\\?\\" + path);
ClientSidePage myPage = new ClientSidePage(ctx);
ClientSideText txt1 = new ClientSideText() { Text = text };
myPage.AddControl(txt1, 0);
var result = Path.ChangeExtension(Path.GetFileName(path), ".aspx");
myPage.Save(pagename);
File fileitem = ctx.Web.GetFileByServerRelativeUrl( ctx.Web.ServerRelativeUrl + "/SitePages/" + pagename);
ctx.Load(fileitem);
ctx.ExecuteQuery();
string targetFilePath = "https://xyz.sharepoint.com/teams/testsite/SitePages/" + folder1 + "/" + pagename;
fileitem.MoveTo(targetFilePath, MoveOperations.Overwrite);
Any help on this is highly appreciated!
Related
Our organization creates the same folder structure with templated documents for each job. Right now staff manually creates each folder and within those folder creates each document from a template in the organizations template gallery. Is there a simple-ish way to automate this?
This is how I would approach this if you would want to create a simple automated sheet script for it.
function createFolder() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var getFolderID = ss.getRange('A2').getValue();
var setFolderName = ss.getRange('B2').getValue();
var setDocName = ss.getRange('C2').getValue();
var setTemplate = ss.getRange('D2').getValue();
var drive = DriveApp;
var parentfolder = drive.getFolderById(getFolderID);
var newFolder = parentfolder.createFolder(setFolderName);
var createDoc = DocumentApp.create(setDocName);
var getDocID = createDoc.getId();
drive.getFileById(getDocID).moveTo(newFolder).setContent(setTemplate);
}
What this does is I created a sheet template like this:
Set the Parent Folder ID on cell A2 which you can find on the folder URL "https://drive.google.com/drive/folders/{parentfolderID}?resourcekey"
Set the folder name on cell B2, as well as the document name on C2 and your template on D2.
You can run the script by going into Tools > Script Editor, or you can assign an image to act as a button and assign the script to that image.
Running the script would result into this folder hierarchy
with the value of D2 inside the Google Doc.
References:
https://developers.google.com/apps-script/reference/document/document-app
https://developers.google.com/apps-script/reference/drive/drive-app
https://developers.google.com/apps-script/reference/spreadsheet/sheet
my problem: I have many text files that I want to rename.
I have been using the ADODB.Stream object to open/read/write the files because they are encoded in UTF-8. So now, if possible, I want to rename the files without the workaround of copying their content, writing their content into a new file with the desired name and deleting the old one. The time stamp on the documents is a valuable information for me, which is why I do not want to create new files.
here is my current workaround that creates new files and deletes the old ones.
Issues with the code:
1) Copied files have new time stamps
2) New Lines don't get copied into the new lines. As they contain some kind of XML code, the generated files become hard to read. I would need to write a piece of code that sets new lines on all appropiate positions after copying.
Sub renameModules()
Dim currentTXT As Variant, newTxt As Variant
Dim currentPath As String, newPath As String
Dim currentContent As String
currentPath = "C:\Users\Me\Desktop\Test\MyCurrent.txt"
newPath = "C:\Users\Me\Desktop\Test\Target001.txt"
Set currentTXT = CreateObject("ADODB.Stream")
currentTXT.Charset = "utf-8"
currentTXT.Open
currentTXT.LoadFromFile (currentPath)
currentContent = currentTXT.ReadText()
Set newTxt = CreateObject("ADODB.Stream")
newTxt.Charset = "utf-8"
newTxt.Open
newTxt.WriteText currentContent
newTxt.savetofile newPath, 1
Kill currentPath
End Sub
For simplicity I have only included the essential steps and omitted all error handling.
My goal: Finding some method to simply rename the current file without fiddling with its content.
No need to deal with ADODB.Stream: You can use the VBA command name not only to rename a file but also to move it to a different folder:
Name currentPath As newPath
I try to write script on Google Sheet to trigger below action.
Move a target file from root (my drive) to the destination folder then rename it in the destination folder.
Below items will be put on google sheet . I want to create a formula function to trigger the script action once below items are provide on google sheet.
Target file name
Name ID for rename
Below script works perfectly (for move file action); i would like alter below script to accomplish above action.
function copyFiles(source_folder, dest_folder) {
var source_folder = DriveApp.getFolderById(id);
var dest_folder = DriveApp.getFolderById(id);
var files = DriveApp.getFilesByName(id);
while (files.hasNext()) {
var file = files.next();
dest_folder.addFile(file);
source_folder.removeFile(file);
}
}
Thank you!!
If you just want to rename your file, you should have a look at File classe, on File.setName(name) method.
Otherwise, you question is a bit unclear and need some explanations/examples of what you trying to do.
I am trying to get files from filedialog and display the name of the file names inside the listview. And also checkboxes should also be created before the filenames inside the listview based on the number of files added. Below is my code that returns only one file with the check box, irrespective of any number of files selected. Help would be appreciated.
def OpenTheFile(self):
file = QtGui.QFileDialog.getOpenFileNames(self.dlg, "Select one or more files to open", os.getenv("HOME"),'.sql (*.sql)')
str_file = ','.join(file)
fileinfo = QFileInfo(str_file)
filename = QFileInfo.fileName(fileinfo)
if fileName:
for files in str_file:
model = QStandardItemModel()
item = QStandardItem('%s' % fileName)
item.setCheckable(True)
model.appendRow(item)
self.dlg.DatacheckerlistView2.setModel(model)
self.dlg.DatacheckerlistView2.show()
It is really unclear what you are doing (there is very little context to your question), but the following code should work. There were several issues with the code in your original question, namely:
You were joining all of the files into a single string and iterating over the string, rather than iterating over the list of filenames.
You were recreating the model each iteration of your loop, effectively deleting any previously added rows.
The code:
files = QtGui.QFileDialog.getOpenFileNames(self.dlg, "Select one or more files to open", os.getenv("HOME"),'.sql (*.sql)')
if files:
model = QStandardItemModel()
for file in files:
item = QStandardItem('%s' % file)
item.setCheckable(True)
model.appendRow(item)
self.dlg.DatacheckerlistView2.setModel(model)
self.dlg.DatacheckerlistView2.show()
Is it possible to get all files (ex : jpg images) from a particular folder with out using openfiledialog method in lotus script? In lotus script, we can give the file path as hard coded.
You can use Dir to get all files from a folder:
Dim pathName As String
Dim fileName As String
pathName = "C:\yourFolder\"
fileName = Dir(pathName + "*.jpg", 0)
Do While fileName <> ""
Print pathName + fileName
fileName = Dir()
Loop
This sample code prints all your jpg files from yourFolder like
C:\yourFolder\a.jpg
C:\yourFolder\b.jpg
C:\yourFolder\c.jpg
From there you can use the list to attach the files to a document or whatever you want to do with the files.
Here you can find the description.