Mass delete file extension batch file - performance

Hello I have a batch file I've created to delete all files of a certain extension that it asks for when you run it. I need to delete 2,111,000 .txt files and the batch file only deletes 3 at a time which will take forever to delete the files. Is there a way I can make it faster or if somebody has a better code to do this?
Here is my code:
#ECHO OFF
CLS
SET found=0
ECHO Enter the file extension you want to delete...
SET /p ext="> "
IF EXIST *.%ext% ( rem Check if there are any in the current folder :)
DEL *.%ext%
SET found=1
)
FOR /D /R %%G IN ("*") DO ( rem Iterate through all subfolders
IF EXIST %%G CD %%G
IF EXIST *.%ext% (
DEL *.%ext%
SET found=1
)
)
IF %found%==1 (
ECHO.
ECHO Deleted all .%ext% files.
ECHO.
) ELSE (
ECHO.
ECHO There were no .%ext% files.
ECHO Nothing has been deleted.
ECHO.
)
PAUSE
EXIT
Can I make this go faster?

The quickest way I can imagine is just:
cd /BASE_PATH
del /s *.txt
You're probably better just letting the OS sequentially delete files rather than trying to delete multiple files in parallel anyways. If you're using a mechanical HDD as opposed to an SSD, you could have files on different platters, heads, sectors, etc, and depending how much load you put on an I/O bound resource, the overall operation takes more time since the drive has to seek data all over the place. Plus, random access on an HDD is abysmal.

You might want to try it this way:
DIR C:\*.txt /S /B > filelist
FOR /f %%i in (filelist) DO ECHO DELETE %%i
Remove the 'ECHO' when you are sure you want to run this ;-)
But this only makes sense when you want to process each file separately, for logging purposes for example. If not, then #Dogbert solution is shorter.

Related

Loop through files in a folder and check if they have different extensions

