Grzegorz Grzegorz - 3 months ago 13
PowerShell Question

Getting the date taken parameter for the image with a name as variable

I want to get the

Date Taken
parameter for an image (identified by
NAME_OF_AN_IMAGE
variable with
PATH_TO_IMAGE_FOLDER
constant) and pass it to the main batch script; within the batch script I must use a Powershell instruction (it is not possible to get the
Date Taken
directly by cmd command).

I have to provide a
NAME_OF_AN_IMAGE
variable to the Powershell script and at the same time bypass the restricted execution policy.

Briefly, the date taken for the named image should be returned from the Powershell script and set to the
Date
variable within the batch file.

I have the following lines of commands within the Powershell 'DateTaken.ps1' script:

[reflection.assembly]::LoadWithPartialName("System.Drawing");

$imagePath = "PATH_TO_IMAGE_FOLDER\NAME_OF_AN_IMAGE.JPG";
$pic=New-Object System.Drawing.Bitmap $imagePath;
$DATE = $pic.GetPropertyItem(36867).value[0..9];

$strYear = [Char]$date[0],[Char]$date[1],[Char]$date[2],[Char]$date[3];
$strMonth = [Char]$date[5],[Char]$date[6];
$strDay = [Char]$date[8],[Char]$date[9];
$DateTaken = $strYear + "." + $strMonth + "." + $strDay;


and within the batch file:

I am not able to run the set of Powershell instructions listed above in order to get to the
$DateTaken
variable and assign it to Data variable within the batch script.

If I get the set of the above Powershell instructions to return the value of
Data
, I will be able to pass it into the temp.txt file using the grater than sign '> temp.txt' and then read it into the batch file by
set /p Data< temp.txt
command.

Answer

I think your problem is, that set requires a String and $DateTaken in your script contains an array. The other thing is, that you have to output $DateTaken.

So that's what I did and this script outputs me the date as String.

[reflection.assembly]::LoadWithPartialName("System.Drawing");

$imagePath = "C:\temp\pic2.jpg"
$pic=New-Object System.Drawing.Bitmap $imagePath

$picDate = $pic.GetPropertyItem(36867).value[0..9]

$strYear = [String][Char]$picdate[0]+[String][Char]$picdate[1]+[String][Char]$picdate[2]+[String][Char]$picdate[3]
$strMonth = [String][Char]$picdate[5]+[String][Char]$picdate[6]
$strDay = [String][Char]$picdate[8]+[String][Char]$picdate[9]
$DateTaken = $strYear + "." + $strMonth + "." + $strDay
$DateTaken

Thanks @MathiasR.Jessen

shorter - more powershellway :)

[reflection.assembly]::LoadWithPartialName("System.Drawing");

$imagePath = "C:\temp\pic2.jpg"
$pic=New-Object System.Drawing.Bitmap $imagePath

$picDate = $pic.GetPropertyItem(36867).value[0..9]

$DateTaken = [char[]]$picdate -join '' -replace '\D','.'
$DateTaken
Comments