如何使用批处理文件来获取文件的属性 [英] How to get attributes of a file using batch file

查看:312
本文介绍了如何使用批处理文件来获取文件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个批处理文件,从U盘删除恶意文件。我知道,这些恶意文件将使用隐藏,只读,系统主要归因于从用户隐藏自身。目前,我通过删除恶意文件删除使用CMD这些文件的属性,然后将其删除。现在,我想使可用于只需输入盘符删除这些文件的一小批文件。

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.

我发现这code在网站上找到一个文件的属性。但在进入批处理文件刚刚退出的文件的名称,而不显示任何结果后

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.

修改

我是能够工作的上述code只为​​单个文件。正如我的我的批处理文件的目的是通过输入盘符删除恶意文件。我怎样才能用它来寻找什么样的属性的文件正在使用一个特定的驱动器。

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.

例如:
在cmd中我们可以使用这个命令来查找某一驱动器的文件属性

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

attrib *.*

先谢谢您的帮助。

Advance thanks for your help

推荐答案

我试过bat文件(不检查的详细信息),它似乎为我工作的罚款。我注意到的是,它会关闭瞬间,如果你不带引号文件路径 - 例如文件。例如:

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

这有望解决您的问题。

至于你的问题的修改而言,一个简单的选择是迭代的文件列表及每一个执行批处理。

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指的是第一个命令行参数)

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 的(读ListOfFiles.txt行由行,并通过每行batch1.bat作为命令行参数):

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)

然后,从CMD:

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.

这篇关于如何使用批处理文件来获取文件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