I have a folder that contains files; each document should have .pdf and .xml format. I need to write a BAT file to run from a scheduled task to verify that both documents exist for each.
My logic is:
loop through files in the folder
strip each file to its name without extension
check that same name files exist for both .xml and pdf.
if not mark a flag variable as problem
when done, if the flag variable is marked, send an Email notification
I know how to use blat to sending email, but I'm having trouble to execute the loop. I found a way to get path and file name without extension but can't merge them.
I've used batch files a few time, before but I'm far from an expert. What am I missing?
Here's the code I have so far:
set "FolderPath=E:\TestBat\Test\"
echo %FolderPath%
for %%f in (%FolderPath%*) do (
set /p val=<%%f
For %%A in ("%%f") do (
Set Folder=%%~dpA
Set Name=%%~nxA
)
echo Folder is: %Folder%
echo Name is: %Name%
if NOT EXIST %FolderPath%%name%.xml
set flag=MISSING
if NOT EXIST %FolderPath%%name%.pdf
set flag=MISSING
)
echo %Flag%
pause
There is no need for fancy code for a task such as this:
#Echo Off
Set "FolderPath=E:\TestBat\Test"
If /I Not "%CD%"=="%FolderPath%" PushD "%FolderPath%" 2>Nul||Exit/B
Set "flag="
For %%A In (*.pdf *.xml) Do (
If /I "%%~xA"==".pdf" (If Not Exist "%%~nA.xml" Set "flag=MISSING")
If /I "%%~xA"==".xml" (If Not Exist "%%~nA.pdf" Set "flag=MISSING")
)
If Defined flag Echo=%flag%
Timeout -1
Something like this :
set "FolderPath=E:\TestBat\Test\"
pushd "%FolderPath%"
for %%a in (*.xml) do (
if exist "%%~na.pdf"(
echo ok
) else (
rem do what you want here
echo Missing
)
)
popd
Is this what you want?
#echo off
setlocal enabledelayedexpansion
set "FolderPath=E:\TestBat\Test\"
echo !FolderPath!
for /f "usebackq delims=" %%f in (`dir !FolderPath! /B`) do (
set /p val=<%%f
For %%A in ("%%f") do (
Set Folder=%%~dpA
Set name=%%~nxA
)
echo Folder is: !Folder!
echo Name is: !name!
if NOT EXIST !FolderPath!!name!.xml set flag=MISSING
if NOT EXIST !FolderPath!!name!.pdf set flag=MISSING
)
echo Flag: !flag!
pause
endlocal
You should reformat your code and keep in mind that the grama for batch file is critical. BTW, if you are trying to update the existing batch variable and read it later, you should enable localdelayedexpansion and use ! instead of %.
Keep it simple:
#echo off
pushd "E:\TestBat\Test" || exit /B 1
for %%F in ("*.pdf") do if not exist "%%~nF.xml" echo %%~nxF
for %%F in ("*.xml") do if not exist "%%~nF.pdf" echo %%~nxF
popd
This returns all files that appear orphaned, that is, where the file with the same name but the other extension (.pdf, .xml) is missing. To implement a variable FLAG to indicate there are missing files, simply append & set "FLAG=missing" to each for line and ensure FLAG is empty initially. Then you can check it later by simply using if defined FLAG.
Note: This does not cover the e-mail notification issue. Since I do not know the BLAT tool you mentioned, I have no clue how you want to transfer the listed files to it (command line arguments, temporary file, or STDIN stream?).
In case there is a huge number of files in the target directory, another approach might be better in terms of performance, provided that the number of file system accesses is reduced drastically (note that the above script accesses the file system within the for loop body by if exist, hence for every iterated file individually). So here is an attempt relying on a temporary file and the findstr command:
#echo off
pushd "E:\TestBat\Test" || exit /B 1
rem // Return all orphaned `.pdf` files:
call :SUB "*.pdf" "*.xml"
rem // Return all orphaned `.xml` files:
call :SUB "*.xml" "*.pdf"
popd
exit /B
:SUB val_pattern_orphaned val_pattern_missing
set "LIST=%TEMP%\%~n0_%RANDOM%.tmp"
> "%LIST%" (
rem // Retrieve list of files with one extension:
for %%F in ("%~2") do (
rem /* Replace the extension by the other one,
rem then write the list to a temporary file;
rem this constitutes a list of expected files: */
echo(%%~nF%~x1
)
)
rem /* Search actual list of files with the other extension
rem for occurrences of the list of expected files and
rem return each item that does not match: */
dir /B /A:-D "%~1" | findstr /L /I /X /V /G:"%LIST%"
rem // Clean up the temporary file:
del "%LIST%"
exit /B
To understand how it works, let us concentrate on the first sub-routine call call :SUB "*.pdf" "*.xml" using an example; let us assume the target directory contains the following files:
AlOnE.xml
ExtrA.pdf
sAmplE.pdf
sAmplE.xml
So in the for loop a list of .xml files is gathered:
AlOnE.xml
sAmplE.xml
This is written to a temporary file but with the extensions .xml replaced by .pdf:
AlOnE.pdf
sAmplE.pdf
The next step is to generate a list of actually existing .pdf files:
ExtrA.pdf
sAmplE.pdf
This is piped into a findstr command line, that searches this list for search strings that are gathered from the temporary file, returning non-matching lines only. In other words, findstr returns only those lines of the input list that do not occur in the temporary file:
ExtrA.pdf
To finally get also orphaned .xml files, the second sub-routine call is needed.
Since this script uses a temporary file containing a file list which is processed once by findstr to find any orphaned files per extension, the overall number of file system access operations is lower. The weakest part however is the for loop (containing string concatenation operations).

Move specific files out of subfolders and delete said subfolders

I recently exported the HDD on my panasonic camera to my notebook and noticed that the video files weren't ordered by name, but by their parent directory and said parent directory was clouded with a bunch of miscellaneous files.
To put things into perspective, the directory tree looks something like this:
\Panasonic
\PRG00A
\PRG00B
...
\PRG069
Panasonic is located inside a bunch of folders, hence why I would like to put my batch file alongside \Panasonic. And have it work relatively to its location.
So basically I want to create a batch file move.bat, which shall traverse the subdirectories of \Panasonic and move out any video files (with extension .MOD to simplify things) and afterwards delete the parent directory (e.g. \PRG00B).
The result would be that the \Panasonic directory only includes video files instead of sub-directories with a bunch of rubbish.
What I've got so far (keep in mind that this is my first batch script, and I haven't even tested it fully). The choice to continue doesn't work, by the way. Not sure why, though.
#echo off
cls
set dirName=%~dp0Panasonic
goto question
:start
goto move
goto end
:move
for /D %%G in ("%cd%") do (
for %%I in ("%%G") do (
if %%I equ "*.MOD" (
move /Y %%I %dirName%
)
)
rmdir /s /q %%G
)
:end
echo Done.
pause
endlocal
exit
:question
set /P c="Are you sure you want to proceed with moving video files from %dirName%? [Y/N]"
if /I %c% equ 'y' (
echo Moving files...
goto start
) else (
goto end
)
Once again, this is my first time creating a batch file, so any help is much appreciated!
you are almost there, but just need to fix a couple of things, very easy to fix, in fact, you just need to simplify a lot your code.
Just in three simple steps
Step 1. To loop over all the directories you already had it right, your friend is for /d
for /d %%a in (*) do echo %%a
Step 2. To move all the .mod files in each of the directories found, to its parent directory or one directory up in the hierarchy, that happens to be the current directory, you just need to
move %%a\*.mod .
don't use /y option, so it will not overwrite existing files already moved to the parent directory (You will have the opportunity the check the results later. Keep reading)
Step 3. And finally, remove the directory,
rd %%a
but don't use /s, so it will only work it the directory is empty, that is, if you have successfully moved out all of the files it contained. This way you can then browse thru them to see what is left without losing any data.
So, your moveupallmod.bat becomes simply
#echo off
for /d %%a in (*) do (
move "%%a\*.mod" .
rd "%%a"
)
and that's all!

