List or delete hidden files from command prompt(CMD)

In Windows, files/folders have a special attribute called hidden attribute. By setting this attribute, we can hide files from being displayed in explorer or command prompt. This article explains how to list this hidden files in windows command line and it also discusses how to delete the hidden files.

To get the list of hidden files from a directory you can run the below command.

dir directory_path /A:H /B

Example:

Get the list of hidden files from C:\Windows\system32 folder.

To get the list of hidden files from all sub directories we need to add /S switch to the command.

dir directory_path /A:H /S /B

Example:
To get the list of hidden files from the folder c:\windows\system32 and from all its subfolders we need to run the below command.

dir c:\WINDOWS\system32 /A:H /B /S

List all hidden folders:

If you want to get the list of all hidden subfolders in a folder, you can run the below command.

dir /s /b /A:DH

Hidden files deletion

To delete hidden files from command prompt we can use Del command. For example to delete a hidden file named example.doc we need to run the below command.

del /A:H example.doc

Note that /A:H is necessary otherwise you will get ‘file not found’ error like below.

C:\>del example.doc
Could Not Find C:\example.doc

To delete all hidden files from a given directory we can run the below command.

del directory_path /A:H

Alternatively you can cd to that directory and then run the below command.

del * /A:H

To delete hidden files from subfolders also you can do that by adding /S switch

del * /A:H /S

If you have the list of hidden folders, then you can delete them all by the following command.
rmdir /s /q hiddenfolder1 hiddenfolder2 hiddenfolder3 .....

If you want to delete all hidden subfolders from a drive/folder then you first need to generate the list of folders and then delete them. This can be done with the below command.
for /F %i in ('dir /s /b /A:DH') do rmdir /s /q %i

Run the above command from the drive or top folder.
Disclaimer: Be cautious while using these commands, as any mistake would lead to data loss. Test and use them at your own risk.

 

How to delete a single file

Open the command prompt by typing “CMD” in the search field in the taskbar and clicking on “Command Prompt”. Navigate to the folder containing the file to be deleted by using the ‘cd’ command. Type the following command in the command prompt:

DEL /F /Q /A name_of_the_file

/F stands for force delete
/Q will not show Y/N confirmation and will delete the files silently
/A selects only the files with ready for archiving attribute

Alternatively, you can also use the file path directly. For example you can type:

DEL /F /Q /A C:\Users\HP1\Documents\file.txt

If the file is deleted, you will not see any error message.

How to delete a folder

Open the command prompt by typing “CMD” in the search field in the taskbar and clicking on “Command Prompt”. Navigate to the folder containing the folder to be deleted by using the ‘cd’ command. Type the following command in the command prompt:

RD /S /Q name_of_the_folder

RD stands for remove directory.

/S causes the deletion of all subfolders and files

/Q will not show Y/N confirmation and will delete the files silently

Alternatively, you can also use the folder path directly. For example you can type:

RD /S /Q C:\Users\HP1\Documents\folder1

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

DEL

Delete one or more files.

Syntax
      DEL [options] [/A:file_attributes] files_to_delete

Key
   files_to_delete : A filename or a list of files, may include wildcards.

   options:
      /P  Give a Yes/No Prompt before deleting. 
      /F  Ignore read-only setting and delete anyway (FORCE) 
      /S  Delete from all Subfolders (DELTREE)
      /Q  Quiet mode, do not give a Yes/No Prompt before deleting.

      /A  Select files to delete based on file_attributes
            file_attributes:
              R  Read-only    -R  NOT Read-only
              A  Archive      -A  NOT Archive
              S  System       -S  NOT System
              H  Hidden       -H  NOT Hidden
              I  Not content indexed  -I  content indexed files
              L  Reparse points       -L  NOT Reparse points

              X  No scrub file attribute  -X  Scrub file attribute   (Windows 8+)
              V  Integrity attribute      -V  NO Integrity attribute (Windows 8+)

   Wildcards: These can be combined with part of a filename

      *  Match any characters
      ?  Match any ONE character

If a folder name is given instead of a file, all files in the folder will be deleted, but the folder itself will not be removed.

Errorlevels: DEL will return an Errorlevel of 0, irrespective if the delete succeeds or fails for any reason.
(If you delete files using PowerShell then a True/False return code ($?) will be set correctly.)

Errorlevels

If the files were successfully deleted %ERRORLEVEL% = 0
Bad or no parameters given = 1

Undeletable Files

Files are sometimes created with a very long filename or a trailing period or with reserved names (CON, AUX, COM1, COM2, COM3, COM4, LPT1, LPT2, LPT3, PRN, NUL) and as a result they become impossible to delete with Windows Explorer.

To delete such files use the syntax: DEL "\\?\path to file"
You can also use "\\.\path to device"

e,g,
DEL "\\?\C:\some folder\AZH64GT."
DEL "\\.\C:\Work\LPT1"

Alternatively for long filenames, you can reduce the total path length by using SUBST to map a drive letter to the folder containing the file.

It is also possible to delete long paths using RoboCopy - Copy/Move the required files to a temporary folder and then delete the folder, one gotcha with that technique is RoboCopy's tendency to follow symbolic links which can cause files outside the source folder to be moved/ deleted.

If a file is still 'undeletable' this may be caused by the indexing service, temporarily stop the service and then delete the file.

Permanent deletion

Deleting a file will not prevent third party utilities from un-deleting it again. Secure file deletion utilities are available, however for casual use, you can turn any file into a zero-byte file to destroy the file allocation chain like this:

TYPE nul > C:\examples\MyFile.txt
DEL C:\examples\MyFile.txt

Delete Locked files

Typically this is caused by the Offline Cache or Internet Explorer temp files.

Close all applications
Open a command prompt
Click Start, and then Shut Down
Simultaneously press CTRL+SHIFT+ALT.
While you keep these keys pressed, click Cancel in the Shut Down Windows dialog box.
In the command prompt window, navigate to the cache location, and delete all files from the folder (DEL /s)
At the command prompt, type explorer, and then press ENTER.

DELTREE - Older versions of Windows had the DELTREE command to delete all files and sub folders. This can be replicated with a script as shown on the DELTREE page.

Examples:

Delete "Hello World.txt"

DEL "Hello World.txt"

Delete 3 named files:

DEL file1.txt file2.txt "C:\demo\file3.txt"

Delete all files that start with the letter A

DEL A*

Delete all files that end with the letter A

DEL *A.*

Delete all files with a .doc extension:

DEL *.doc

Delete all read only files:

DEL /a:R *

Delete all files including any that are read only:

DEL /F *

Normally DEL will display a list of the files deleted, if Command Extensions are disabled; it will instead display a list of any files it cannot find.

DEL is an internal command. ERASE is a synonym for DEL

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

DEL "C:\Program Files\IBM\ISAM ESSO\AA\Cryptoboxes\Wallets" /A:H  /F /Q 强制静默删除Wallets目录下的所有文件,包括隐藏文件。不包含子目录


posted on 2019-03-16 13:32  遠離塵世の方舟  阅读(2163)  评论(0编辑  收藏  举报

导航