China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-14 23:27
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use the for command to delete autorun.inf in each partition View 4,887 Replies 7
Original Poster Posted 2006-09-24 04:53 ·  中国 重庆 电信
初级用户
Credits 45
Posts 17
Joined 2006-09-23 13:24
19-year member
UID 63469
Status Offline
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.
Floor 2 Posted 2006-09-24 05:32 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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.
Floor 3 Posted 2006-09-24 05:34 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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>
Floor 4 Posted 2006-09-24 05:36 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
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 ]
Floor 5 Posted 2006-09-24 06:59 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
A similar issue was discussed the other day. Please refer to this thread: Batch Delete All Same - Named Specific File Virus on Hard Disk
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-09-24 07:26 ·  中国 重庆 电信
初级用户
Credits 45
Posts 17
Joined 2006-09-23 13:24
19-year member
UID 63469
Status Offline
Hehe, sorry about that. I was eager and asked without looking further back.
Floor 7 Posted 2007-06-29 02:15 ·  中国 湖北 十堰 郧西县 联通
初级用户
Credits 38
Posts 20
Joined 2006-07-04 16:50
20-year member
UID 58001
Status Offline
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.)
Floor 8 Posted 2007-06-29 02:44 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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).


@echo off
rem Specified file file
set file=a.txt
for %%a in ( c d e f g h i j k l ) do (
if exist %%a:\%file% del %%a:\%file%
)
Forum Jump: