0

My goal is to delete a file named Open Notebook.onetoc2 in all folders and subfolders of Z: drive, BUT NOT delete that file from those folders, which contain *.one files (actual OneNote files).

I have searched for solutions online for that matter, but have not found that and do not know, how to build one such command myself exactly.

I have got this far:

for /F "delims=" %a in ('dir /b /s "Open Notebook.onetoc2"') do @if exist %~da%~pa*.one echo del: "%~da%~paOpen Notebook.onetoc2" 

Seems 99% done, but stuck with the situation, where "if exist *.one" includes the .onetoc2 files into the "search" as well, which is unwanted. I would only like the "search" to find .one files, not .onetoc2 among them, as it is important to skip deleting the .onetoc2 file, if .one file(s) exist in that directory.

Many thanks in advance! :)

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Artto
  • 1
  • 1
  • why not be more specific on what you want to on search or match it somehow? `do if "%~xa"==".one" ...` the question is still a little unclear as you first say you want to delete a file named `Open Notebook.onetoc2` then you say your don't and only want to delete `.one` files. – Gerhard Oct 22 '19 at 07:14
  • If I understand your question correctly, perhaps this is what you're wanting: `For /F "Delims=" %A In ('Dir /B/S/A-D-S-L "Z:\Open Notebook.onetoc2" 2^>NUL')Do @If Not Exist "%~dpA*.one" Del /A/F "%A"` – Compo Oct 22 '19 at 09:03
  • @Compo - you got it! This worked perfectly! :)) Thank you! – Artto Oct 22 '19 at 10:15
  • @GerhardBarnard - Thank you as well for well pointing out the way to look at things. – Artto Oct 22 '19 at 10:16

1 Answers1

2

My commented suggestion as an answer to be ran from the :

For /F "Delims=" %A In ('Dir /B/S/A-D-S-L "Z:\Open Notebook.onetoc2" 2^>NUL')Do @If Not Exist "%~dpA*.one" Del /A/F "%A"

Based upon the For /F syntax: For /F ["options"] %variable In ('command')Do command [command-parameters] we first need to take a look at 'command'. We want to output all files rooted at Z:\ named Open Notebook.onetoc2, we do that using Dir, (enter dir /? for its usage). For safety I have excluded any system files or reparse points from that output, and have redirected any error message to the NUL device, which prevents any message, should none exist, from being passed to Do.

The For loop will pass every file as a fully qualified path to Do as metavariable %A. We want to first verify that along side any file returned, another file with the extension .one does not exist. For that we use an If Not Exist condition, (see if /? for its syntax). %A can be expanded to its drive and path only, (i.e. the parent directory of any returned Open Notebook.onetoc2 file), using %~dpA, (this syntax is available in the For command help information, for /?).

If the condition is met, we delete the file using the Del command. As the files could be read only, we use the /A and /F options, (see del /? for its syntax).


If you wanted to do the same, but from a , it would look more like this:
@For /F "Delims=" %%A In ('Dir /B/S/A-D-S-L "Z:\Open Notebook.onetoc2" 2^>NUL')Do @If Not Exist "%%~dpA*.one" Del /A/F "%%A"
Compo
  • 36,585
  • 5
  • 27
  • 39