Windows batch file - process one file at a time

Odd question - but it's driving me a bit crazy. I have a directory where multiple files can be dumped via FTP, then I need to process them one at time. So basically in this directory I could have 1.txt, 2.txt, 3.txt etc then I need to:
copy the file to an archive (if exist copy to archive)
move the file to one specific filename one at a time - data.txt <--- this is what's getting me
run a command on a legacy backend system client using that specific filename (data.txt)
run another command on legacy client using data.txt
delete data.txt
Move on to the next file and repeat
So far I've tried several methods of do loops without any luck - they all get hung up on trying to rename multiple files into one file, and that just kills me. I'd long ago since given up on batch files but annoyingly, this application has to use windows, and Server 2003 to boot.
EDIT: Here's what I've tried-
This works to do one file at a time:
if exist c:\jail\ftp*.txt copy c:\jail\ftp*.txt w:\scans\archive*.txt
if exist c:\jail\ftp*.txt move c:\jail\ftp*.txt w:\data.txt
if exist w:\data.txt C:\temp\rmtcmdb.exe
if exist w:\data.txt del w:\data.txt
I've tried multiple for loops without success, here is the latest (NOTE - I'm just trying to get past the move stage on this one, once I'm done with that I'll add in the rest):
setlocal ENABLEDELAYEDEXPANSION
FOR /f %%a IN ("c:\jail\ftp\") DO (
CALL SET /A x = !x! +1
if !x! == 1 (
CALL copy %%a w:\scans\archive*.txt
CALL move %%a w:\data.txt
)
)
I've also tried some very basic for loops, and again - nothing is getting past the move stage.
Any suggestions?
#echo off
setlocal enableextensions
for %%a in ("c:\jail\ftp*.txt") do (
set "fileName=%%~na"
setlocal enabledelayedexpansion
copy "%%~fa" "w:\scans\!fileName:*ftp=archive!%%~xa"
endlocal
move /y "%%~fa" "w:\data.txt"
start "" /wait "c:\temp\rmtcmdb.exe"
if exist "w:\data.txt" del /s "w:\data.txt" >nul 2>nul
)

