0

I want to create a folder naming it to the previous month. But my code does not seem to work. Here is my code.

@echo off

for /F "tokens=1,3 delims=-" %%a in ("%date%") do (
   set prevYear=%%a
   set month=%%b
)
if "%month:~0,1%" equ "0" set month=%month:~1%
set /A prevMonth=month-1
if "%prevMonth%" equ "0" (
   set /A prevMonth=12, prevYear-=1
)
if %prevMonth% lss 10 set prevMonth=0!prevMonth!

mkdir %prevMonth%"-"%prevYear%"-01"

Any help please.

EDIT: Base on comment.

simple guy
  • 633
  • 1
  • 6
  • 19

1 Answers1

1

Here'e an alternative using PowerShell:

@Echo Off
For /F UseBackQ %%A In (
    `PowerShell "(Get-Date).AddMonths(-1).ToString('yyy-MM')"`
) Do If Not Exist "%%A-01\" MD "%%A-01"
Compo
  • 36,585
  • 5
  • 27
  • 39