Batch file 根据子文件夹名称重命名子文件夹的批处理文件

Batch file 根据子文件夹名称重命名子文件夹的批处理文件,batch-file,cmd,Batch File,Cmd,我不熟悉批处理脚本,我需要帮助来解决这个重复的手动任务。我需要自动使用批处理文件 我有如下示例所示的目录结构 c:\machines\models\a\defects c:\machines\models\b\defects c:\machines\models\c\defects c:\machines\models\d\defects and it continues e, f, g, h (a、b、c、d只是示例,因为机器将根据我们测试的模型生成文件夹) 我想移动缺陷文件夹内容(包括其中

我不熟悉批处理脚本,我需要帮助来解决这个重复的手动任务。我需要自动使用批处理文件

我有如下示例所示的目录结构

c:\machines\models\a\defects
c:\machines\models\b\defects
c:\machines\models\c\defects
c:\machines\models\d\defects
and it continues e, f, g, h
(a、b、c、d只是示例,因为机器将根据我们测试的模型生成文件夹)

我想移动缺陷文件夹内容(包括其中的子文件夹和文件) 指向具有以下内容的目标文件夹: 1.当前日期 2.使用批处理文件(ms dos)根据如下所示的it模型重命名缺陷文件夹


请帮忙

以下是此任务的注释批处理代码:

@echo off
setlocal

rem Define source and backup path.
set "SourcePath=C:\machines\models"
set "BackupPath=E:\backup"

rem Get current date in format YYYY-MM-DD independent on local date format.
for /F "skip=1 tokens=1 delims=." %%T in ('%SystemRoot%\System32\wbem\wmic.exe OS get localdatetime') do set LocalDateTime=%%T & goto ReformatDate
:ReformatDate
set "YearMonthDay=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%

rem For each subfolder in source path check if there is a subfolder "defects".
rem If subfolder "defects" exists, copy all files and subfolders of "defects"
rem to the appropriate backup directory with current date and subfolder name
rem in source folder path. Then remove the subfolder "defects" in source
rem folder even if being completely empty to avoid processing this folder
rem again when not being recreated again in the meantime.

for /D %%# in ("%SourcePath%\*") do (
    if exist "%%#\defects\*" (
        %SystemRoot%\System32\xcopy.exe "%%#\defects\*" "%BackupPath%\%YearMonthDay%\%%~nx#_defects\" /H /I /K /Q /R /S /Y >nul
        rd /Q /S "%%#\defects"
    )
)
endlocal
在命令提示符窗口
wmic OS get localdatetime
中运行一次,查看此命令的输出,以更好地了解当前日期是如何以
YYYY-MM-DD
格式确定的。使用
%DATE%
会更快,但
%DATE%
的日期字符串格式取决于Windows区域和语言设置中设置的国家/地区,因此需要了解运行此批处理文件的计算机上的日期字符串格式

如果
models
目录中存在
defects
子文件夹,则带有所用选项的命令XCOPY不会在备份目录中创建子文件夹,但在整个
defects
子文件夹树中,至少没有一个文件要复制

使用环境变量DATE的替代代码,预期扩展的日期字符串以:
MM/DD/YYYY
结尾,例如
2016年5月17日星期二
,在回答中详细解释:

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • echo/?
  • endlocal/?
  • 获取/?
    。。。还解释了
    %%~nx#
    (模型子文件夹的名称(和扩展名))
  • 如果/?
  • rd/?
  • 设置/?
  • setlocal/?
  • wmic.exe操作系统获取/?
  • xcopy/?
另请参阅Microsoft关于理解nul的含义的文章

为什么
%%~nx#
而不仅仅是
%%~n#
,因为文件夹没有文件扩展名

Windows命令处理器不确定字符串是文件夹还是文件名。最后一个反斜杠之后的所有内容都被解释为与文件或文件夹的真实名称无关的文件名。字符串中最后一个点后最后一个反斜杠后的所有内容都被解释为文件扩展名,即使这意味着用
%~n
引用的文件夹或文件名是空字符串,因为文件夹/文件名以点开头,并且不像*nix系统上的许多“隐藏”文件那样多包含一个点,例如
.htaccess
。因此,如果命令行上需要文件夹或文件的完整名称,则应始终使用
%~nx

以下是我的注释代码。Mofi速度更快(没有适合老人的网站…)

使用而不是

注意
/L
robocopy
命令中切换:仅列出-不要复制、标记时间或删除任何文件。在调试之前立即删除
/L
开关

@ECHO OFF
SETLOCAL EnableExtensions
rem obtain %_currentdate% variable in locale independent yyyymmdd format
for /f "tokens=2 delims==" %%G in (
  'wmic OS get localdatetime /value^|find "="') do set "_currentdate=%%G"
set "_currentdate=%_currentdate:~0,8%"
rem show %_currentdate% variable for debugging purposes
set _currentdate

rem appoint source and target directories
set "_sourcedir=c:\machines\models"
set "_targetdir=E:\backup\%_currentdate%"

