Windows命令行脚本将文件夹重命名为当前月份-3(例如2009-04至2009-01)

Windows命令行脚本将文件夹重命名为当前月份-3(例如2009-04至2009-01),windows,command-line,scripting,Windows,Command Line,Scripting,使用YYYY-MM格式将文件夹从当前月份重命名为当前月份-3,Windows命令行脚本是什么 e、 g: 应成为: c:\myfiles\2009-01\ ren*-04*-01不幸的是,您必须自己剖析%DATE%的内容。cmd中没有本地化安全日期/时间操作设施 对于我的区域设置(使用标准ISO 8601日期格式),我可以使用以下内容: @echo off rem %DATE% comes back in ISO 8601 format here, that is, YYYY-MM-DD se

使用YYYY-MM格式将文件夹从当前月份重命名为当前月份-3,Windows命令行脚本是什么

e、 g:

应成为:

c:\myfiles\2009-01\

ren*-04*-01

不幸的是,您必须自己剖析
%DATE%
的内容。cmd中没有本地化安全日期/时间操作设施

对于我的区域设置(使用标准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%"

但是,根据您使用的日期格式,它可能看起来略有不同。

对于我的区域设置,我需要一些不同的内容

我想你还需要处理个位数的月份

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

英国日期格式(DD-MM-YYYY)的回答问题版本为:


这是正确的(我给了+1)。虽然我在问题上犯了一个错误-对不起!我现在已经更改了问题中的源文件夹名称。非常感谢!我只是调整了一下英国日期格式(我应该在问题中提到)。我也会发布你的答案的英国版本。另外,我必须将%1定义为源文件夹。@Sohnee-您在编辑中做了什么更改吗?它看起来和我一模一样。
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
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