中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-29 08:05
47,816 topics / 349,916 posts / today 0 new / 48,265 members
DOS批处理 & 脚本技术(批处理室) » How to use the for command to delete autorun.inf in each partition
Printable Version  5,044 / 7
Floor1 kmlxk Posted 2006-09-24 04:53
初级用户 Posts 17 Credits 45
To delete autorun.inf with hidden attributes in each partition C..D..E.....J using a batch file, you can use a loop like this:

```batch
@echo off
for %%i in (C D E F G H I J) do (
if exist %%i:\autorun.inf (
attrib -s -h -r %%i:\autorun.inf
del /f /q %%i:\autorun.inf
)
)
```

This batch file loops through each drive from C to J, checks if autorun.inf exists on that drive, removes the hidden, system, and read-only attributes, and then deletes the file.
Floor2 redtek Posted 2006-09-24 05:32
金牌会员 Posts 1,147 Credits 2,902
Principle:

1. Enumerate all drive letters using for
2. Delete specified files

* Is there any command that is convenient for deleting files with read-only or hidden attributes?
You can list hidden files by using the /a parameter of dir, which is convenient for finding and testing~ : )
The DEL command (in the DOS environment under Windows) can help with deletion, and you don't need to use Attrib.

DEL command usage help:
=============================================================

D:\>del /?
Deletes one or more files.

DEL attributes]] names
ERASE attributes]] names

names Specifies one or more files or directory lists. Wildcards can be used
to delete multiple files. If a directory is specified, all files in the
directory will be deleted.

/P Prompts for confirmation before deleting each file.
/F Forces deletion of read-only files.
/S Deletes specified files from all subdirectories.
/Q Quiet mode. Does not require confirmation when deleting global wildcards.
/A Selects files to delete based on attributes.
attributes R Read-only files S System files
H Hidden files A Archive files
- Prefix indicating "no"

If command extensions are enabled, DEL and ERASE change as follows:

The display syntax of the /S switch is reversed, that is, only the deleted files are displayed, and the files not found are not displayed.

=============================================================


Deleting all files in all directories on a drive specified by you is extremely dangerous!
===============
D:\>del /p /f /s /a d:*.bak
===============

The /P parameter is added after Del. This is to prompt before deletion when testing whether your self-written batch processing is correct.
This sentence is necessary during testing!


///////////// The following commands are dangerous, and improper operation will result in all data being lost ///////////////

I made a typo in the command during testing:
(Del d: is wrong here, it's a habit, so the *.bak behind it is ignored)
====================
D:\>del d: /f /s /q /p *.bak
D:\hosts, do you want to delete (Y/N)? n
D:\Windows Server 2003 For Intel.GHO, do you want to delete (Y/N)? n
D:\# 29 Machine Test Preparation\# Exam Special Directory.rar, do you want to delete (Y/N)? n
D:\# 29 Machine Test Preparation\j2ee.rar, do you want to delete (Y/N)?
====================
Fortunately, I got used to using "prompt" for testing and did not choose the quiet mode of the /Q parameter, because the quiet mode parameter does not require your confirmation, and all things will be deleted before you can regret it!

When testing other fors, it is best to use ECHO instead of actually doing the things.
Fortunately, I have 17GB of data. Check more for possible accidents, haha...



Correct command operation:

///////////////////////// Note: Improper operation is also dangerous ///////////////////////

====================================
del /f /s /a /q d:*.bak
====================================
The above instruction is to delete all *.BAK files with any attributes in all directories and subdirectories on drive D: and delete them directly without manual confirmation!

Therefore, please do not use dangerous operations like *.* later.



If it is what you want:
====================================
del /f /s /a /q d:autorun.inf
====================================


You can enumerate all drive letters through for to automatically delete specified files on all drives.
Floor3 redtek Posted 2006-09-24 05:34
金牌会员 Posts 1,147 Credits 2,902
An experiment done, using echo to avoid unexpected situations :)

C:\TEMP>copy con test.bat
@ECHO off
FOR %%c in (Z,Y,X,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C) do (
IF exist %%c: (
echo del /f /s /a /q %%c:*.bak
)
)



^Z
1 file(s) copied.

C:\TEMP>test.bat
del /f /s /a /q G:*.bak
del /f /s /a /q F:*.bak
del /f /s /a /q E:*.bak
del /f /s /a /q D:*.bak
del /f /s /a /q C:*.bak
C:\TEMP>
Floor4 redtek Posted 2006-09-24 05:36
金牌会员 Posts 1,147 Credits 2,902
This is the code that the original poster wants to automatically delete all autorun.inf files with any attributes in any directory on all disks:

@ECHO off
FOR %%c in (Z,Y,X,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C) do (
IF exist %%c: (
del /f /s /a /q %%c:autorun.inf
)
)

[ Last edited by redtek on 2006-9-24 at 05:37 ]
Floor5 namejm Posted 2006-09-24 06:59
荣誉版主 Posts 1,737 Credits 5,226 From 成都
A similar issue was discussed the other day. Please refer to this thread: Batch Delete All Same - Named Specific File Virus on Hard Disk
Floor6 kmlxk Posted 2006-09-24 07:26
初级用户 Posts 17 Credits 45
Hehe, sorry about that. I was eager and asked without looking further back.
Floor7 SunRiseBoy Posted 2007-06-29 02:15
初级用户 Posts 20 Credits 38
If you only want to delete the specified files in the root directory of the disk, how to modify the For? Do not delete the INF in the subdirectories. (Too stupid, I didn't understand the 4th floor. I ran it and deleted the autorun.inf in my driver backup that was not in the root directory.)
Floor8 lxmxn Posted 2007-06-29 02:44
版主 Posts 4,938 Credits 11,386
Originally posted by SunRiseBoy at 2007-6-29 02:15:
If you only delete the specified file in the root directory of the disk, how to change the For, without deleting the INF in the subdirectory (too stupid, I didn't understand the 4th floor, I ran it and deleted the autorun.inf in my driver backup not in the root directory).

[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023