I have to delete all *.pdf files under folder and its subfolders using command prompt.
Del *.pdf /s/q works .
But if any one of the file is having long name it fails and also not ignored and moves further.
Any idea?
Loading
I have to delete all *.pdf files under folder and its subfolders using command prompt.
Del *.pdf /s/q works .
But if any one of the file is having long name it fails and also not ignored and moves further.
Any idea?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjay SharmaPosted Sep 5, 2023, 8:20 AM
None of the answers provided working.
Ashok DudiPosted Aug 19, 2023, 11:15 AM
You can use the
forcommand in combination with thedelcommand to handle files with long names when deleting *.pdf files under a folder and its subfolders in Command Prompt. Theforcommand will help you iterate through each file and delete it, even if the filename is long. Here's how you can do it:Open Command Prompt and navigate to the folder where you want to delete the *.pdf files and run the following command:
Here's what the command does:
for /r /d %x in (*.pdf)iterates through all files with the *.pdf extension in the current directory and its subdirectories.do del "%x"deletes each file with a long filename. The%xrepresents the filename, and the quotes around"%x"are important to handle filenames with spaces or special characters.Please note that if you're running this command in a batch file (a
.batfile), you need to double the percentage signs for the loop variable. In that case, the command would be:Gurpreet AroraPosted Aug 18, 2023, 6:46 AM
/r %G in (*.pdf) do del "%G"
Cr BhargaviPosted Aug 17, 2023, 5:14 PM
If you're encountering issues with long file names when using the `del` command in Command Prompt to delete PDF files recursively, you can try using the `for` loop along with the `del` command. This approach allows you to handle long file names more effectively. Here's how you can do it:
1. Open Command Prompt:
Open the Command Prompt by searching for "cmd" in the Windows Start menu.
2. Navigate to the Folder:
Use the `cd` command to navigate to the directory where you want to delete the PDF files and its subfolders.
Example
cd C:\Path\To\Your\Folder
3. Run the Command:
Use the following command to delete all PDF files recursively while handling long file names:
for /r %G in (*.pdf) do del "%G"
This command iterates through all PDF files recursively and deletes each file.
- `/r` tells the `for` command to execute the specified command for all files in the specified directory and its subdirectories.
- `%G` represents the file name being processed.
- `del "%G"` deletes the current file.
This approach should handle long file names more effectively compared to the direct `del *.pdf /s/q` command. However, please be cautious when using commands that involve file deletion, especially in a recursive manner. Double-check the directory you're in and the command you're executing before proceeding.
Ravikant SahuPosted Aug 10, 2023, 12:48 PM
Try Using this command -
for /r %i in (*.pdf) do del "%i"
Mohammad HussainPosted Aug 8, 2023, 12:44 PM
If you're encountering issues with the
delcommand failing due to long file names, you can use therobocopycommand to work around this limitation.robocopyis a robust file copying tool in Windows that can also be used for deleting files.Here's how you can use
robocopyto delete all*.pdffiles under a folder and its subfolders:robocopy "C:\your\folder\path" "C:\your\folder\path" *.pdf /s /min:0 /create /w:0 /purge
Let's break down the parameters used:
"C:\your\folder\path": Replace this with the actual path of the folder where you want to delete PDF files./s: Copies subdirectories (but not empty ones)./min:0: Specifies that files with a modification date of today or later should be copied./create: Creates a simple directory tree structure without copying any files./w:0: Specifies the wait time in seconds between retries (0 seconds in this case)./purge: Deletes files from the destination that no longer exist in the source.Using
robocopythis way allows you to effectively delete files while bypassing the long file name issue. Since we're using the/createswitch, it only creates the directory structure without actually copying any files.Remember to replace
"C:\your\folder\path"with the actual path of the folder where you want to delete the*.pdffiles. Always be cautious when using commands that delete files, as the operation cannot be undone.