batchfile fails to delete unwanted files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

batchfile fails to delete unwanted files

#1 Post by Sebastian42 » 22 Sep 2023 03:26

I have no trouble 'manually' deleting files with the extension .jsonlz4, preceded by '==', but want to do it by batchfile and keep failing. Can anyone help me with the right code to do this ? One example of such a file is 'bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4'

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: batchfile fails to delete unwanted files

#2 Post by DOSadnie » 22 Sep 2023 05:59

What do mean by
Sebastian42 wrote:
22 Sep 2023 03:26
I have no trouble 'manually' deleting files with the extension .jsonlz4, preceded by '==
[...]
?

Maybe it is the cause behind also your BAT scripts not being able to do this task?

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: batchfile fails to delete unwanted files

#3 Post by OJBakker » 22 Sep 2023 11:49

I assume you are using a for loop to delete these files.
Something like the following will fail.

Code: Select all

for %%A in (bookmark*.jsonlz4) do del %%A
You can protect the filenames in the del command by enclosing the filename in double quote's.

Code: Select all

for %%A in (bookmark*.jsonlz4) do del "%%A"

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: batchfile fails to delete unwanted files

#4 Post by Sebastian42 » 22 Sep 2023 20:25

DOSadnie - I mean that if I click on those files and press the delete key, the files disappear - something I have NOT achieved when using a batch file.

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: batchfile fails to delete unwanted files

#5 Post by Sebastian42 » 22 Sep 2023 20:34

OJBakker - you seem to be focusing on the deletion of multiple files - I would be satisfied if just ONE got deleted by batch file code.
The code I have used and which fails is

del /q C:\Data\Firefox\Bookmarks\*.jsonlz4

and also

del C:\Data\Firefox\Bookmarks\*.jsonlz4 /q

Notice that these files are COPIES (in C:\Data\) of those Firefox's backups.

I do not understand "I assume you are using a for loop to delete these files."

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: batchfile fails to delete unwanted files

#6 Post by OJBakker » 23 Sep 2023 04:38

Sebastian42 wrote:
22 Sep 2023 20:34
...
I do not understand "I assume you are using a for loop to delete these files."
In your first post you have posted no code and just one filename.
In your last post you have posted one line of code.
The code you have posted has no problem deleting a file with the filename you have posted.

Because del with wildcard pattern works without problem with your filename and a for loop with wildcard pattern fails with your filename I had assumed you were using a for loop.

Deleting the file without using a wildcard also shows the problem.

Code: Select all

del bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4
Using double quot's will solve this.

Code: Select all

del "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4"
The next script will show the problem and the solution. Run this in an empty folder. It will create the test files.

Code: Select all

@echo off
cls

echo create dummy file(s)
copy nul "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4"
echo dir/del with wildcard NO PROBLEM
dir *.jsonlz4
del *.jsonlz4

pause
cls
echo create dummy file(s)
copy nul "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4"
echo dir/del without double quot's FAILS, It searches for 2 files, the part before and the part after the ==
dir bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4
echo dir gives File not found because the = is seen as separator similar to space , and ;
del bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4
echo del fails with: Could Not Find bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw
dir/b

pause
cls
echo create dummy file(s)
copy nul "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4"
copy nul "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw"
copy nul ".jsonlz4"
dir/b
echo dir/del without double quot's FAILS, It searches for 2 files, the part before and the part after the ==
dir bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4
echo dir shows now 2 files instead of the one you are requesting, the = is seen as separator similar to space
echo del now deletes 2 files instead of the one you are requesting
del bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4
dir/b

pause 
cls
echo dir/del with double quot's NO PROBLEM
dir "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4"
del "bookmarks-2023-09-02_304_NTwyuxMm2eEdhF++Azc4Bw==.jsonlz4"

pause

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: batchfile fails to delete unwanted files

#7 Post by Sebastian42 » 23 Sep 2023 06:44

