Deletion Batch file but receive cannot find desktop.ini - batch-file

I've created a simple batch file to delete files over 14 days which is a simple command as most of you probably know so it's doing the below
forfiles /p "C:\%userprofile%\Downloads" /s /m *.* /c "cmd /c Del #path" /d -14
but I keep receiving could not find C:\User\%userprofile%\downloads\desktop.ini
So I assume it's searching for the desktop.ini file but I have all folders and files unhidden. Is there a way to prevent it looking for that file and just doing as a I ask it?
Any help would be appreciated.

As Mike Nakis suggests, del is probably failing on desktop.ini because that file is typically set +ash (archive, system, and hidden). The easiest solution would be just to ignore it. It's harmless anyway.
forfiles /p "%userprofile%\Downloads" /s /m *.* /c "cmd /c Del #path 2>NUL" /d -14
If it really bothers you and you insist on deleting it, then remove the system attribute.
attrib -r -s -h -a "%userprofile%\Downloads\*"
forfiles /p "%userprofile%\Downloads" /s /m *.* /c "cmd /c del #path" /d -14
... but it'll probably just get re-created eventually anyway. I'd just ignore it.

What do you mean when you say that you have all folders and files unhidden? You have probably instructed the windows explorer to also show hidden files, but that does not mean that the files are not hidden.
You have two options: for each file that you are about to delete, either use the attrib command to make sure it is not hidden prior to deleting it, or play with the /A option of the del command to make it delete everything, even hidden files.

Related

How do you change the -D automatically in a single batch file?

I have a question about batch files. For instance,
forfiles /p "C:\Users\ ..." /s /m . /D -150 /C "cmd /c del #path"
Is it possible to run this, then change the -150, to -130, -110 automatically instead of writing multiple commands like
TITLE DELETE OLD FILES
forfiles /p "C:\Users\ ..." /s /m . /D -150 /C "cmd /c del #path"
forfiles /p "C:\Users\ ..." /s /m . /D -130 /C "cmd /c del #path"
forfiles /p "C:\Users\ ..." /s /m . /D -110 /C "cmd /c del #path"
PAUSE
Rationale: The system stores data about a semiconductor related equipment and holds files which are about more than 2 years old. However, deleting these files immediately will cause a crash. Hence, I am deleting these files periodically.
I will also appreciate if there is a command which allows the user to choose when to display the current storage space available, and then allows the user to choose whether to delete the next batch of files.
I have asked the same question, "https://stackoverflow.com/questions/72428986/how-to-delete-batch-files-cmd-from-the-oldest-date-first-then-iterate-till-a" and answered the responses. However, I am still stuck and unfamiliar on how to proceed.
Thank you.

Multiple commands using forfiles?

I am trying to automate converting some old wav files to MP3 to free up storage using LAME encoder. Unfortunately, I am not having much sucess. I am using the following command:
forfiles /p "c:\wavfiles" /s /m *.wav /c "cmd /c c:\lame\lame.exe -q5 #path" This works, however it outputs the files into the same folder as the wav files. This makes it necessary to then move the files in a different process. There has to be a better way.
Ideally, I would like to output the MP3 files to a different drive using the same folder structure using a single batch file.
I thought about using:
forfiles /p "c:\wavfiles\" /s /m *.wav /c ^&cmd /c C:\lame\lame.exe -q5 #path" &robocopy C:\wavfiles\ E:\converted *.mp3 /create /s /mov
However this does not seem to work, and seems rather inefficient.
I also thought about perhaps using the #relpath variable to copy the folder structure, so that I could try something like this:
forfiles /p "c:\wavfiles" /s /m *.wav /c "cmd /c c:\lame\lame.exe -q5 E:\#relpath"
However I'm not sure how to do this. Any assistance would be appreciated. Thank you.

How do i delete all of the content inside 2 certain folders using batch files?

I was recently messing with automating tasks on my computer and i wanted to make a batch file that automatically deletes content inside 2 certain folders.
I tried to delete the content of 2 folders with this batch file:
forfiles -p "C:Test1" -s -m *.* /C "cmd /c del #path"
forfiles -p "C:Test2" -s -m *.* /C "cmd /c del #path"
That seemed to work, but it only deleted files, not folders, and i was looking to delete everything inside them. I tried an alternative, but still the same outcome:
del /s /q "C:Test1\*.*"
del /s /q "C:Test2\*.*"
Can someone give me an explanation on how to also delete folders, not only files?
To delete a folder you have to use the rd command
forfiles -p "C:\Test2" -s -m *.* /C "cmd /c IF #isdir == TRUE rd /S /Q #path"
Here's my suggestion:
#(For %%G In ("C:\Test1" "C:\Test2") Do #PushD "%%~G" && (RD /S /Q . & PopD)) 2> NUL
It uses the RD command only, and does it by asking to quietly remove the current directory and all of its subdirectories. You cannot remove a directory if it is the current directory, so the code will make the directory its current directory, using PushD, if that is successful, &&, the RD command is run, using the relative current directory ., then it returns to the previously current directory, PopD. The RD command, whilst it cannot remove the current directory, can still remove everything in it, and does, although it does generate an error message. I have supressed the error messages by redirecting StdErr, 2> to the NUL device.

Batch file to delete all text files in a folder over 10 days old except certain ones

I'm new to batch files, trying to write one that will delete all .txt files in a folder over 10 days old EXCEPT one called template.txt. How is this done? I have the below but it deletes ALL txt files over 10 days. Appreciate your help.
forfiles /p "C:\test" /s /m *.txt /c "cmd /c del #path" /d -10
Just implement the contition into the command line run by forfiles, like this:
forfiles /S /P "C:\test" /M "*.txt" /D -10 /C "cmd /C if #isdir==FALSE if /I not #file==0x22template.txt0x22 del #path"
The if #isdir==FALSE part is to exclude any directories from being processed further in case there are some with .txt at the end of their names (although quite unlikely), because forfiles enumerates both files and directories.
if /I not #file==0x22template.txt0x22 becomes if /I not "<name of currently iterated item>"=="template.txt" and excludes files named template.txt from being deleted. The /I option makes the comparison case-insensitive, like Windows also treats file and directory paths.

Batchscript to delete file more than 200kb

can anyone pls explain the below command
FORFILES /S /M *.imp /C "cmd /c if #fsize gtr 200000 del #file"
I want to delete the .imp file if the file is more than 200kb. I got the command but when I try to put the path //flxxx/g_ff_gg$/CDO_MTK_SMT/Hari krishna/gggg_SCRIPTS/ I am getting an error. Where shall I put the path in the above command.
or is there any command to check the file which is more than 200kb and deletes the file with the given directory
One possible solution, I use in such cases, is to first change into the directory. Then call the command and final leave. pushd and popd are made for such actions.
pushd \\ComputerName\ShareName\Directory1\Directory2
FORFILES /S /M *.imp /C "cmd /c if #fsize gtr 200000 del #file"
popd

Resources