Skip to content

Commit a6ecc42

Browse files
Add NewFileName parameter to Convert-PnPFile for choosing a custom output filename (#4878)
* Add NewFileName parameter to Convert-PnPFile for custom output filenames * Update NewFileName parameter to be optional for UploadToSharePoint and improve extension validation * Expand Warning about extension missmatch. * Removed Typo * Updated Parameter Description * Overwrite Warning should contain the automatic or custom NewFileName --------- Co-authored-by: Gautam Sheth <gautamdsheth@outlook.com>
1 parent f463324 commit a6ecc42

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

documentation/Convert-PnPFile.md

+21
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ Convert-PnPFile -Url "/sites/demo/Shared Documents/Test/Book.png" -ConvertToForm
7777

7878
Retrieves the file and converts to JPG, and save it to the given Document library (Folder) in SharePoint Online (same site collection)
7979

80+
### EXAMPLE 7
81+
```powershell
82+
Convert-PnPFile -Url "/sites/demo/Shared Documents/Test/Book.xlsx" -Folder "/sites/demo/Shared Documents/Archive" -NewFileName "differentname.pdf"
83+
```
84+
85+
Retrieves the file and converts to PDF, and save it to the given Document library (Folder) in SharePoint Online (same site collection) giving it the filename differentname.pdf
86+
8087
## PARAMETERS
8188

8289
### -Url
@@ -177,6 +184,20 @@ Accept pipeline input: False
177184
Accept wildcard characters: False
178185
```
179186
187+
### -NewFileName
188+
Filename to give the file local or on SharePoint
189+
190+
```yaml
191+
Type: String
192+
Parameter Sets: Save to local path, Upload to SharePoint
193+
194+
Required: False
195+
Position: Named
196+
Default value: None
197+
Accept pipeline input: False
198+
Accept wildcard characters: False
199+
```
200+
180201
## RELATED LINKS
181202
182203
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/Files/ConvertFile.cs

+22-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class ConvertFile : PnPWebCmdlet
3636
[ValidateNotNullOrEmpty]
3737
public FolderPipeBind Folder;
3838

39+
[Parameter(Mandatory = false, ParameterSetName = URLTOPATH)]
40+
[Parameter(Mandatory = false, ParameterSetName = UPLOADTOSHAREPOINT)]
41+
public string NewFileName = string.Empty;
42+
3943
[Parameter(Mandatory = false, ParameterSetName = URLASMEMORYSTREAM)]
4044
public SwitchParameter AsMemoryStream;
4145

@@ -68,18 +72,30 @@ protected override void ExecuteCmdlet()
6872

6973
LogDebug("Converting file to the specified format");
7074
var convertedFile = sourceFile.ConvertTo(new ConvertToOptions { Format = ConvertToFormat });
75+
var newFileExtension = "." + ConvertToFormat.ToString();
7176

72-
var fileName = System.IO.Path.GetFileNameWithoutExtension(sourceFile.Name);
73-
var newFileName = fileName + "." + ConvertToFormat.ToString();
74-
77+
if (string.IsNullOrEmpty(NewFileName))
78+
{
79+
// Use original filename with new extension
80+
var fileName = System.IO.Path.GetFileNameWithoutExtension(sourceFile.Name);
81+
NewFileName = fileName + newFileExtension;
82+
}
83+
else
84+
{
85+
var extensionMatch = System.IO.Path.GetExtension(NewFileName).Equals(newFileExtension, System.StringComparison.OrdinalIgnoreCase);
86+
if (!extensionMatch)
87+
{
88+
LogWarning($"File extension of NewFileName '{NewFileName}' doesn't match ConvertToFormat '{newFileExtension}'. The new file might become unusable.");
89+
}
90+
}
7591
switch (ParameterSetName)
7692
{
7793
case URLTOPATH:
7894

79-
var fileOut = System.IO.Path.Combine(Path, newFileName);
95+
var fileOut = System.IO.Path.Combine(Path, NewFileName);
8096
if (System.IO.File.Exists(fileOut) && !Force)
8197
{
82-
LogWarning($"File '{sourceFile.Name}' exists already. Use the -Force parameter to overwrite the file.");
98+
LogWarning($"File '{NewFileName}' exists already. Use the -Force parameter to overwrite the file.");
8399
}
84100
else
85101
{
@@ -101,7 +117,7 @@ protected override void ExecuteCmdlet()
101117

102118
LogDebug("Uploading file to the specified folder");
103119
var folder = EnsureFolder();
104-
var uploadedFile = folder.UploadFile(newFileName, convertedFile, Force);
120+
var uploadedFile = folder.UploadFile(NewFileName, convertedFile, Force);
105121

106122
try
107123
{

0 commit comments

Comments
 (0)