Windowscmd如何操作文件名中带点的文件?
How to operate file with dots in its name by Windows cmd?
例如
E:12.12.25 NO.23 Peter\Dailylog.txt
E:12.12.27 NO.12 John\log.txt
E:12.12.29 NO.25 Amy\log.txt
E:12.1.1 NO.23 Peter\bug .txt
E:13.1.2 NO.12 John\recoder.xml
E:12.1.3 NO.12 John\log.txt
然后,我想把约翰的所有文件夹都放到一个文件夹里;
像这样
E:\John12.12.27 NO.12 John\log.txt
E:\John13.1.2 NO.12 John\recoder.xml
E:\John12.1.3 NO.12 John\log.txt
我试过了
move "E:\*John" E:\John
但失败了。
如有任何帮助,我们将不胜感激。
与目录名称一起使用的命令MOVE不会移动目录,而是重命名目录。
下面的批处理代码为输入示例生成输出:
E:\Amy12.12.29 NO.25 Amy\log.txt
E:\John12.12.27 NO.12 John\log.txt
E:\John13.1.2 NO.12 John\recoder.xml
E:\John12.1.3 NO.12 John\log.txt
E:\Peter12.12.25 NO.23 Peter\Dailylog.txt
E:\Peter12.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:12.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"
将字符串 25
分配给忽略的循环变量 A
,将字符串 B
分配给环境变量 FolderName
旁边分配的字符串 Amy(later)
.
要从此文件夹名称中获取 Amy
,需要将此代码行更改为
for /F tokens^=6^ delims^=^(.^ %%A in ("%~nx1") do set "FolderName=%%A"
现在左括号也被解释为字符串定界符,如点和 space 并且只有字符串 6 Amy
被分配给循环变量 A
,这是下一个分配的到环境变量 FolderName
.
这里需要选项的奇怪语法,因为 (
对 Windows 命令解释器有特殊含义,因此不能轻易将字符串定界符指定为文字字符。
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
call /?
dir /?
echo /?
for /?
goto /?
if /?
md /?
move /?
rd /?
rem /?
set /?
另请参阅 Microsoft 文章 Using command redirection operators 以了解 >nul
(抑制标准输出)和 2>nul
(抑制错误输出)的解释,重定向运算符 >
是使用脱字符 ^
在 2>nul
上转义,在执行命令 DIR 时被解释为重定向运算符,而不是在解析 FOR命令行。
例如
E:12.12.25 NO.23 Peter\Dailylog.txt
E:12.12.27 NO.12 John\log.txt
E:12.12.29 NO.25 Amy\log.txt
E:12.1.1 NO.23 Peter\bug .txt
E:13.1.2 NO.12 John\recoder.xml
E:12.1.3 NO.12 John\log.txt
然后,我想把约翰的所有文件夹都放到一个文件夹里; 像这样
E:\John12.12.27 NO.12 John\log.txt
E:\John13.1.2 NO.12 John\recoder.xml
E:\John12.1.3 NO.12 John\log.txt
我试过了
move "E:\*John" E:\John
但失败了。
如有任何帮助,我们将不胜感激。
与目录名称一起使用的命令MOVE不会移动目录,而是重命名目录。
下面的批处理代码为输入示例生成输出:
E:\Amy12.12.29 NO.25 Amy\log.txt
E:\John12.12.27 NO.12 John\log.txt
E:\John13.1.2 NO.12 John\recoder.xml
E:\John12.1.3 NO.12 John\log.txt
E:\Peter12.12.25 NO.23 Peter\Dailylog.txt
E:\Peter12.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:12.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"
将字符串 25
分配给忽略的循环变量 A
,将字符串 B
分配给环境变量 FolderName
旁边分配的字符串 Amy(later)
.
要从此文件夹名称中获取 Amy
,需要将此代码行更改为
for /F tokens^=6^ delims^=^(.^ %%A in ("%~nx1") do set "FolderName=%%A"
现在左括号也被解释为字符串定界符,如点和 space 并且只有字符串 6 Amy
被分配给循环变量 A
,这是下一个分配的到环境变量 FolderName
.
这里需要选项的奇怪语法,因为 (
对 Windows 命令解释器有特殊含义,因此不能轻易将字符串定界符指定为文字字符。
要了解使用的命令及其工作原理,请打开命令提示符 window,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
call /?
dir /?
echo /?
for /?
goto /?
if /?
md /?
move /?
rd /?
rem /?
set /?
另请参阅 Microsoft 文章 Using command redirection operators 以了解 >nul
(抑制标准输出)和 2>nul
(抑制错误输出)的解释,重定向运算符 >
是使用脱字符 ^
在 2>nul
上转义,在执行命令 DIR 时被解释为重定向运算符,而不是在解析 FOR命令行。