I'm not familiar with the term 'for loop', hence the "I don't understand"

Unless I misunderstand you, you are suggesting that deletion will occur if I put the entire file name in quotes first.
But that would require me to intervene 'manually' before applying the batchfile - which annuls the advantage of automation achieved by batch file.

Your reply also seems to confirm my suspicion that it is the '==' which is interfering with the deletion process.

Have I understood you correctly ?

"The code you have posted has no problem deleting a file with the filename you have posted.", but when I run a batch file with that code, it leaves the file undeleted.
So it seems is not the code that is lacking, but a difference in your and my running of the batch file.
Or the inclusion of extra bits like : 'echo', 'cls', 'dir', 'nul'

Incidentally you seem to have triggered error codes (explanation for failure to delete), whereas my attempts do not.

I think your batchfile performs four slightly different actions, and it gives you the opportunity to 'say' at each PAUSE : "OK, now do the next step".

It must be obvious that my grasp of your procedure is quite fragmented.

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: batchfile fails to delete unwanted files

#8 Post by OJBakker » 23 Sep 2023 13:06

The cause of your problem is most likely you are using the del command wrong.
I have explained what goes wrong en how to prevent that from happening.
The demo-code shows and explains this, with dir showing the selected files and del deleting these files and echo commenting on why some stuff fails.

The == in a filename can be the reason your del command fails, but there are other possible reason why a del can fail.
A file can have file attributes preventing deletion, for example if a file is set to readonly.
A file can also have security settings preventing deletion.

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: batchfile fails to delete unwanted files

#9 Post by Batcher » 23 Sep 2023 19:20

test-1.bat

Code: Select all

if exist "C:\Data\Firefox\Bookmarks\*.jsonlz4" (
    del /f /q /a C:\Data\Firefox\Bookmarks\*.jsonlz4
) else (
    echo No *.jsonlz4 found
)
pause

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: batchfile fails to delete unwanted files

#10 Post by Sebastian42 » 23 Sep 2023 20:23

OJBaker - I started my post by saying that I can MANUALLY delete the file; does not that settle the issues of "A file can have file attributes preventing deletion, for example if a file is set to readonly. AND A file can also have security settings preventing deletion."
I think it is quite likely that the '==' prevents deletion by batchfile code, but putting quotes around the file name is not a practical solution, unless it can be done by batch file as a preliminary step to deletion.

Your 'slip of the pen' in writing 'en' instead of 'and' suggest that you may be either in Holland, or Dutch. Am I right ?


Batcher - 'No *jsonlz4 found' seems to suggest a reason why my batch file fails - I am looking for a remedy.

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: batchfile fails to delete unwanted files

#11 Post by OJBakker » 24 Sep 2023 08:01

Yep, I am from the Netherlands and Dutch is my primary language so sometimes my English will have a 'touch of Dutch'.
And my id at this forum is OJBakker, not OJBaker :lol:

Your use of the term 'manually' is ambiguous.
For you it apparently means: manually from the windows gui using windows explorer and mouse clicks (from your reply to DOSadnie).
For many script writers/users manually means from the command prompt using keyboard to type cmd commands (del filename).
At the command prompt you can run commands manually by entering by keyboard or pasting from editor.
Or you can write commands in an editor, save these as a batch script and run this script from the command line or from the windows gui.

Using Gui (windows explorer) or command prompt (cmd) are entirely different beasts and have different rules and pitfalls.
A delete from windows explorer will default move the file to the waste basket and ignore the read only attribute.
A delete from command prompt will respect the read only atribute and will only allow delete using a special force delete flag.

Using the command prompt 'manually' is similar to using commands in batchfiles, but not the same.
There are several differences between these to methods of using cmd commands.

Next is an example of using the command prompt to gather info for your problem files.
Do not put these commands in a batchfile because batchfile will require several changes to this code.

