Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to generate a date string for an LDAP query compatible with the Active Directory whenChanged field. I'm pulling AD data into CSV using CSVDE and need an LDAP query that will filter the results to those items changed within the last 2 days. I pieced together the following FOR command to generate the first part of the comparison string based on a few examples found here on Stack Overflow:

FOR /F "usebackq" %i in (`PowerShell $date^= [DateTime]::Today.AddDays^(-2^)^; $date.ToString^('yyyyMMdd'^)`) DO SET daysAgo = %i

This FOR command works fine from the command prompt, but bombs inside a batch script, w/ the following output:

:Today.AddDays(-2); was unexpected at this time.

What's causing the command to bomb? Thanks.

share|improve this question
1  
Solved. Variables need to be prefixed w/ %% inside a batch file. – Brian Rowland May 7 at 3:39
4  
Suggestion: If you have solved this then answer your own question and (when permitted) accept that answer. (PS. all of your problem can be solved in PSH: AD and CSV are both included without external components.) – Richard May 7 at 8:17
Power shell does allow cmd commands, you know that right? – fftk4323 Jun 10 at 13:37

1 Answer

I'm stealing his juice... FOR iterator variables in a batch file need to have a double percent sign, %%. So, your line would look like this

FOR /F "usebackq" %%i in (<snipped-powershell-command>) DO SET daysAgo=%%i
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.