Windows delete many files - batch-file

i'm trying to delete many files in many sub-folders with random extensions, how can i do that?
I have many encrypted files (ransomware), now i decrypted all files and i have a duplicate each file (30.000 files).
All encrypted files have a random name extensions with 6 characters
like "namefile.pdf.123456" / "namefile.docx.ujyrtf"
Can you help me to write e script to delete all these files?
Are stored in folder and sub-folders
Maybe i can try to use a Multiple File Renamer to rename .jpg. to *.jpg.del and then delete all *.del by cmd, i don't know if i can do this.
I try to use CMD's like this and i type:
del *.jpg.*
but this command deleted all .jpg and all .jpg.*
Thank you
ATTENTION: there aren't any legit files in these folders. It is a folder data with mkv, jpg, doc, xls, etc. Documents, Video, Audio. Not application or Windows Folder or something like that. Don't use this SCRIPT on system partition, program folders because there may be legit files that would be deleted. Thanks Gerard

If all of the files have a 6 digit extension then we can use a findstr regex to delete them.
from cmd
#for /f "delims=" %i in ('dir /b /a-d *.????.* ^| findstr /r "\.[^\.][^\.][^\.][^\.][^\.][^\.]$"') do #echo del "%i"
or in a batch file:
#echo off
cd /d "C:\Path to files\to delete\"
for /f %%i "delims=" in ('dir /b /a-d *.????.* ^| findstr /r "\.[^\.][^\.][^\.][^\.][^\.][^\.]$"') do echo del "%%i"
This will just echo the result, you need to remove echo only once you can confirm that it does not delete un-intended files.

Related

Creating Batch File to search .txt files in a specific folder for a specific string, and move all files to a new folder

Just as the title mentions, I am looking for a way to create a batch file which looks for a specific string in .txt files in a folder on my C drive. Any txt files that contain this string then need to be moved to a different folder.
I can get my batch file to find the items, I just can't figure out how to move all text files which contain the string.
Update: I am now able to get the files to move, with the use of the solution, provided below.
Thanks
Basically what you need to do is capture the output from a command. That is done with the FOR /F command.
FOR /F "delims=" %%G in ('findstr /m /C:"Thread aborted" *.txt') do (
move "%%G" C:\backup\
echo %%G>>logfile.txt
)
Just to show you that this does work. I am running everything from the command line to show you the output.
C:\BatchFiles\Tyler>echo Thread aborted>file.txt
C:\BatchFiles\Tyler>dir /b
backup
file.txt
C:\BatchFiles\Tyler>FOR /F "delims=" %G in ('findstr /m /C:"Thread aborted" *.*') do #move "%G" backup
1 file(s) moved.
C:\BatchFiles\Tyler>dir /b
backup
C:\BatchFiles\Tyler>dir /b backup\*.*
file.txt
C:\BatchFiles\Tyler>
Save this script to sorting.bat, run from open Cmd Prompt, and let me know if any errors. Based on your question, you seems to have no dir structure to bypass in the folder with target files. This code will also help users, who run a similar task and need to look through dir structure:
#echo off
set "sourcedir=C:/Provider.Rates" & set "destindir=C:/Aborted.Threads"
for /R "%sourcedir%" %%G in (*.txt) do (
find /i "Thread Aborted" "%%G" >nul 2>&1 && move /y "%%G" "%destindir%\%%~nxG" >nul)
echo/ & echo Sorting completed.
timeout 5
exit /b

Batch file that would delete all the files and folders that the batch file is located in?

Is it possible to make a batch file that would delete all the files and folders that the batch file is located in?
I.E: I place the batch file into folder with useless files, I run it, and it deletes all the files and folders in that folder.
Then I can just move the batch file to another folder and do the same there...
Would really help me out! I need this to delete temporary files on other people's computers before installing new files... But sadly I am not very familiar with batch.
To remove all the files in the current directory except your batch file use:
echo off
for %%i in (*.*) do if not "%%i"=="del.bat" del /q "%%i"
Note that the "del.bat" is the name of the batch file you save this as.
As another side note you could add after the del /q the "/p" command to make this a little bit safer. That way it will prompt you before deleting each file.
To remove everything in the folder including the .bat file use
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
To remove everything including folders except the .bat file use
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || if not "%%i"=="del.bat" del "%%i" /s/q)
Where del.bat is your batch file name.

Batch empty subfolders from folders in a CSV file

I have a very long list of partail names for folders (each partial is unique to a specific folder); what I am looking to do is write a batch file to go through each of the sub-folders within each folder, pull the files to the root of that folder, and replace the files if there's duplication (there is a lot of duplication we're looking to remove).
I've used a couple batch files to move and do other things with these folders, I just can't get the code to work on emptying subfolders.
Batch file 1 (accessing CSV, calling the other batch to do the work):
#echo off
FOR /F "tokens=1,3 delims=," %%G IN (Pt1Test.csv) DO call Empty1.bat %%G
Batch file 2 (is supposed to empty the subfolders, then move along to the next folder in the list):
#echo off
set _Uname=%1
for /f "eol=: delims=" %%F in ('dir /b^|find "%_Uname%"') do <NEXT BIT I CAN'T FIGURE OUT>
The problem I'm having is getting the command to pull within the main folder.
I know this code works when I throw it right into DOS:
for /r %f in (*) do #move /y "%f"
But I can't have a for, do, for, do and I don't want to have to type that in for the 5000 or so folders I'd like to remove duplication from.
Thanks for any help!
Tyler
Is there a reason for the 1,3? You aren't using the 3 (%%H) in your examples.
How about this? Just change to each directory and back, before your 2nd command.
#echo off
FOR /F "tokens=1,3 delims=," %%G IN (Pt1Test.csv) DO (
pushd *%%G*
for /r %%a in (*) do #move /y "%%a"
popd
)

Rename A Group Of Files

I need to rename a group of files in the same folder.
When I try to run the batch file, it doesn't work correctly:
`ren *.txt Updated_*.txt`
The file names contain date_names_location.txt, examples are below
08232013_name1_nyc.txt
08212013_name1_nyc.txt
08232013_name1_la.txt
08212013_name1_la.txt
When I run the batch file I get back:
Updated_1_name1_nyc.txt
instead of
'Updated_08232013_name1_nyc.txt'
Any ideas on how to fix? Thanks
This is one way:
#echo off
for /f "delims=" %%a in ('dir /b /a-d *.txt') do ren "%%a" "Updated_%%a"
REN has no insert mode, so it just replaces the beginning of your file names. Try the solution provided here
Batch renaming files using Windows 7 REN (adding prefix)?

Batch Script - Empty Multiple Directories In Paths With A Pattern

I'm looking to write a short batch script that will delete all files within a set of directories. More specifically, suppose I have the top directory "workspace" and it contains several directories beginning with the sting "project" (e.g. project-something, project-another). Then each of these "project" directories contain a "model" directory. I want to have the script empty each of these model directories.
I know this is doesn't work, but I looking for something along the lines of
del project*\model\*
But I know that the * after project will not select all directories starting with project then proceed into the model directories to clear them. What would be a correct way to go about doing this?
Thank you for your time!
Put this into a .bat file and run.
#echo off
for /F "usebackq delims=" %%F in (`dir /ad /s /b model`) do (
del /s /q "%%F"
echo Removed "%%F"
)
pause

Resources