Open a command prompt and copy/paste the code below into the open cmd window.
This will create a dummy bookmarks file in the dir C:\Data\Firefox\Bookmarks
Next this will create a log file in your dir for temporary files.
This logfile will contain a list of files selected by your pattern and show the file attributes and permissions for these files.
The dummy file guarantees there will be output and gives a point of reference to compare with other files found.
Please post the logfile in this forum.
If your problemfile is shown in the logfile than the logfile will contain enough information to determine why the file is not deletable.
IF your problemfile is not shown in the logfile you will have to collect the info for attributes and permissions from windows explorer and add this info to the post including the logfile.

Code: Select all

@echo off
cls
set "dir=C:\Data\Firefox\Bookmarks"
set "pattern=bookmarks*.jsonlz4"
set "DirPattern=%dir%\%pattern%"
rem create dummy bookmarks file
if not exist "%dir%\BookmarksDummy==.jsonlz4" copy nul "%dir%\BookmarksDummy==.jsonlz4"
(ver
echo location of file dummy.log: %TEMP%
echo .
if exist "%dir%" ((echo Dir found, attributes precede dir name, attributes for %dir%) & (attrib %dir%))
if exist "%DirPattern%" ((echo Files Found, attributes precede filename) & (attrib "%DirPattern%")) else (echo No Files with pattern %DirPattern%)
echo .
if exist "%dir%" ((echo Permissions for dir %dir%) & (icacls %dir%))
if exist "%DirPattern%" ((echo Permissions for file) & (icacls "%DirPattern%")) else (echo No Files with pattern %DirPattern%)
) > %TEMP%\dummy.log
rem cleanup variables
for %A in (dir pattern DirPattern) do set "%A="
@echo on
type %TEMP%\dummy.log
The code posted by Batcher has the same problem as your code: missing double quot's in the del command.
Try the corrected code below:

Code: Select all

if exist "C:\Data\Firefox\Bookmarks\*.jsonlz4" (
    del /f /q /a "C:\Data\Firefox\Bookmarks\*.jsonlz4"
) else (
    echo No *.jsonlz4 found
)
pause

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: batchfile fails to delete unwanted files

#12 Post by Sebastian42 » 24 Sep 2023 15:26

Oh jee ! bakker ! You must have made a mess of the oven contents ! (hahaha)

Starting with the simplest instructions (right at the end) ..... I created a batch file from Batcher's code and ran it.
Of course a DOS window opened - and stayed open without seeming to achieve anything, so I just closed it.

'Your use of the term 'manually' is ambiguous.' I mean doing it by mouse and keyboard rather than by code in batchfile : select the file and press DEL key.

'Using Gui (windows explorer) or command prompt (cmd) are entirely different beasts' - I understand that using CMD follows the rules initially laid down in DOS.

'A delete from command prompt will respect the read only attribute and will only allow Delete using a special force delete flag.'
Well that seems to explain why a batch file fails where GUI succeeds.

I pasted the code into a command prompt and am reproducing the entire DOS window contents in the attachment - I hope tat is what you mean by 'the log file'.

I can not interpret the results to decide whether or not 'your problem file is shown in the logfile'
When I clicked on what I took to be the 'insert attachment tool' an unexpected response was the addition of 'Image' at the bottom of this post.
Trying to paste the image between the brackets failed. I will try to reproduce the text : there appear to be many 'spelling' errors, and I will correct them if you ask me to.
or provide the attachment some other way.

