batch-file - Windows 批处理脚本:查找目录中最后一个文件夹的名称
问题描述
我正在编写一个批处理文件以将文件夹的内容复制到具有新名称的同一目录中的文件夹中。我希望命名约定为: 2021000-Template (这是从中复制文件的位置) 2021001-CompanyA 2021002-CompanyB 等
我提示输入公司名称,但被困在最后一部分。
如何在目录中找到最后一个文件夹的名称,将 202100x 存储在变量中并添加 1?
之后我需要将新变量包含在带有 cname 的字符串中,但我想我可以弄清楚。
谢谢你的帮助!
@echo off
:: CD to correct folder and year in the root
CD\Estimates\2021
:: Asks for user input on company name and stores it in the cname var
Set /P "cname= Enter Company Name (without spaces): "
:: Copying the entire contents of the Template folder to a new folder named the cname
XCOPY C:\Estimates\2021\2021000-Template C:\Estimates\2021\%cname% /S /I
:: Opens the new folder
start C:\Estimates\2021\%cname%
解决方案
for /f "delims=-" %%a in ('dir /b /ad /on C:\Estimates\2021\2021*-*') do set /a last=%%a+1
echo next subdirectory to be created is "%last% - %cname%"
应该做这项工作,假设没有子目录名称以不符合您的模式开始2021
并且名称中包含 a 。-
该命令以名称顺序 ( ) 的基本形式(仅名称)生成与模式“2021 - ”匹配的dir
目录 ( ) 列表。(这是隐式的)依次选择每个目录名中的第一个标记,由so分隔,将被分配, ,依此类推 - 按此顺序。将数字加 1,因此是序列中的下一个数字。以这种方式,当循环结束时选择下一个可用的数字,即使数字由于某种原因已被删除。/ad
/on
something
somethingelse
tokens=1
-
%%a
2021000
2021001
2021002
set /a
for
推荐阅读
- android - 如何在Android中左对齐到居中的文本
- sql - 根据收到的表中的数据
- python - 将指数曲线拟合到数据点以计算衰减率
- javascript - 在数组中移动对象(Javascript)
- sharepoint - 无法获得超过 50 项的响应
- python - 基于另一个布尔列使用 Django-Tables2 格式化列
- javascript - 我在尝试使用道具时遇到打字错误,有没有办法解决这个问题?
- django - 我在 django 视图中的 success_url 中放了什么。现在,这不是保存编辑的表单,也不是重定向页面
- sql - 复合主键中的 NULL 值
- speech-to-text - 如何使用创建自定义类来格式化 Google Speech-to-Text api 中的电话号码?