基于部分文件夹名称增加文件夹名称

时间:2020-05-13 20:57:32

标签: batch-file numbers

我有一堆带有时间戳的文件夹。

  • 001_FDV__24-04-2020_2152
  • 002_FDV__27-04-2020_2202
  • 003_FDV__29-04-2020_2209
  • 004_FDV__01-05-2020_2240

以上所有文件都位于c:\video_production

这是我目前拥有的代码。

For /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)

REM
set mydir=FDV_%mydate%_%mytime%
mkdir %mydir%
move *.* %mydir%

我想从c:\video_production中的最后一个开始增加文件夹编号。我会有005、006、007等吗?

谢谢

1 个答案:

答案 0 :(得分:1)

我想您想从所有文件夹名称的前3位中找到最大的零填充3位数字。

未体验

rem the dir listing of folders only sorted alphabetically in reverse, the first folder listed will be the "last" one
for /f %%a in ('dir /ad /o-n /b "c:\video_production"') do (
   set "lastfolder=%%~a"
   goto :continue
)

:continue
rem At this point, lastfolder should be the "last" folder name.
rem Get the first 3 digits.  Change to 4 for 4 digits or 5 for 5 etc.
set lastnumber=%lastfolder:~0,3%

rem Now lastnumber should be the zero padded number.
rem There are a variety of ways to increment this number.
rem I prefer to remove the zero padding, increment it, and then zero pad it again.

rem There are many ways to remove the zero padding.
rem I like to manipulate the string and then use modulo to get the number.
rem We can also increment it in one statement with this method.

rem Use 10000 for 4 digits, 100000 for 5 digits, etc.
set /a nextnumber = 1%lastnumber% %% 1000 + 1

rem Now we pad it with zeros.
rem Use 000 for 4 digits, 0000 for 5 digits, etc.
set nextnumber=00%nextnumber%
rem and trim it to 3 digits.
rem use 4 for 4 digits, 5 for 5 etc.
set nextnumber=%nextnumber:~-3%

rem And you should be good to go:
set mydir=%nextnumber%_FDV_%mydate%_%mytime%

希望文件夹号必须是准确的数字。拥有未知数量的数字和未知数量的填充零数字更为复杂

相关问题