I managed to make a script that opens a pdf file, reads it, makes 2 variables with information from it and saves the file using those variables, but i cant make it do that to all the pdf files in a folder. i dont know if anyone can help me. This is what i have so far:
$file = "C:\Users\..." #path to my pdf file
Add-Type -Path "C:\Program Files\...\itextsharp.dll"
$pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file
$text=[iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,1)
$startss = $text.LastIndexOf("Completo")
$endss = $text.LastIndexOf("Doc")
$name = $text.Substring($startss +9,$endss - $startss-10)
$startss2 = $text.LastIndexOf("Modalidad")
$endss2 = $text.LastIndexOf("(Entre")
$mode = $text.Substring($startss2 +10,$endss2 - $startss2-10)
$pdf.Close()
Rename-Item -NewName ($name + "-" + $mode + "-" + ".pdf") -Path "$file"
You just need a foreach loop. I would recommend reading about Get-ChildItem if you're not sure what it is and some basics on foreach loops.
$folder = "C:\Users\path\to\pdf folder"
Add-Type -Path "C:\Program Files\...\itextsharp.dll"
$pdfFiles = Get-ChildItem "$folder\*.pdf"
foreach($file in $pdfFiles)
{
$pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file
$text= [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,1)
$startss = $text.LastIndexOf("Completo")
$endss = $text.LastIndexOf("Doc")
$name = $text.Substring($startss +9,$endss - $startss-10)
$startss2 = $text.LastIndexOf("Modalidad")
$endss2 = $text.LastIndexOf("(Entre")
$mode = $text.Substring($startss2 +10,$endss2 - $startss2-10)
$pdf.Close()
Rename-Item -NewName ($name + "-" + $mode + "-" + ".pdf") -Path $file
}
Related
I am trying to convert .pptx file to .pdf using powershell. I have used below code
write-host "Converting pptx to pdf....." -ForegroundColor Green
$ppt = New-Object -com powerpoint.application
$opt = [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
$ifile = $file.FullName
$pres = $ppt.Presentations.Open($ifile)
$pathname = split-path $ifile $filename = Split-Path $ifile -Leaf
$file = $filename.split(".")[0]
$ofile = $pathname + "\" + $file + ".pdf"
$pres.SaveAs($ofile, $opt)
While running the code, i am getting error as:
Error HRESULT E_FAIL has been returned from a call to a COM component.
Any help would be highly appreciated
$pptx = "C:\test\test.pptx"
$pdf = "C:\test\test.pdf"
$ppt = New-Object -ComObject PowerPoint.Application
$ppt.Visible = $True
$presentation = $ppt.Presentations.Open($pptx)
$presentation.SaveAs($pdf, [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF)
$presentation.Close()
$ppt.Quit()
Im trying to rename every pdf file in a folder according to variables obtained from those files but i cant get it to work, i can rename one file at a time but i cant manage to make it work for every file.
Here is what i have to rename one file:
Add-Type -Path "C:\Program Files\...\itextsharp.dll"
$file = "C:\Program Files\...\pdf file.pdf"
$pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file
$text=[iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,1)
$startss = $text.LastIndexOf("Completo")
$endss = $text.LastIndexOf("Doc")
$name = $text.Substring($startss +9,$endss - $startss-10)
$startss2 = $text.LastIndexOf("Modalidad")
$endss2 = $text.LastIndexOf("(Entre")
$mode = $text.Substring($startss2 +10,$endss2 - $startss2-10)
Rename-Item -NewName ($name + "-" + $mode + ".pdf") -Path "$file"
$pdf.Close()
And here is what i have to rename every file:
$folder = "C:\...\pdfs folder"
Add-Type -Path "C:\Program Files\...\itextsharp.dll"
$pdffiles = Get-ChildItem -File "$folder\*.pdf"
Foreach($file in $pdffiles)
{
$pdf = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file
$text=[iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdf,1)
$startss = $text.LastIndexOf("Completo")
$endss = $text.LastIndexOf("Doc")
$name = $text.Substring($startss +9,$endss - $startss-10)
$startss2 = $text.LastIndexOf("Modalidad")
$endss2 = $text.LastIndexOf("(Entre")
$mode = $text.Substring($startss2 +10,$endss2 - $startss2-10)
Rename-Item -NewName ($name + "-" + $mode + "-" + ".pdf") -Path "$file"
$pdf.Close()
}
Hope someone can help me to make it work, thanks in advance
I'm trying to achieve the following via powershell:
I have a table(TBL_DDL) with 5 columns (CATALOG,SCHEMA,OBJECT_TYPE,OBJECT_NAME,DDL)
Now, i'm extract data from this table and then trying to create a folder structure by concatenating first 4 columns (CATALOG,SCHEMA,OBJECT_TYPE,OBJECT_NAME) in C: drive and then exporting the data in DDL column in txt file.
For eg: C:\"CATALOG"\"SCHEMA"\"OBJECT_TYPE"\"OBJECT_NAME"\DDL.txt
I'm trying to achieve this via powershell. Can anyone help me please?
$SqlCmd = 'snowsql -c example -d tu_test -s public -q "select catalog,schema,OBJECT_TYPE,OBJECT_NAME,DDL from SF_TBL_DDL limit 2"'
$MultiArray = #(Invoke-Expression $SqlCmd)
$dt = New-Object System.Data.Datatable
[void]$dt.Columns.Add("CATALOG")
[void]$dt.Columns.Add("SCHEMA")
$Output = foreach ($Object in $MultiArray)
{
foreach ($SCHEMA in $Object.SCHEMA)
{
$someother = New-Object -TypeName psobject -Property #{CATALOG = $Object.CATALOG; SCHEMA = $SCHEMA}
$nRow = $dt.NewRow()
$nRow.CATALOG = $someother.CATALOG
$nRow.SCHEMA = $someother.SCHEMA
$dt.Rows.Add($nRow)
}
}
$dt.row.count
At the moment, i'm getting 0 rows in $dt.
Cheers
You can use System.Data.DataTable object the pull your result set and then loop through it to perform the required operation.
Here GetTableValues function will retrieve the table values and then use following cmdlet to create directory and file
New-Item -ItemType "directory" -Path $dirPath
New-Item -ItemType "file" -Path $filePath
Complete code looks like this
function GetTableValues(){
$DBConnectionString = "<Your DB connection string>";
$sqlConn = new-object System.Data.SqlClient.sqlConnection $DBConnectionString;
$sqlConn.Open();
$sqlCommand = $sqlConn.CreateCommand();
$sqlCommand.CommandText = "select catalog,[schema],OBJECT_TYPE,OBJECT_NAME,DDL from TBL_DDL"; ##Put your correct query here
$result = $sqlCommand.ExecuteReader();
$table = New-Object System.Data.DataTable;
$table.Load($result);
$sqlConn.Close();
return $table;
}
$tableValue = GetTableValues;
foreach ($Row in $tableValue)
{
$filePath = "C:\" + $Row.catalog.TrimEnd() + "\" + $Row.schema.TrimEnd() + "\" + $Row.OBJECT_TYPE.TrimEnd() + "\" + $Row.OBJECT_NAME.TrimEnd() + "\" + $Row.DDL.TrimEnd() + ".txt"
$dirPath = "C:\" + $Row.catalog.TrimEnd() + "\" + $Row.schema.TrimEnd() + "\" + $Row.OBJECT_TYPE.TrimEnd() + "\" + $Row.OBJECT_NAME.TrimEnd()
New-Item -ItemType "directory" -Path $dirPath ##Creates directory
New-Item -ItemType "file" -Path $filePath ##Creates file in $dirPath directory
}
This works perfectly fine for me.
A little background first.
In my Inbox I have a Subfolder called One
Inside this is an email from someone with an attachment called One.pdf
In my Inbox I have a Subfolder called Two
Inside this is an email from someone with an attachment called Two.pdf
In my Inbox I have a Subfolder called Own inside this I have a Subfolder called Three.
Inside this is an email from someone with an attachment called Three.pdf
In my Inbox I have am Email with an attachment called Four.pdf
Inbox
-----One
--------Three
-----Two
Still with me? :)
I have a requirement to do the following.
I need to parse through the inbox and find the .PDF attachment and save it to another location on a drive.
Then I need to parse through the Subfolders of the inbox. If I find a .pdf I need to do two things.
I need to check if the folders exists and if not create it.
I need to then save the .PDF in that Subfolder to the folder I just created.
Currently I can create the Subfolders.
My problem is I am unable to create the correct files in each sub folder. Infact right now am only able to create four.pdf and I can create that in every sub folder.
Currently I am working with this code.
$O=0
$Obj = New-Object -comobject outlook.application
$Name = $Obj.GetNamespace(“MAPI”)
$Mail = $Name.pickfolder()
$Path = "C:\Attached\"
$SubFolder = {
param(
$currentFolder
)
foreach ($item in $currentFolder.Folders) {
$Mail.Items | ForEach {
$O=$O+1
$_.Attachments | foreach {
$item.FolderPath
& $SubFolder $item
}
}
}
}
$Walk = & $SubFolder $Mail
ForEach ($Fo in $Walk){
$Fo.Items | ForEach {
$O=$O+1
$_.Attachments | foreach {
$Sub = $Fo
$Pos = $Sub.IndexOf("\")
$LeftPart = $Sub.Substring($Pos+28)
$FilePath = $Path + $LeftPart + "\"
If ($_.filename -like "*.PDF") {
$_
If (!(Test-Path -path $FilePath))
{
$Dest = New-Item $FilePath -type directory
}
$_.saveasfile((Join-Path $FilePath $_.filename))
}
}
}
}
This allows the selection of the Outlook folder and then does nothing. If I change $Fo.Items to $Mail.items it creates the folders and the four.pdf
I know $Fo isn't what I want it to be but am not sure what differection to take from here.
Any advice please.
Thanks
Not sure why I was voted down, be intrested to know?
Anyway. I can to this solution for my issue.
Each "section" will drill down a sub folder level, build the folder structure then save off the attahments based on type. Its by no means pretty but it does the job I need. I have posted it here for reference.
$O=0
$Outlook = New-Object -com "Outlook.Application"
$NameSpace = $Outlook.GetNamespace("MAPI")
$Mail = $NameSpace.pickfolder()
$Parent = $Mail.Folders
$Path = “C:\Attached\”
ForEach ($Folder in $Parent) {
#Parent Folder.
ForEach($SubFolder in $Folder) {
#First subfolder.
ForEach ($Item in $SubFolder.Items){
$O=$O+1
$Sub = $SubFolder.FolderPath
$LeftPart = $Sub.Substring($Pos+28)
$FilePath = $Path + $LeftPart + "\"
If (!(Test-Path -path $FilePath))
{
$Dest = New-Item $FilePath -type directory
}
foreach ($Attach in $Item.Attachments){
If ($Attach.filename -like "*.PDF") {
$Attach.saveasfile((Join-Path $FilePath $Attach.filename))
}
If ($Attach.filename -like "*.doc*") {
$Attach.saveasfile((Join-Path $FilePath $Attach.filename))
}
}
}
ForEach ($SubSubFolder in $SubFolder.Folders){
#Second Subfolder.
ForEach ($SubItem in $SubSubFolder.Items){
$1=$1+1
$Sub = $SubSubFolder.FolderPath
$LeftPart = $Sub.Substring($Pos+28)
$FilePath = $Path + $LeftPart + "\"
If (!(Test-Path -path $FilePath))
{
$Dest = New-Item $FilePath -type directory
}
foreach ($Attach in $SubItem.Attachments){
If ($Attach.filename -like "*.PDF") {
$Attach.saveasfile((Join-Path $FilePath $Attach.filename))
}
If ($Attach.filename -like "*.doc*") {
$Attach.saveasfile((Join-Path $FilePath $Attach.filename))
}
}
}
}
}
}
I am trying to write a script that will get the DATETAKEN attribute from a photo and create a folder structure based on that and move the file to this new location. I have found scripts on google that I'm trying to use but when I running it, it returns:
PS C:\Temp> C:\Temp\MovePhoto.ps1
GAC Version Location
--- ------- -------- True v4.0.30319
C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0....
Move-Item : The process cannot access the file because it is being
used by another process. At C:\Temp\MovePhoto.ps1:43 char:5
+ Move-Item $FileFullName "$FileDirFull\$FileBaseNameNU"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\NoBackup\TES...RA\IMG_1372.JPG:FileInfo) [Move- Item],
IOException
+ FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
If I do the script without the SystemDrawing line it works. But then I can't get the DATETAKEN attribute. I just can't figure out what I am missing.
Here is the script
[reflection.assembly]::LoadWithPartialName("System.Drawing")
$FileAll = (Get-ChildItem $SourcePath -Recurse | where {!$_.psiscontainer} | Select-Object Name,Fullname,BaseName,Extension,CreationTime,LastWriteTime,Length,#{Name="MD5";Expression={Get-Md5Hash $_.fullname}} | group MD5 | Where {$_.Count -gt 1 } | %{$_.Group} | sort MD5)
foreach ($File in $FileAll) {
$FileBaseName = $File.BaseName
$FileExtension = $File.Extension
$FileFullName = $File.FullName
$FileBaseNameNu = $FileBaseName + $FileExtension
$FileName = $File.Name
}
$foo = New-Object -TypeName System.Drawing.Bitmap -ArgumentList $FileFullName
$date = $foo.GetPropertyItem(36867).Value[0..9]
$arYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3]
$arMonth = [Char]$date[5],[Char]$date[6]
$arDay = [Char]$date[8],[Char]$date[9]
$strYear = [String]::Join("",$arYear)
$strMonth = [String]::Join("",$arMonth)
$strDay = [String]::Join("",$arDay)
$DateTaken = $strYear + "-" + $strMonth + "-" + $strDay
$FileLastWriteTime = $File.LastWriteTime
$FileDirYear = $FileLastWriteTime.Year
$FileDirDate = $FileLastWriteTime.ToShortDateString()
$FileDirFull = "$DestinationPath\DUBLETTER\$FileDirYear\$DateTaken"
# Create destination path
if ((Test-Path $FileDirFull) -eq $false) {
New-Item -Path $FileDirFull -ItemType Directory
}
if (Test-Path (Join-Path $FileDirFull $File.Name)) {
$n = 0
while ((Test-Path (Join-Path $FileDirFull $FileBaseNameNU)) -eq $true){
$FileBaseNameNU = $FileBaseName + "-" + ++$n + $FileExtension
}
}
Move-Item $FileFullName "$FileDirFull\$FileBaseNameNU"
}
Can you try to replace
[reflection.assembly]::LoadWithPartialName("System.Drawing")
by
Add-Type -AssemblyName "system.drawing"
Forget it, your trouble is with your file C:\NoBackup\TES...RA\IMG_1372.JPG wich can't be moved because it's open (seems to be open for usage in $foo var). Try first to copy it. You perhaps can use $foo.Dispose() before Move-Item $FileFullName "$FileDirFull\$FileBaseNameNU"
I've done this in VBScript, using GetDetailsOf()... see MSDN
This can be done using PowerShell, see... Scripting Guys
There may be a .NET method way of doing this, i.e.: more "native" to PS.