所以我試圖制作一個腳本,其中批處理檔案搜索某個目錄,它的檔案夾和子檔案夾,以查找重復的檔案。然后它還應該識別最舊的檔案(很可能是原始檔案)和最新的檔案(可能是用戶當前正在處理的檔案),并詢問用戶是否要從所有檔案中洗掉每個檔案它已識別(即使是最舊和最新的,但是當它們被洗掉時,它應該將它們識別為最舊和最新)我對這種腳本很陌生,所以請原諒我犯的愚蠢錯誤。在這種情況下,我制作了一個“Giorgio”Word 檔案并將其放置在一些子檔案夾中,其中一個我也更改了所寫的內容并且很糟糕
這是我嘗試處理的部分,即關于識別最舊和最新的檔案。我相信我試圖做一些我不允許的事情,我使用這些命令是錯誤的
for /f "delims=" %%a in (
"""dir /a-d /b /tw /od "C:\Desktop\Batch testes\Teste 1" /Giorgio.docx"
) do set %file%=="oldest"
echo %oldest%
for /f "delims=" %%a in (
"""dir /a-d /b /tw /o-d "C:\Desktop\Batch testes\Teste 1 /*Giorgio.docx""
) do set %file%=="newest"
echo %newest%
如果你們好奇,這就是我的整個程式(對不起,如果有些句子寫得不好,我在過去 3 分鐘內將所有內容從葡萄牙語翻譯成英語)
@echo off
setlocal EnableDelayedExpansion
set "source_folder=C:\Desktop\Batch testes\Teste 1\Giorgio.docx"
echo Welcome to this Practice/Test Program
echo This program has the obective of finding duplicated files.
pause
echo The user will have the chance to eliminate these files if he wishes
echo There will be the identification of the oldest and the newest file
pause
echo Let us start...
pause
:aPrompt
cls
echo ========================================
echo What do you wish to do?
echo 1.Show only the duplicated files. (Clique '1')
echo 2.Show the duplicated files and eliminate them. (Clique '2')
echo 3.Exit the program (Clique '3')
choice /n /c:123 /M "Escolha uma opcao "%1
goto Alfa-%errorlevel%
:Alfa-1 A1
echo Showing the files being analysed
forfiles /P "C:\Desktop\Batch testes\Teste 1" /M Giorgio.docx /S /C "cmd /c echo @file"
echo Showing the paths of the files being analysed
forfiles /P "C:\Desktop\Batch testes\Teste 1" /M Giorgio.docx /S /C "cmd /c echo @relpath@fname"
pause
echo ===============================================================================
for /f "delims=" %%a in (
"""dir /a-d /b /tw /od "C:\Users\11506018\Desktop\Batch testes\Teste 1" /Giorgio.docx"
) do set %file%=="oldest"
echo %oldest%
for /f "delims=" %%a in (
"""dir /a-d /b /tw /o-d "C:\Desktop\Batch testes\Teste 1 /*Giorgio.docx""
) do set %file%=="newest"
echo %newest%
echo=======================
echo=======================
echo=======================
pause
:bPrompt
echo Here are the duplicated files
echo Do you wish to leave or go back? (Type '1' to end the program, '2' to return to the previous menu)
choice /n /c:12 /M "Pick an option "%1
goto Bravo-%errorlevel%
:Bravo-1 B
echo The program will now end
pause
exit
:Bravo-2 B
echo You chose to go back
pause
goto :aPrompt
:Alfa-2 A2
echo You chose to show and eliminate the duplicated files
pause
echo Showing the files paths
forfiles /P "C:\Users\11506018\Desktop\Batch testes\Teste 1" /M Giorgio.docx /S /C "cmd /c echo @relpath@fname"
echo Files identyfied by the the program (it serves the purpose of confirming if they are the right files)
forfiles /P "C:\Users\11506018\Desktop\Batch testes\Teste 1" /M Giorgio.docx /S /C "cmd /c echo @file"
for /f "delims=" %%a in (
"""dir /a-d /b /tw /od "C:\Users\11506018\Desktop\Batch testes\Teste 1" /Giorgio.docx"
) do set %file%=="oldest"
echo %oldest%
for /f "delims=" %%a in (
"""dir /a-d /b /tw /o-d "C:\Desktop\Batch testes\Teste 1 /*Giorgio.docx""
) do set %file%=="newest"
echo %newest%
echo You sure you want to delete the files? (Type '1' to eliminate them, '2' to not eliminate)
:cPrompt
choice /n /c:12 /M "Pick an option "%1
goto Charlie-%errorlevel%
:Charlie-1 C1
echo You chose to delete the files
echo The files will be deleted according to your decision
echo For each file it will be asked if you wish to delete it
pause
del /p /s "C:\Desktop\Batch testes\Teste 1"\*Giorgio.docx
echo The files have been deleted at your choice
pause
:dPrompt
echo Do you wish to exit the program or go back? (Type '1' to end the program, type '2' to go back to the main menu)
choice /n /c:12 /M "Pick an option "%1
goto Delta-%errorlevel%
:Delta-1 D1
echo You chose to leave. The program will shut down
pause
exit
:Delta-2 D2
echo You chose to go back to the main menu
pause
goto :aPrompt
:Charlie-2 C2
:ePrompt
echo You chose not to delete the files.
echo Do you wish to go back to the main menu or to leave? (Type '1' to go back to the main menu, '2' to leave the program)
choice /n /c:12 /M "Pick an option "%1
goto Evo-%errorlevel%
:Evo-1 E1
echo You chose to go back to the main menu
pause
goto :aPrompt
:Evo-2 E2
echo You chose to exit the program. The program will shut down now
pause
exit
:Alfa-3 A3
echo The program will shut down then
pause
exit
我還沒有在任何地方找到這樣的腳本。如果我設法讓它作業,我會在 Stack Overflow 中分享它,以便將來需要類似東西的人可以有更多的內容可以使用
uj5u.com熱心網友回復:
除了一些嚴重的語法錯誤(您可以通過開關獲得每個命令的幫助/?,例如set /?, for /?)。
除此之外,您只需一個回圈即可完成:
set "oldest=NONE"
set "newest=NONE"
for /f "delims=" %%a in ('dir /a-d /b /tw /od "C:\Desktop\Batch testes\Teste 1\*Giorgio.docx"') do (
if not defined oldest set "oldest=%%a"
set "newest=%%a"
)
echo oldest: %oldest%
echo newest: %newest%
注意:一個檔案夾中只能有一個同名檔案,因此在檔案夾中搜索最新/最舊的檔案只有使用通配符才有意義。我在這里猜到了,所以您當然想檢查/更改*Giorgio.docx
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/491637.html
下一篇:批處理腳本-使用7-zip壓縮
