How to get file attributes using a batch file

I am trying to make a batch file to remove malicious files from pendrive. I know that these malicious files use hidden, read-only and system attributes, which are mostly hidden from users. I am currently deleting these files with cmd, deleting the attributes of the malicious files and then deleting them. Now I'm going to make a small batch file that you can use to delete these files by simply typing the drive letter.

I found this code on a website to find file attributes. But after entering the file name, the batch file just exits without 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 

You can tell me why this batch file comes out without any results. Or you can give a better batch of script to get file attributes.

EDIT

I was able to process the above code for only one file. Since my goal of my batch file is to remove the malicious files by typing the drive letter. How can I use it to find which attribute files are used on a particular drive.

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

 attrib *.* 

Thank you for your help.

+4
source share
3 answers

I tried the bat file (without checking the details) and it seems to work fine for me. I noticed that it closes instantly if you do not add the path to the file with quotes - for example. "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 for your question in EDIT , a simple option is to iterate over the list of files and execute a package on each of them.

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. 

Then create a list of all files in the specified path (for example, "folder", including all subfolders):

 dir /s /b folder > ListOfFiles.txt 

main.bat (read ListOfFiles.txt each time 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 full results. Of course, this can be done in a more polished (and probably shorter) way, but this is one obvious direction you could take.

+2
source

Here you use a for /f loop, which is optional (and may produce unwanted results if the file name contains spaces). Change this:

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

in it:

 for %%i in ("%atname%") do set attribs=%%~ai 
+2
source

This is dangerous code, but it will only remove read, hidden and system files. It should not work on c: drive, but I have not tested it. Note that some Windows installations are located on drives other than c:

 @echo off echo "%cd%"|find /i "c:\" >nul || ( del *.??? /ar /s /f del *.??? /ah /s del *.??? /as /s ) 
0
source

Source: https://habr.com/ru/post/954117/


All Articles