0

I want to create a small batch script for my "Send to" menu that deletes certain files:

I have a lot of files named in this convention: foo.xxx, foo.jpg, bar.xxx and bar.jpg. I want to delete .xxx files where there is a corresponding .jpg file recursively for all nested folders below the level from where I call this script.

Based on this post I came up with the following:

@ echo off
pushd "%~1"
for /R %%f in (*.jpg) do echo "%%f"

This at least lists the .jpg files it seems but I get an error message that reads like this:

"UNC-Pfade werden nicht unterstützt. Stattdessen wird das Windows-Verzeichnis als aktuelles Verzeichnis gesetzt."

I found a post which shows how to delete files recursively but I do not really understand how to combine the two solutions to delete only those .xxx files for which I found a .jpg. Furthermore it would be nice if there where some sanity check that checks if there really is a .xxx file with the current name, sometimes there are .jpg files in these folders that have no corresponding .xxx file.

It seems I can remove by file extension like this:

@ echo off
pushd "%~1"
for /R %%f in (*.jpg) do echo "%%~nf"

But now I have no complete path to delete the wanted files? Is this correct so far? Thank you in advance!

NorrinRadd
  • 545
  • 6
  • 21
  • Are you trying to do this on a network drive? – Dialecticus May 26 '20 at 13:04
  • Yes, it sould work on a networkdrive ( where the data is stored) but also locally ond my drives. – NorrinRadd May 26 '20 at 13:55
  • 1
    I don't speak German very well, but this "UNC Pfade" wording means you can't do this on network path. Try to map this network path to a letter (with SUBST command), and then work on usual DOS path. – Dialecticus May 26 '20 at 14:05
  • Thanks for the hint, but the script even lists the correct path and files as is.... BUt i stll do not understand how to delete those files that i want. I could solve the UNC-PAth error later. – NorrinRadd May 26 '20 at 14:15
  • Given that the command extensions are enabled, which is the default configuration anyway, `pushd` does support UNC paths and maps such ti a temporary drive letter, which becomes the current working directory (`popd` deletes the temporary drive letter then). Is this the whole script you have troubles with, or is there any more code you did not share so far? – aschipfl May 26 '20 at 14:23
  • There can be many (like 20 to 50) but not more. xxx stands as a placeholder as this is data from a REM/EDX mashine which stores the data with a different extenison based on what has been meassured. Would i have to add popd "%~1" at the end of the script? But i am mainyl interested in how to delete the files. – NorrinRadd May 26 '20 at 14:37
  • You could set the "read only" attribute on all `*.jpg`, then `for /R %%a in (*.jpg) do del "%%~dpn.*` (it will not delete Read-Only files), then remove the read only attributes again. [attrib](https://ss64.com/nt/attrib.html) – Stephan May 26 '20 at 14:50
  • So :`@ echo off pushd "%~1" for /R %%f in (*.jpg) do del "%%~dpnf.xxx" popd` seems to work... I hope that there is nothing dangerous about this? Thanks for your help. – NorrinRadd May 26 '20 at 15:28
  • for ".xxx" yes, it works. For "all but .jpg" it doesn't. For testing, you can disable the delete with `... do ECHO del "%%~dpnf.xxx"`, so you can verify it does what you want before removing the `ECHO` command to enable the `del` command. (Best practice: work on a copy or your data or have a backup) – Stephan May 26 '20 at 16:22

1 Answers1

0

Solution:

@ echo off
pushd "%~1"
for /R %%I in (*.jpg) do del "%%~dpnI.vgd"

popd

See comments for all contributions.

NorrinRadd
  • 545
  • 6
  • 21
  • `... do del "%%~dpnI.vgd" 2>nul` to suppress the error message, when the file doesn't exist. – Stephan May 27 '20 at 12:08
  • Batch script for my "Send to" menu: pushd "%~1" will not work. There can be 50 .xxx You have to write part of your command 50 times. What is the path to the root UNC folder? – somebadhat May 27 '20 at 12:30