如何通过Windows cmd在其名称中操作带点的文件?

时间:2017-02-06 10:40:14

标签: batch-file cmd wildcard

例如

E:\2012.12.25  NO.23  Peter\Dailylog.txt
E:\2012.12.27  NO.12  John\log.txt
E:\2012.12.29  NO.25  Amy\log.txt
E:\2012.1.1  NO.23  Peter\bug .txt
E:\2013.1.2  NO.12  John\recoder.xml
E:\2012.1.3  NO.12  John\log.txt

然后,我想把所有John的文件夹放到一个文件夹中; 像这样

E:\John\2012.12.27  NO.12  John\log.txt
E:\John\2013.1.2  NO.12  John\recoder.xml
E:\John\2012.1.3  NO.12  John\log.txt

我试过了

move "E:\*John" E:\John

但失败了。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

与目录名一起使用的命令 MOVE 不会移动目录,而是重命名目录。

下面的批处理代码为输入示例生成输出:

E:\Amy\2012.12.29  NO.25  Amy\log.txt
E:\John\2012.12.27  NO.12  John\log.txt
E:\John\2013.1.2  NO.12  John\recoder.xml
E:\John\2012.1.3  NO.12  John\log.txt
E:\Peter\2012.12.25  NO.23  Peter\Dailylog.txt
E:\Peter\2012.1.1  NO.23  Peter\bug .txt

这是批处理代码,顶部的主代码和底部的子程序之间有注释行,用于文件夹移动。

@echo off
for /F "delims=" %%I in ('dir /AD /B E:\*.*.*NO.* 2^>nul') do call :CheckFolder "E:\%%I"
goto :EOF

rem The subroutine CheckFolder first splits up each folder name passed
rem to the subroutine into strings using dot and space as delimiters.

rem Of interest is just the sixth string which is assigned to loop
rem variable B which can contain itself dots and spaces. The first
rem 5 strings (year, month, day, NO and number) are not of interest
rem for determining the target folder name for the current folder.

rem A folder which can't be split up into at least 6 strings is ignored
rem for further processing as this is most likely one of the target folders.

rem The target folder is next created if not already existing. If creation
rem of target folder fails, the current folder with files to move is ignored.

rem Next all files in current folder are moved to the target folder and
rem the current folder is removed which is only successful if the current
rem folder contains now no files or subfolders.

:CheckFolder
set "FolderName="
for /F "tokens=5* delims=. " %%A in ("%~nx1") do set "FolderName=%%B"
if "%FolderName%" == "" goto :EOF
if exist "%~dp1%FolderName%\%~nx1" goto MoveFolder
md "%~dp1%FolderName%\%~nx1"
if errorlevel 1 goto :EOF

:MoveFolder
move /Y "%~1\*" "%~dp1%FolderName%\%~nx1\" >nul
rd "%~1"
goto :EOF
根据有关E:\2012.12.29 NO.25 Amy(later)\log.txt的评论,

修改

在处理文件夹名称2012.12.29 NO.25 Amy(later)命令行

for /F "tokens=5* delims=. " %%A in ("%~nx1") do set "FolderName=%%B"

将忽略的循环变量A分配给字符串25,并将循环变量B分配给在环境变量Amy(later)旁边分配的字符串FolderName。 / p>

要从此文件夹名称中获取Amy,必须将此代码行更改为

for /F tokens^=6^ delims^=^(.^  %%A in ("%~nx1") do set "FolderName=%%A"

现在,一个左括号也被解释为字符串分隔符,如点和空格,只有字符串6 Amy被分配给循环变量A,它被分配在环境变量{{1}旁边}}

这里需要看起来奇怪的选项语法,因为FolderName对于Windows命令解释器具有特殊含义,因此不能轻易地将其指定为字符串分隔符作为文字字符。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • (
  • call /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • md /?
  • move /?
  • rd /?
  • rem /?

有关set /?(禁止标准输出)和>nul(禁止错误输出)的说明,请参阅Microsoft文章Using command redirection operators,以便重定向运算符2>nul被转义带有插入符>的{​​{1}}在执行命令 DIR 时被解释为重定向操作符,而不是在解析 FOR 命令行时。