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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/981494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:37:43  来源:igfitidea点击:

Windows Command Line Script to rename a folder to current month -3 (e.g. 2009-04 to 2009-01)

windowscommand-linescripting

提问by Techboy

What would the Windows command line script be to rename a folder from the current month, to the current month - 3, using the format YYYY-MM ?

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

e.g.:

例如:

c:\myfiles\myFolder\

should become:

应该变成:

c:\myfiles09-01\

回答by Cheeso

For my locale, I need something different.

对于我的语言环境,我需要一些不同的东西。

Also you need to deal with single-digit 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

回答by Joey

You will have to dissect the contents of %DATE%by yourself, unfortunately. There are no localization-safe date/time manipulation facilities in cmd.

%DATE%不幸的是,您将不得不自己剖析内容。cmd 中没有本地化安全的日期/时间操作工具。

For my locale (which uses standard ISO 8601 date format) I could just use the following:

对于我的语言环境(使用标准 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%"

However, depending on the date format you use it may look slightly different.

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

回答by Suvesh Pratapa

ren *-04 *-01

仁*-04 *-01

回答by Techboy

The version of the answered question for UK date format (DD-MM-YYYY) is:

英国日期格式 (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