"set "dir=C:\Data\Firefox\Bookmarks'
set 'pattern=bookmarks‘.jsonlzd'
set 'DirPattern=%dir%\%pattern%'
rem create dummy bookmarks file
if not exist ‘Xdir%\BookmarksDummy==.jsonlzd' copy nul 'Xdir%\BookmarksDummy==q
‘sonlzd'
1 file(s) copied.
(ver
More? echo location of file dummy.log: XTEMPX
More? echo .
More? if exist 'XdirX' ((echo Dir found, attributes precede dir name, attribute:
for XdirX) s (attrib Xdir%))
More? if exist "XDirPatternX" ((echo Files Found, attributes precede filename)!
(attrib 'XDirPatternX")) else (echo No Files with pattern XDirPatternX)
More? echo .
More? if exist "XdirX" ((echo Permissions for dir XdirX) & (icacls Xdir%))
More? if exist 'XDirPatternX' ((echo Permissions for file) s (icacls 'XDirPattel
nX')) else (echo No Files with pattern XDirPatternX)
More? ) > XTEMP%\dummy.log
rem cleanup variables
for %A in (dir pattern DirPattern) do set '%A='
@echo on
C:\NINDONS\5ystem32>type XTEMP%\dummy.log"

I will attempt to 'collect the info for attributes and permissions from windows explorer'
I can't because the files that used to be in the bookmarks folder are all gone and have been replaced by YOUR creation : "BookmarksDummy==.jsonlz4"

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: batchfile fails to delete unwanted files

#13 Post by OJBakker » 24 Sep 2023 18:14

Wow, what a mess you have created. :? :cry:
You seem to be incapable of reading/following the most elementary instructions!

By starting with running the corrected code from Batcher you yourself have deleted the bookmarkfiles!
This confirms that your problem file had no permissions problem.
An attribute problem is still possible because Batcher code uses silent and forced delete for the selected files for all possible attributes.

Next you have copied my code as requested and ran this as requested.
This will have created a dummy bookmarks file in your folder C:\Data\Firefox\Bookmarks
This will have created a log file with the info gathered by my code in your folder for temporary files.
The last line of my code types the contents of this file and even reports the name and location of this file.

What you have posted is not the logfile but only the first part of the output in the cmd window, corrupted and mangled to an unusable mess.
This is not just a copy / paste of the text on the cmd window.
You have not posted this in a code block but just in the post itself.
Posting code (output) this way is wrong. The forumsoftware can interpret / change code (output) in unwanted ways, you should have used the code button to prevent this.
But the forumsoftware won't randomly change % to X as one of the most visible anomalies. Another is changing C:\Windows\ to C:\NINDONS\
Did you make a printscreen of the cmd window and use a program to extract the text from this printscreen?
And last but not least, you left out the most important part of the info on the cmd screen, the type of the contents of the logfile: %temp%\dummy.log

You have not attached the logfile to your post.

How difficult can it be to:
click on attachments
click on add files
in the select window type: %TEMP%\dummy.log

Sebastian42
Posts: 34
Joined: 17 Feb 2017 02:28

Re: batchfile fails to delete unwanted files

#14 Post by Sebastian42 » 24 Sep 2023 19:51

I'll accept any blame that is due - in the hope that a solution to my problem will eventuate.

'You seem to be incapable of reading/following the most elementary instructions!' I have tried and apparently failed.

'This confirms that your problem file had no permissions problem.' I take that to be good news.

'This will have created a dummy bookmarks file in your folder C:\Data\Firefox\Bookmarks'
The only file in C:\Data\Firefox\Bookmarks\ is the dummy.

'in your folder for temporary files.' I find temporary files in many folders (and routinely delete them). It would help if you can suggest a likely folder for me to look in.

'You have not posted this in a code block'; I don't know how to do that.

'Did you make a printscreen of the cmd window and use a program to extract the text from this printscreen?' I did - and I know that explains the corruptions. I offered to correct them, if you asked me to do it.

'How difficult can it be to:' That seems simple enough and yet I managed to bungle it. When I have time I will try to improve on my botched effort.

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: batchfile fails to delete unwanted files

#15 Post by Batcher » 24 Sep 2023 21:05

Allow me to guess.
1. Make sure your bat file is saved as Windows format (0D0A in the end of each line), rather than Linux format or MacOS formact (only 0D or 0A in the end of line).
2. Edit your bat file with Notepad (not any other tools), File, Save As, Encoding, choose ANSI.

Post Reply