1.

Solve : delete xml Files in folder when corresponding TS files doesn't exist?

Answer»

Hi,
I have a home theater pc and wanna clean the folder "m:\recorded tv" every day.
I'm searching for a batch file that deletes all xml files in this folder where no corresponding TS files exists.
For example this folder looks like this:
Video1.ts
Video2.ts
Video1.xml
Video2.xml
Video3.xml
Video4.xml

After the batch file was running the folder should look like this:
Video1.ts
Video2.ts
Video1.xml
Video2.xml

Video3.xml and Video4.xml are no deleted.

In the www I have found this

FOR %%I IN (*.*) DO IF NOT EXIST "%%~dI%%~pI%%~nI.ts" (del %%~nI.*)
exit

But that's not working properly.

Can someone help?

olli14ollii14,
I have a batch file below from foxidrive that I use to delete *.CR2 files that do not have a corresponding *.JPG file with the same name. You could probably modify it to suit your needs.

Code: [SELECT]::Posted by foxidrive on another forum
::Modified to fit my needs.
::Delete any CR2 file that does not have a JPG file of the same name.
::The command line is set to ECHO the Delete command to a batch file for inspection.
::Run the created batch file to delete the .CR2 files.

::Put this batch file in with the folder that contains the .JPG and CR2 files.

echo off
if not exist *.jpg goto :fail
if not exist *.cr2 goto :fail

for %%a in (*.cr2) do if not exist "%%~na.jpg" echo del "%%a" >>!CR2_files_2B_del.bat
goto :EOF

:fail
echo Either the .CR2 or .JPG files are missing.
echo Make the necessary corrections. This batch file has failed.
echo Press the space bar to close this window.
pause>nul
goto :EOFHi Ocalabob,
it's working great. Thanks a lot.

olli14It's me again. As I wrote already the Batch is working. But now I want to clean all subfolders too. Someone an idea? Quote from: olli14 on OCTOBER 20, 2016, 01:31:55 AM

It's me again. As I wrote already the Batch is working. But now I want to clean all subfolders too. Someone an idea?
Look at USING the /R OPTION with the FOR command.  You will need to change the FOR variable modifiers as well. Quote from: olli14 on October 20, 2016, 01:31:55 AM
But now I want to clean all subfolders too. Someone an idea?

Do you want to remove every file in a Subdirectory tree?

This is easy as Squashman SAYS but it's a dangerous script to have laying around - as it could find itself in another place and then all files can be removed where it's not supposed to run.

The solution is to include the correct path so it can only operate in the correct folder.

Code: [Select]del /s /q /f "c:\your folder tree\and at the\right\folder\*.*"

Ocalabob who gave you your script was a good friend to me, and he passed away from Cancer on 2 November 2013
Here's a pic from happier times.


I'm not so sure that the fish was too happy, but Bob had a ball. 
His home base was in Florida.R.I/P/

So 2 months after the post above...

Thanx for the pic.


Discussion

No Comment Found