for /F "delims=" %%G in ('dir /B /AD "%_sourcedir%\"') do (
  set "_notEmpty="
  rem check if `defects` folder exists 
  if exist "%_sourcedir%\%%G\defects\*" (
    rem credit and tribute to DBenham in https://stackoverflow.com/a/10818854/3439404
    rem check if a folder contains at least one file 
    >nul 2>nul dir /a-d "%_sourcedir%\%%G\defects\*" && (set "_notEmpty=f")
    rem check if a folder or any of its descendents contain at least one file
    >nul 2>nul dir /a-d /s "%_sourcedir%\%%G\defects\*" && (set "_notEmpty=f")

    if defined _notEmpty (
        rem create target directory silently; suppress error message 'already exists'
        md "%_targetdir%\%%G_defects\" >NUL 2>&1

        rem move content of %_sourcedir%\%%G\defects\* to %_targetdir%\%%G_defects\
        robocopy "%_sourcedir%\%%G\defects" "%_targetdir%\%%G_defects" /S /E /MOVE /L

        rem recreate source directory deleted (moved) by robocopy 
        md "%_sourcedir%\%%G\defects\" >NUL 2>&1
    ) else (
        echo nothing to do in %_sourcedir%\%%G\defects
    )
  ) else (
      echo nothing to do in %_sourcedir%\%%G
  )
)

向中的DBenham致敬(检查文件夹是否为空)。

欢迎使用SO。请阅读。请在这里输入一些代码。谢谢你。一旦我擅长批处理代码,我会在这里放一些代码:)如果不尝试(并弄错:),你就不可能擅长批处理代码。)好的echo%DATE%的输出是Tue 05/17/。有没有更好的方法将日期用作目录。我更喜欢使用日期,我会四处搜索并阅读您提供的链接。谢谢:)为从环境变量日期确定的目录名添加了一个当前日期格式为
YYYY-MM-DD
的备用批处理代码,格式为
缩写的工作日MM/DD/YYYY
,例如
2016年5月17日星期二
。您好@josefZ,很遗憾,机器(XP)不支持robocopy,我感谢您的帮助。将研究您的代码,因为它可能会在将来的批处理代码中有所帮助。感谢您的帮助:)
@echo off
setlocal

rem Define source and backup path.
set "SourcePath=C:\machines\models"
set "BackupPath=E:\backup"

rem Get current date in format YYYY-MM-DD depending on local date format.
set "YearMonthDay=%DATE:~-4,4%-%DATE:~-10,2%-%DATE:~-7,2%"

rem For each subfolder in source path check if there is a subfolder "defects".
rem If subfolder "defects" exists, copy all files and subfolders of "defects"
rem to the appropriate backup directory with current date and subfolder name
rem in source folder path. Then remove the subfolder "defects" in source
rem folder even if being completely empty to avoid processing this folder
rem again when not being recreated again in the meantime.

for /D %%# in ("%SourcePath%\*") do (
    if exist "%%#\defects\*" (
        %SystemRoot%\System32\xcopy.exe "%%#\defects\*" "%BackupPath%\%YearMonthDay%\%%~nx#_defects\" /H /I /K /Q /R /S /Y >nul
        rd /Q /S "%%#\defects"
    )
)
endlocal
@ECHO OFF
SETLOCAL EnableExtensions
rem obtain %_currentdate% variable in locale independent yyyymmdd format
for /f "tokens=2 delims==" %%G in (
  'wmic OS get localdatetime /value^|find "="') do set "_currentdate=%%G"
set "_currentdate=%_currentdate:~0,8%"
rem show %_currentdate% variable for debugging purposes
set _currentdate

rem appoint source and target directories
set "_sourcedir=c:\machines\models"
set "_targetdir=E:\backup\%_currentdate%"

for /F "delims=" %%G in ('dir /B /AD "%_sourcedir%\"') do (
  set "_notEmpty="
  rem check if `defects` folder exists 
  if exist "%_sourcedir%\%%G\defects\*" (
    rem credit and tribute to DBenham in https://stackoverflow.com/a/10818854/3439404
    rem check if a folder contains at least one file 
    >nul 2>nul dir /a-d "%_sourcedir%\%%G\defects\*" && (set "_notEmpty=f")
    rem check if a folder or any of its descendents contain at least one file
    >nul 2>nul dir /a-d /s "%_sourcedir%\%%G\defects\*" && (set "_notEmpty=f")

    if defined _notEmpty (
        rem create target directory silently; suppress error message 'already exists'
        md "%_targetdir%\%%G_defects\" >NUL 2>&1

        rem move content of %_sourcedir%\%%G\defects\* to %_targetdir%\%%G_defects\
        robocopy "%_sourcedir%\%%G\defects" "%_targetdir%\%%G_defects" /S /E /MOVE /L

        rem recreate source directory deleted (moved) by robocopy 
        md "%_sourcedir%\%%G\defects\" >NUL 2>&1
    ) else (
        echo nothing to do in %_sourcedir%\%%G\defects
    )
  ) else (
      echo nothing to do in %_sourcedir%\%%G
  )
)