我需要在windows中创建一个批处理文件,以解析文件夹中所有可用的文件名,并将符合某些条件的文件移动到文件夹中。我已经使用FOR命令创建了循环周期,并将文件名设置为一个变量。现在我需要检查文件名的变量中有多少个"-“连字符;如果这个字符出现的次数超过10个,那么文件就不应该被移动。此外,我还需要检查文件名中是否有空格:同样,在这种情况下,文件不能移动。请注意:我可以用哪种方式创建脚本来检查变量中假设的出现次数和空格字符的存在?
非常感谢
发布于 2015-06-30 14:53:12
这段代码只是使用for /f标记器来确定是否应该复制或排除该文件。在假设的情况下,10个-字符生成11个令牌,或多或少是排除的原因。在空格的情况下,单个空格生成两个标记。这是最简单的部分,但是
可能会出现
for /f记号赋予器仅将连续的分隔符作为一个分隔符处理,每个假设都替换为-#,文件名以#为前缀以确保正确拆分。for可替换参数。因此,我们需要延迟扩展才能读取更改后的变量的内容。但是,当延迟扩展处于活动状态时,!字符将成为一个问题,因为解析器将尝试将其解释为变量引用。为避免此问题,仅在需要时才启用延迟扩展,并再次禁用延迟扩展。#为前缀和后缀,以确保正确处理开头或结尾的空格。@echo off
setlocal enableextensions disabledelayedexpansion
rem Generate a set of test files
type nul > "0-1-2-3-4-5-6-7-8-9-10-11-12.testFile"
type nul > "0-1-2-3-4-5-6-7-8-9-10-11.testFile"
type nul > "0-1-2-3-4-5-6-7-8-9-10.testFile"
type nul > "0-1-2-3-4-5-6 -7-8-9-10.testFile"
type nul > "0-1-2-3-4-5-6-7-8-9.testFile"
type nul > "-----------.testFile"
type nul > "----------.testFile"
type nul > "!-----------!.testFile"
type nul > "!----------!.testFile"
type nul > "noSpaces.testFile"
type nul > "this file has spaces.testFile"
type nul > "0-1-2-3-4-5!!-6-!!7-8-9-10-11-12.testFile"
type nul > "0-1-2-3-4-5!!-6-!!7-8-9-10-11.testFile"
type nul > "0-1-2-3!!-4-5-6-7-8-9!!-10.testFile"
type nul > "0-1-2-3!!-4-5-6-7-8!-!9.testFile"
type nul > "no!Sp!aces.testFile"
type nul > "this !file! has spaces.testFile"
for %%f in (*) do (
rem The default behaviour is not to exclude the file
set "exclude="
rem Test the hypen conditions
set "fileName=%%~nf"
setlocal enabledelayedexpansion
for /f "tokens=1,11,12 delims=-" %%a in ("#!fileName:-=-#!") do (
endlocal
rem Testing less than 10 hypen
if "%%~b"=="" set "exclude=1"
rem Testing more than 10 hypen
if not "%%~c"=="" set "exclude=1"
)
rem Test if the file name contains a space
for /f "tokens=1,2" %%a in ("#%%~nf#") do if not "%%~b"=="" set "exclude=1"
rem Now we know what to do with the file
if defined exclude (
echo EXCLUDE "%%~ff"
) else (
echo COPY "%%~ff"
)
)
del /q "*.testFile"另一种方法是过滤文件列表,以便只检索应该处理的文件
for /f "delims=" %%a in ('
dir /b
^| findstr /v /c:" "
^| findstr /r /x /c:"[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*-[^-]*"
') do echo COPY %%~fa发布于 2015-06-30 01:25:25
确定字符串中包含的连字符数量的一种简单方法是将原始字符串的长度与删除连字符的字符串的长度进行比较:连字符的数量是长度的差值。
1. Save the length of the original string
2. Remove all hyphens from the string
3. Compare the length of the new string to that of the original:
originalLength - newLength = numberOfHyphens类似地,此方法可用于确定空间是否存在。如果原始字符串的长度大于字符串的长度减去空格,则必须删除空格,因此必须在原始字符串中存在空格。
发布于 2015-06-30 03:34:51
如果我没理解错的话,你不会真的想知道一个名字有多少个假设,而只是想知道这个名称是否有超过10个假设。这可以通过for /F命令的"tokens=12 delims=-"选项轻松实现:
EDIT:根据注释中声明的新请求修改的代码。新的代码检查名称是否恰好包含10个连字符,并且不包含空格。
@echo off
setlocal
for %%a in (one-two-three-four-five-six-seven-eight-nine-end.txt
one-two-three-four-five-six-seven-eight-nine-ten-end.txt
one-two-three-four-five-six--eight-nine-ten-end.txt
one-two-three-four-five---eight-nine-ten-end.txt
one-two-three-four-five----nine-ten-end.txt
one-two-three-four-five-----ten-end.txt
one----------end.txt
----------.txt
one-two-three-four-five-six-seven-eight-nine-ten-eleven-end.txt
"one-two-three-four-five-SIX SEVEN--eight-nine-ten-end.txt") do (
set "fileName=%%~a"
call :checkName
)
goto :EOF
:checkName
echo Checking "%fileName%"
set "result=Don't have 10 hypens"
for /F "tokens=11,12 delims=-" %%a in ("%filename:--=X-X-X%") do (
if "%%a" neq "" if "%%b" equ "" set "result=Name correct"
)
for /F "tokens=2" %%a in ("%fileName%") do if "%%a" neq "" set "result=Include space"
echo %result%
exit /Bhttps://stackoverflow.com/questions/31121583
复制相似问题