How to get attributes of a file using batch file

前端 未结 3 1566
[愿得一人]
[愿得一人] 2020-12-17 04:34

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

相关标签:
3条回答
  • 2020-12-17 05:18

    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.

    0 讨论(0)
  • 2020-12-17 05:23

    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
    )
    
    0 讨论(0)
  • 2020-12-17 05:25

    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
    
    0 讨论(0)
提交回复
热议问题