2017-09-14 254 views
0

我目前正在製作一個用於刪除多個用戶下載的批處理文件。例如(ccomley.V2),我被困在獲取擴展名的文件夾中。如何刪除帶擴展名的多個文件夾? (BATCH FILE)

大約有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 

希望你能幫幫我! (附註。我曾嘗試做*,

(無法顯示它在有私人信息的整批文件。)

回答

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之後,然後執行計算以生成報告字段。請注意,由於子過程中的setlocal而不是會影響主程序中的startend次。

末,只需使用for /d\\SRV-FILES01\IJ_StudentsW$\Profiles目錄下生成要刪除的名稱,並通過call

for /d %%x in ("\\SRV-FILES01\IJ_StudentsW$\Profiles\*") do call zap1user "%%x" 

,並全部完成傳遞這些名字的子程序!

(當然 - 你也可以將你的經過時間程序變成另一個輔助批次,它提供兩個參數,開始和結束時間,並計算經過時間,毫無疑問它會在別處有用......)

+0

謝謝,沒意識到它會那麼簡單! –

相關問題