如何删除带扩展名的多个文件夹? (批量文件)

时间:2017-09-14 09:01:45

标签: batch-file

我目前正在制作一个删除多个用户下载的批处理文件。我坚持要获得一个带有扩展名的文件夹,例如(ccomley.V2)。

大约有172个配置文件,我知道我可以在不同的命令行中完成所有这些配置文件,但是有更简单的方法可以在一行中删除它们吗?

当前行批处理文件

color 2
del "C:\logs\student_deletion.log"
cls
@echo off
@setlocal

set start=%time%

:: Runs your command
cmd /c %*

set end=%time%
set options="tokens=1-4 delims=:.,"
for /f %options% %%a in ("%start%") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in ("%end%") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 100

set /a hours=%end_h%-%start_h%
set /a mins=%end_m%-%start_m%
set /a secs=%end_s%-%start_s%
set /a ms=%end_ms%-%start_ms%
if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
if %hours% lss 0 set /a hours = 24%hours%
if 1%ms% lss 100 set ms=0%ms%

:: Mission accomplished
set /a totalsecs = %hours%*3600 + %mins%*60 + %secs%

cd /d "c:\users"
echo Date: %date% Time: %time% >> C:\logs\student_deletion.log
echo. >> C:\logs\student_deletion.log

dir \\SRV-FILES01\IJ_StudentsW$\Profiles\ccomley.V2\downloads >> C:\logs\student_deletion.log
for /d %%a in (*) do rd /s /q "\\SRV-FILES01\IJ_StudentsW$\Profiles\ccomley.V2\downloads" >>nul 2>>&1
@echo Successfully deleted ccomley downloads! (%totalsecs%.%ms%s total) to delete their downloads
echo ccomley took, (%totalsecs%.%ms%s total) to delete their downloads >> C:\logs\student_deletion.log
echo. >> C:\logs\student_deletion.log

dir \\SRV-FILES01\IJ_StudentsW$\Profiles\anotheraccount.V2\downloads >> C:\logs\student_deletion.log
for /d %%a in (*) do rd /s /q "\\SRV-FILES01\IJ_StudentsW$\Profiles\anotheraccount.V2\downloads" >>nul 2>>&1
@echo Successfully deleted anotheraccount downloads! (%totalsecs%.%ms%s total) to delete their downloads
echo anotheraccount took, (%totalsecs%.%ms%s total) to delete their downloads >> C:\logs\student_deletion.log
echo. >> C:\logs\student_deletion.log
希望你能帮助我! (旁注。我试过做*,,)

(无法显示整个批处理文件,其中包含私人信息。)

1 个答案:

答案 0 :(得分:0)

dir \\SRV-FILES01\IJ_StudentsW$\Profiles\ccomley.V2\downloads >> C:\logs\student_deletion.log
for /d %%a in (*) do rd /s /q "\\SRV-FILES01\IJ_StudentsW$\Profiles\ccomley.V2\downloads" >>nul 2>>&1
@echo Successfully deleted ccomley downloads! (%totalsecs%.%ms%s total) to delete their downloads
echo ccomley took, (%totalsecs%.%ms%s total) to delete their downloads >> C:\logs\student_deletion.log
echo. >> C:\logs\student_deletion.log

需要重复运行此块,并将固定文本ccomley[.v2]替换为变量。

简单的方法是将call作为子例程,将所需的名称作为参数传递。

:sub
setlocal
set "name=%%~n1"&set "ext=%%~x1"
:: if no ext, abandon ship.
if not defined ext goto :eof
:: otherwise, execute the block with `%~1`, `%name%` and `%ext%' as required.

这可以是一个内部子程序,或者最好是一个独立的批处理,这样你就可以执行zap1user fred.bloggs来删除用户fred.bloggs的文件。

下一步是在子程序中包含时序。在setlocal之后,设置start并在for...rd...设置end之后,然后执行计算以生成报告字段。请注意,由于子过程中的start会影响主过程中的endsetlocal次。

最后,只需在for /d目录上使用\\SRV-FILES01\IJ_StudentsW$\Profiles生成要删除的名称,并通过call

将这些名称传递给子程序
for /d %%x in ("\\SRV-FILES01\IJ_StudentsW$\Profiles\*") do call zap1user "%%x"

全部完成!

(好吧 - 你也可以将你的经过时间例程变成另一个辅助批次,它提供两个参数,开始和停止时间,并计算经过的时间。毫无疑问,它会在其他地方有用......)< / p>