所以,当我运行以下脚本时,我得到了“)此时出乎意料。”因为它击中了第一个“for”声明。
该脚本旨在纠正“数据库”中文件夹的编号结构,数字从最新到最早(从0开始)。 “数据库”中的文件夹结构只需要与以下内容类似地命名:“1 - 2_12_2017”或“34 - 1_14_2017”。
关于我做错了什么的想法?
@echo off
setlocal enabledelayedexpansion
set "dbpth=C:\database"
set cnt=-1
cd %dbpth%
set "fy=unset"
set "fm=unset"
set "fd=unset"
for /f "delims=" %%A in ('dir /b /a:d /o:-n "%dbpth%\*"') do (
for /f "delims=1234567890-_ " %%B in ("%%A") do (if not "%%B" == "" goto skp)
set "str=%%A"
set "str=!str:~-10!"
set "str=!str: =!"
set "str=!str:-=!"
set "ys=unset"
set "ms=unset"
set "ds=unset"
for /f "tokens=1-3 delims=_" %%F in ("!str!") do (
if !ms! == unset (set "ms=%%F")
if !ds! == unset (set "ds=%%G")
if !ys! == unset (set "ys=%%H")
)
if !fy! == unset (
set "fy=!ys!"
set "fd=!ds!"
set "fm=!ms!"
)
if not !fy! == unset (
if !ys! leq !fy! (
set "fy=!ys!"
if !ms! leq !fm! (
set "fm=!ms!"
if !ds! leq !fd! (set "fd=!ds!")
)
)
)
set /a cnt=!cnt!+1
:skp
)
:loop
for /f "delims=" %%G in ('dir /b /a:d /o:-n "%dbpth%\*"') do (
set "str=%%G"
set "str=!str:~-10!"
set "str=!str: =!"
set "str=!str:-=!"
if !str! == !fm!_!fd!_!fy! (
ren "%dbpth%\%%G" "!cnt! - !fm!_!fd!_!fy!"
set /a cnt=!cnt!-1
)
)
set /a fd=!fd!+1
if !fd! gtr 31 (
set /a fm=!fm!+1
set fd=1
if !fm! gtr 12 (
set /a fy=!fy!+1
set fm=1
)
)
title !cnt! - !fm!_!fd!_!fy!
if !cnt! geq 0 (goto loop)
endlocal
exit