batch-file - 如何从批处理文件中的文件夹名称中提取和使用信息(值、字符串)...
问题描述
我想创建一个批处理文件来组织我的文件夹。这基本上是我第一次编写任何代码,对于错误感到抱歉。
文件夹名称由整数和文本组成,如下所示:“C:\bad folder\22my folder\”
在这种情况下,我想使用文件夹名称的文本和按字母顺序排列的整数文件创建新文件夹。在这里,它将创建文件夹:
C:\完成的文件夹\我的文件夹
仅包含文件
C:\finished folder\my folder\22.txt(文件夹内第22个文件)
这是我到目前为止所做的,但这绝对是可怕的,我会尽力解决它:
MKDIR C:\cleaned folders\
CD C:\bad folders\
::I will try and make a loop for all the folders in "C:\bad folders\"
set oldfoldername=%CD% ::the whole folder name (number+text)
set newfoldername= ::the text in the folder name
set number= ::the number in the folder name
mkdir C:\cleaned folders\newfoldername
CD ..
copy C:\bad folders\oldfoldername C:\cleaned folders\newfoldername
CD C:\cleaned folders\newfoldername
::loop maybe for all the files in "C:\cleaned folders\newfoldername"
IF (filerank) neq %number% DEL (filerank)
那么,如何从文件夹名称中获取这些信息并使用它呢?先感谢您。
解决方案
@ECHO OFF
SETLOCAL
:: make destination directory - note quotes as md x y will create directories x and y
MKDIR "U:\cleaned folders\"
:: Read each directoryname in turn from ...\bad folders\ and assign to %%a
:: Examine a dir listing in bare format (/b) of directories (/ad).
FOR /f "delims=" %%a IN ('dir /ad /b "u:\sourcedir\bad folders"') DO (
rem Note REM not :: within a code block (parenthesised sequence of lines)
rem set oldfoldername to the name found
SET "oldfoldername=%%a"
rem partition into newfoldername and number
CALL :split
REM create new destination directory and MOVE file
CALL :cre8move
)
GOTO :EOF
:split
:: initialise destination components
SET "number="
SET "newfoldername=%oldfoldername%"
:splitlp
:: See whether the first character of newfoldername is in the string 0..9
ECHO 0123456789|FIND "%newfoldername:~0,1%">NUL
IF ERRORLEVEL 1 GOTO :EOF
:: first character is numeric - accumulate and remove
SET "number=%number%%newfoldername:~0,1%"
SET "newfoldername=%newfoldername:~1%"
GOTO splitlp
:cre8move
:: create new destination directory
MD "U:\cleaned folders\%newfoldername%"
:: we need to skip (number - 1) lines, so calculate
SET /a skiplines=%number% - 1
:: Read the directorylist (not including directorynames /a-d), skipping number-1 names
:: Move that file (You may want to COPY) and then terminate the loop
FOR /f "skip=%skiplines%delims=" %%q IN ('dir /a-d /b "u:\sourcedir\bad folders\%oldfoldername%\*"') DO (
ECHO MOVE "u:\sourcedir\bad folders\%oldfoldername%\%%q" "U:\cleaned folders\%newfoldername%"&GOTO :eof
)
GOTO :eof
我相信以上内容应该可以满足您的奇怪要求。我希望通过评论的方式叙述是有用的。
您没有说是要移动还是复制文件。我刚刚echo
编辑了所需的行。根据需要进行更改。
请注意,这不适用于名为 where number
is 0 或 1 的目录。如果需要,修复这些值并不复杂,但这里介绍的其他值的方法演示似乎是这里的对象。
Batch 也有关于前导 0 的有趣想法。如果您需要保留前导 0,那么这会增加一些复杂性。
批处理语法需要一点时间来适应。看似微小的更改可能是灾难性的,因此复制并粘贴到文本编辑器可能是最好的方法。不要使用文字处理器,因为他们习惯于重新格式化文本以使其看起来合乎逻辑。
请注意,当尝试创建已存在的目录时,将显示错误消息。这是无害但丑陋的。2>nul
您可以通过附加到每一MD ...
行来抑制错误消息。
SET
批处理对语句中的空格很敏感。SET FLAG = N
将名为“FLAG Space”的变量设置为值“ SpaceN”
语法SET "var=value"
(其中值可能为空)用于确保分配的值中不包含任何杂散的尾随空格。
推荐阅读
- php - 如何从 Goutte 获取响应状态代码
- html - 将coldfusion / html页面呈现为docx扩展而不是doc扩展时出现问题
- python - 在 Python 中使用递归时出现 UnboundLocalError?
- r - 使用 R 中的模板字符串将因子转换为字符串
- visual-studio-code - VSCode 多语言扩展不加载第二语言的语法
- mongoose - 猫鼬 findByIdUpdate 已弃用
- python - 在类实例化时返回不同的类型?
- android - AndroidX 偏好库 DropDownPreferences 不适用于 API < 24
- python-3.x - UDATE:如何将一组值重复分配给时间序列?
- concourse - 如何从另一个资源的目录执行 Concourse 任务脚本?