Multiple commands using forfiles? - batch-file

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.

Related

How to delete a single file if older than x days?

I need to delete a single file before starting a program, but this should be done only when it's older than a day.
At the moment I execute always in a command prompt window:
del /f filepath
*run program*
I know, there is this option to do it for multiple files via batch:
ForFiles /p "path" /d -X /c "cmd /c del #file"
But I want to do this only for a specific file, do I have to use forfiles or is there another little bit faster option without iteration?
Here is your code:
forfiles /p "path" /m "file name" /d -Number of the days older mean the value of your X days /c "cmd /c del /f /q /a #file"
You got the code in your question hard coded. The /s switch of forfiles iterates through all the sub folders of given path. So there is no use of that switch. And the value of /d switch deletes files more older than 30 days, but you have wanted anonymous X days. Change them and your code will work. For more help, start cmd and type forfiles /?.

Is there a way to perform a negative search mask to forfiles?

I know how to utilize search masks in the forfiles command to target files of a particular extension, but I am working with a case where I want to avoid a particular extension and catch everything else.
Is there an easy way to achieve this with the forfiles command or will I need to write a custom for loop to handle this process? I am not looking to run a command like below which would force me to define my search masks before hand.
for %G in (.txt, .edi, .csv) do forfiles -p "C:abc\del" -s -m *%G -d -10 -c "cmd /c echo #path"
My use case is removing all files that are not encrypted from a directory recursively based on a single extension. This would be my negative value.
This batch file script command deletes everything older than 30 days if the filename does not end in PDF. It uses the 2003 version of forfiles in a Windows 7 cmd shell.
forfiles can not use reg exps, it only processes simple wildcards for masks. This is inefficient but ok for a small number of files.
for /f "delims=" %%X in ('dir /b %DIRNAME% ^|grep -vi pdf$') do forfiles /p %DIRNAME% /m "%%X" /d %DAYNUM% /c "cmd /c del #FILE" ```
As per #Compo's comments to my question, an approach to applying a negative search mask to the forfiles command would be to add a filter to the cmd implementation after the fact like so:
forfiles /P %localDir% /S /M *.* /C "cmd /c if not #EXT == %ignoredExtension% echo #PATH
However, #Compo raised a valid concern that this implementation would cause a new cmd.exe instance to be opened for every file found. In instances where the file count is fixed or known to be constant this may be a decent approach, but a more efficient alternative that is able to be scaled would be to create a for loop manually that applies this filter at the for loop instance before making the call to the do operations like so:
For /F "Delims=" %%G In ('dir /A-D^|FindStr /VE %ignoredExtensions%')Do #echo %%G

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.

Deletion Batch file but receive cannot find desktop.ini

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.

Trying to use a Windows Batch script to delete files older that a certain date BUT want to exclude folders with a certain name

So what I want to do is use the batch script to delete off files on my data directory on my server(I have already created a archive copy just need to delete the files now). Problem is we use roaming profiles and if I ran this on the data directory it would go though and delete all kinds of ini's and application files that are needed but not changed very often.
forfiles -s -m *.* /D -01/01/2014 /C "cmd /c del #path"
If possible I would like to run a script like this but exclude any folder that has profile or profile.V2 in it and anything under it.
My directory structure looks something like this.
C:/Data/Users/srodgers/profile/Desktop
-or-
C:/Data/Users/ssmith/profile.V2/Desktop
and I would like to run the script from the root of data
Test this and remove the echo (it currently just displays the del commands) to acually delete the files.
In limited testing it's working fine.
#echo off
forfiles -s -m *.* /D -01/01/2014 /C "cmd /c echo #path |find /i /v 0x22\profile\0x22|find /i /v 0x22\profile.V2\0x22 >nul && echo del #path"
pause

Resources