1

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.

Grzegorz
  • 25
  • 2
  • 7
  • 1
    Don't use variable name `date` as this is system-reserved! – aschipfl Aug 29 '16 at 11:35
  • Possible duplicate of [Windows batch assign output of a program to a variable](http://stackoverflow.com/questions/2323292/windows-batch-assign-output-of-a-program-to-a-variable) – wOxxOm Aug 29 '16 at 11:44
  • I will change the date to the data then. – Grzegorz Aug 29 '16 at 11:47
  • As for assigning Powershell output to a batch variable with bypassing execution policy restriction at the same time - I don't have a faintest idea. – Grzegorz Aug 29 '16 at 11:50

1 Answers1

2

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
Eldo.Ob
  • 774
  • 4
  • 16