Windows Script command line to rename the folder in the current month -3 (for example, 2009-04 - 2009-01)

Which Windows script command line should be renamed from the current month, to the current month - 3, using the YYYY-MM format?

eg:.

c:\myfiles\myFolder\

should become:

c:\myfiles\2009-01\
+3
source share
4 answers

For my language I need something else.

You also need to deal with unambiguous months, I suppose.

setlocal
@REM example:  Thu 06-11-2009
set stamp=%DATE%

@REM get the year
set year=%stamp:~10,4%
@REM example: 2009

@REM get the month
set month=%stamp:~4,2%
@REM example:  06

@REM subtract 3 months
set /a month=%month%-3
@REM example:  3

@REM test if negative (we rolled back beyond January 1st)
if %month% LSS 1  (
  set /a month=%month%+12
  @REM example: 8
  set /a year=%year%-1
  @REM example: 2008
)

@REM prepend with zero for single-digit month numbers
set month=0%month%

@REM take last 2 digits of THAT
set month=%month:~-2%

set newFolder=%year%-%month%

@REM move %1 %newFolder%
endlocal
+4
source

Unfortunately, you will have to analyze the content yourself %DATE%. Cmd lacks tools for setting the date and time that do not require localization.

( ISO 8601) :

@echo off
rem %DATE% comes back in ISO 8601 format here, that is, YYYY-MM-DD
set Y=%DATE:~0,4%
set /a M=%DATE:~5,2% - 3
if %M% LSS 1 (
    set /a Y-=1
    set /a M+=12
)
ren myFolder "%Y%-%M%"

, , , .

+2

ren * -04 * -01

0
source

Version of the answer question for the date format in the UK (DD-MM-YYYY):

setlocal

@REM example:  11-06-2009
set stamp=%DATE%
@REM get the year

set year=%stamp:~6,4%
@REM example: 2009
@REM get the month

set month=%stamp:~3,2%
@REM example:  06

@REM subtract 3 months
set /a month=%month%-3
@REM example:  3

@REM test if negative (we rolled back beyond 1st January)
if %month% LSS 1  (
  set /a month=%month%+12
  @REM example: 8

  set /a year=%year%-1
  @REM example: 2008
)

@REM prepend with zero for single-digit month numbers
set month=0%month%

@REM take last 2 digits of THAT
set month=%month:~-2%

set newFolder=%year%-%month%

move c:\myfiles\myFolder\ %newFolder%

endlocal
0
source

Source: https://habr.com/ru/post/1710255/


All Articles