我无法在第一个for循环中使用第二个参数(%%b
)在for第二个循环中进行比较(if !count! GTR %%b
),希望专家提供帮助,对不起我的英语不好
文件text1.txt
# #
#Employee*.PDF,3
School*.PDF,4
Family*.PDF,5
文件夹c:\user\text
xxxxxxxxx.pdf
Employee1.pdf
Employee3.pdf
Employee2.pdf
Employee4.pdf
Employee5.pdf
Employee6.pdf
Employee7.pdf
School1.pdf
School3.pdf
School2.pdf
School4.pdf
School5.pdf
School6.pdf
School7.pdf
总代码:
@Echo off
@SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
for /f %%x in ('dir C:\user\text\%%a /b') do (
set /a count+=1
if !count! GTR %%b (
del "C:\user\text\%%x"
)
)
)
我不能在第二个for循环中使用%%b
。
运行All files are deleted
后输出。
但是我只想删除:
Employee4.pdf
Employee5.pdf
Employee6.pdf
Employee7.pdf
School5.pdf
School6.pdf
School7.pdf
请帮帮我!
我尝试...添加set /a flag=%%b
并更改比较条件= if !count! GTR !flag!
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
set /a flag=%%b
for /f %%x in ('dir C:\user\text\%%a /b') do (
set /a count+=1
if !count! GTR !flag! (
del "C:\user\text\%%x"
)
)
)
但是输出!flag! = 5
,
等于其最终值Family*.PDF,5
。
答案 0 :(得分:2)
您的第一个代码似乎运行良好(我添加了一些代码来添加演示文件,并在dir
命令中进行了较小的更改以匹配环境-适应您的需求):
@echo off
setlocal enabledelayedexpansion
REM create test environment:
for /l %%a in (1,1,7) do break>School%%a.pdf
for /l %%a in (1,1,7) do break>Family%%a.pdf
dir /b *.pdf
echo ---------
type text1.txt
echo ---------
pause
for /f "delims=, tokens=1,2 eol=#" %%a in ('Type text1.txt') do (
set /a count=0
for /f %%x in ('dir %~dp0%%a /b') do (
set /a count+=1
if !count! GEQ %%b echo "%%~fx"
)
)
输出:
Family1.pdf
Family2.pdf
Family3.pdf
Family4.pdf
Family5.pdf
Family6.pdf
Family7.pdf
School1.pdf
School2.pdf
School3.pdf
School4.pdf
School5.pdf
School6.pdf
School7.pdf
---------
# #
#Employee*.PDF,3
School*.PDF,4
Family*.PDF,5
---------
Drücken Sie eine beliebige Taste . . .
"D:\tmp\test\School4.pdf"
"D:\tmp\test\School5.pdf"
"D:\tmp\test\School6.pdf"
"D:\tmp\test\School7.pdf"
"D:\tmp\test\Family5.pdf"
"D:\tmp\test\Family6.pdf"
"D:\tmp\test\Family7.pdf"
是的-我将GTR
更改为GEQ
以匹配您的示例。
答案 1 :(得分:1)
dir命令在%%a
之后需要通配符
for /f %%x in ('dir /b "C:\user\text\%%a*"') do (
从%%b
读取的text1.txt
不仅是数字,而且附加了扩展名.pdf
。
可以使用
if !count! GTR %%~nb
删除扩展名,或test1.txt
,使数字和扩展名之间使用另一个逗号。