So im trying to make a script where the batch file searches a certain directory, it's folders and sub-folders, for duplicated files. It then should also identify the oldest file(that is most probably the original) and the newest file(that is probably the one where the user is working on currently) and also ask the user if it wants to delete each file from all the ones it identified (even the oldest and newest but when it comes up for them to be deleted it should identify them as oldest and newest) Im new to this kind of scripting so forgive me for stupid mistakes i made. In this case i made a "Giorgio" Word document and placed it in some sub-folders, in one i altered what was written as well and dire
Here is the part im trying to work on, that is about identifying the oldest and newest file. I believe im trying to do something i'm not allowed, im using these commands wrong
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%
This is my whole program if you guys are curious (sorry if some sentences aren't well written, i've translated everything from portuguese to english in the last 3 minutes)
@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
I haven't found a script anywhere like this. If i manage to get it to work i'll share it here in Stack Overflow so that in the future, people that need something similar can have more content to work with
CodePudding user response:
Besides some severe syntax errors (you can get help with each command with the /?
switch, for example set /?
, for /?
).
Besides those, you can do it in just one loop:
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%
Note: There can be only one file with the same name in a folder, so searching for the newest/oldest file in a folder does only make sense with a wildcard. I guessed here, so you certainly want to check/change *Giorgio.docx