4

I am trying to make a batch file to delete malicious files from pendrive. I know that these malicious files uses hidden,read only and system attributes mainly to hide itself from users. Currently i am deleting these files using cmd by removing malicious files attributes then deleting it. Now I am thinking to make a small batch file which can be used to remove these files just by entering the drive letter.

I have found this code in a website to find attributes of a file. But after entering the name of the file the batch file just exits without showing any results.

    @echo off
setlocal enabledelayedexpansion
color 0a
title Find Attributes in Files

:start
set /p atname=Name of the file:
if not exist %atname% (
cls
echo No file of that name exists!
echo.
echo Press any key to go back
pause>nul
goto start
)
for /f %%i in (%atname%) do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
echo Press any key to go back
pause>nul
goto start

can you tell me why this batch file is exiting without showing any results. Or can you give any better batch script for getting attributes of a file.

EDIT

I was able to work the above code only for a single file. As my purpose of my batch file is to remove malicious files by entering the drive letter. How can i use it to find what kind of attributes files are using in a particular drive.

For example: In cmd we can use this command to find the file attributes of a given drive

attrib *.*

Advance thanks for your help

Eka
  • 14,170
  • 38
  • 128
  • 212
  • Deleting every file with those attributes is going to damage programs and systems. I think your plan should be modified... – foxidrive Sep 07 '13 at 03:48
  • @foxidrive i am making this batch file only for pendrive and not for any other drives. – Eka Sep 07 '13 at 06:32

3 Answers3

3

You're using a for /f loop here, which isn't necessary (and may yield undesired results if the filename contains spaces). Change this:

for /f %%i in (%atname%) do set attribs=%%~ai

into this:

for %%i in ("%atname%") do set attribs=%%~ai
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
2

I tried the bat file (without inspecting the details) and it seems to work fine for me. What I noticed is that it closes instantly if you don't enclose file path with quotation marks - e.g. "file". Example:

Name of the file: path\file.txt // this will close immediately
Name of the file: "path\file.txt" // now it will stay open and display the result

This hopefully solves your problem.

As far as your question in EDIT is concerned, a simple option is to iterate a list of files and execute the batch on each one.

batch1.bat: (%1 refers to the first command-line parameter)

@echo off
setlocal enabledelayedexpansion

echo %1
set atname=%1

for %%i in ("%atname%") do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.

Next, generate a list of all files within a given path (say 'folder' including all subfolders):

dir /s /b folder > ListOfFiles.txt

main.bat (read ListOfFiles.txt line-by-line and pass each line to batch1.bat as a command line parameter):

@echo off
for /f "tokens=*" %%l in (ListOfFiles.txt) do (batch1.bat %%l)

Then, from cmd:

main.bat >> output.txt

The last step generates an output file with complete results. Granted, this can be done in a more polished (and probably shorter) way, but that's one obvious direction you could take.

w128
  • 4,680
  • 7
  • 42
  • 65
0

This is dangerous code - but it'll delete read only, hidden and system files. It should fail to run on c: drive but I haven't tested it. Note that some Windows installs are on drives other than c:

@echo off
echo "%cd%"|find /i "c:\" >nul || (
del *.??? /ar /s /f
del *.??? /ah /s
del *.??? /as /s
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68