0

I'm trying to create a batch script which contains PowerShell to get yesterday's date.

So far, I have managed to write the "get the date" part, which is as below:

powershell $date=Get-Date; $date=$date.AddDays(-1); $date.ToString('yyyyMMdd')

How can I pass the $date variable back into the batch script, so that I can use the $date variable to move files? What I am trying to achieve here is seen below:

move *%date%*.xml D:\Sample\

And I would like to accomplish this within a single batch script.

1
  • 1
    Why not write the entire script in Powershell so you don't have to pass a date back from it? Commented Sep 6, 2015 at 11:10

3 Answers 3

1

You could run the PowerShell command line as follows to get the value in a variable in your batch script:

@echo off

for /f "tokens=* delims=" %%d in (
  'powershell.exe -Command "(Get-Date).AddDays(-1).ToString(\"yyyyMMdd\")"'
) do set "yesterday=%%d"

move *%yesterday%*.xml D:\Sample\

You shouldn't name the variable %date%, because that is a variable CMD automatically populates with the current date. See help set:

%DATE% - expands to current date using same format as DATE command.


However, as @alroc suggested in the comments to your question, it would probably be simpler to just write the entire script in PowerShell:

$yesterday = (Get-Date).AddDays(-1).ToString('yyyyMMdd')
Move-Item "*$yesterday*.xml" 'D:\Sample'
Sign up to request clarification or add additional context in comments.

5 Comments

Is the quoting right in D:\Sample in the last script?
@foxidrive It should be. Why?
The two terms on the the last line have different quoting. Is one wrong? Or both right?
@foxidrive PowerShell supports both single and double quotes. In double-quoted strings variables, escape sequences, subexpressions, etc. are expanded, in single-quoted strings they're not. This behavior is similar to other languages, like C#, Perl, Python, or bash. I normally use single quoted strings when they're supposed to remain unchanged like the path literal in the example above. The first argument to Move-Item is double-quoted because it contains a variable ($yesterday) that wouldn't be expanded in a single-quoted string.
Oh! Thank you, I hadn't seen that mentioned and is useful info. I'm likely to forget it too unfortunately - sleep is rare these days. C, C#, Perl, Python, bash aren't in my vocabulary - I'm a bit of a dinosaur.
1

If I understand your question correctly, you want to write a Batch Script (.bat) that uses Powershell to get the previous Date.

Below is what you can put in your .bat Script to get the value.

powershell -Command (Get-Date).AddDays(-1).ToString('yyyyMMdd')

You do not need to "pass back" the variable. Instead, you just make the output of the powershell call the information you want. Using your code you posted, there is no output. Using what is above, however, will be equivalent to "returning" the $date variable in your code.

Batch doesn't really have a simple way to capture the variable, however. There are two main ways to capture variables. You either use a for loop (this is kind of a hack), or to write the output to a file, then read that file into a variable.

Method 1 - For Loop
Here is the code to capture the output into variable named v (access with %v%).

for /f %d in (
  'powershell.exe -Command "(Get-Date).AddDays(-1).ToString(\"yyyyMMdd\")"'
) do set "v=%d"

Method 2 - File Output
Here, we write the powershell output to a file, whose name is stored in the variable tempFile. Then, we read the file's content back into the variable named v. Finally, we delete the tempFile.

powershell.exe -Command (Get-Date).AddDays(-1).ToString('yyyyMMdd') > %tempFile%
set /p v=< %tempFile%
del %tempFile%

1 Comment

Thanks for the notice, forgot about that. I've updated the answer with the two main methods for capturing command output into variables.
0

You could as well do it in CMD.exe completly, although it is a little bit more complex...

:Get_Dates
set day=%DATE:~-10,2%
set month=%DATE:~-7,2%
set year=%DATE:~-4%

set TODAY=%year%-%month%-%day%

set /a tag-=1
if %day% LEQ 0 set /a monat-=1
if %month% LEQ 0 set /a jahr-=1 & set monat=12

if %day% GEQ 1 goto :done
rem day is in the previous month
if %month% EQU 1 set tag=31
if %month% EQU 3 set tag=31
if %month% EQU 4 set tag=30
if %month% EQU 5 set tag=31
if %month% EQU 6 set tag=30
if %month% EQU 7 set tag=31
if %month% EQU 8 set tag=31
if %month% EQU 9 set tag=30
if %month% EQU 10 set tag=31
if %month% EQU 11 set tag=30
if %month% EQU 12 set tag=31

if %month% NEQ 2 goto :done
rem februar so check year
set /a mod4=year % 4
set /a mod100=year % 100
set /a mod400=year % 400
set day=28
if %mod4% NEQ 0 goto :done
set day=29
if %mod100% NEQ 0 goto :done
set day=28
if %mod400% NEQ 0 goto :done
set day=29

:done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.