Writing multiplie lines in a single file using batch file

I am in the middle of a batch file programming and i got stuck in this below script.
(
IF EXIST h:\*.png del h:*.png
IF EXIST h:\*.mov del h:*.mov
) > file.txt
My intention is the script first finds png and mov format in a given drive (in this case h)and then if exists it deletes it. i want to write all process to a txt file (file.txt).
As this is a simple question i really don't want to ask in main SE. I tried first in chat (hello world room). But i didnt get any useful replay regarding it.
Advance thanks for your help
you might try this:
#echo off &SETLOCAL
(
IF EXIST h:\*.png (
DIR /b h:\*.png
del h:\*.png
)
IF EXIST h:\*.mov (
DIR /b h:\*.mov
del h:\*.mov
)) > file.txt
TYPE file.txt
No need for IF. A simple one liner will do, and it is easy to add additional extensions:
>file.txt (for %%X in (png mov) do 2>nul dir /b h:*.%%X && del h:*.%%X)
EDIT
Shoot, it can be made even simpler:
>file.txt dir /b h:*.png h:*.mov && del h:*.png h:*.mov
Or, if you want to specify the list only once:
set "list=h:*.png h:*.mov"
>file.txt dir /b %list% && del %list%

CMD delete files

Perhaps someone can be of help; I have several files with the following naming convention:
fooR1.txt, fooR2.txt, fooR3.txt, . . . , fooR1000.txt
I wish to delete all the files greater than R500. I have several folders and I know how to pass through each folder, but I am not sure how to capture and delete the files with replication 501 and greater. How can I do such?
How about simply:
ren foo500.txt foo499bis.txt
del fooR5??.txt fooR6??.txt fooR7??.txt fooR8??.txt fooR9??.txt fooR10??.txt
ren foo499bis.txt foo500.txt
Not elegant, but efficient.
This will delete all files fooR###.txt where ### is greater than 500.
#echo off
setlocal EnableDelayedExpansion
for %%f in (fooR*.txt) do (
set num=%%~f
set num=!num:~4,-4!
if !num! gtr 500 del /q "%%~f"
)
endlocal
Because your range is open, I've reversed your criteria: delete anything that is not in the range 1-499. Please be aware that this is not exactly equivalent to yours, for example it will also delete a file named fooR001.txt or fooR_something_else.txt
It's also pretty slow.
#echo off
for %%F in (fooR*.txt) do (
echo %%F | findstr /v /r "fooR[1-9]\.txt fooR[1-9][0-9]\.txt fooR[1-4][0-9][0-9]\.txt" >nul && echo del %%F
)
First line (for) enumerates files starting with fooR, then for each file findstr checks if it does not match pattern (/v option) and finally a command is executed if a check (ie does not match) is positive (&& means execute only if previous command was successfull).
Code above will just echo commands, not execute them, so you may safely run it to verify it actually behaves as it should. To actually run delete, just remove echo in front of it.
note: you could actually run this directly from command line in a form of:
#for %F in (fooR*.txt) do #echo %%F | findstr /v /r "fooR[1-9]\.txt fooR[1-9][0-9]\.txt fooR[1-4][0-9][0-9]\.txt" >nul && echo del %F
You would need to make a Batch script for this. Then in the Batch file you could write.
DEL "fooR500.txt"
To delete all files with a .txt ending you would just write:
DEL "*.txt"
That's all I know, but if you want to get it so it does files 500 and higher you would have
to create a variable in Batch that holds the value 500 using:
set Value = 500
and then have it delete file "fooR" + Index + ".txt" so to do that you would have to do:
set "FilePre = fooR"
set "FileW = %FilePre% %Value%"
set "Ex = .txt"
set "FileX = %FileW% %Ex%"
del FileX
Then you will have to make Value go up by one and repeat the process 500 times until it reaches 1000.

Resources