Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#1
Post
by tinfanide » 03 Apr 2012 07:17
C:\>DIR /B /S "C:\test\New folder"
C:\test\New folder\folder1
C:\test\New folder\folder1\good
C:\test\New folder\folder1\txt.txt
C:\test\New folder\folder1\good\txt.txt
C:\>test\test.bat
Deleted file - C:\test\New folder\folder1\txt.txt
Deleted file - C:\test\New folder\folder1\good\txt.txt
The system cannot find the file specified.
The system cannot find the path specified.
The system cannot find the file specified.
The system cannot find the path specified.
The system cannot find the path specified.
The system cannot find the path specified.
Press any key to continue . . .
C:\>DIR /B /S "C:\test\New folder"
C:\>
Code: Select all
@ECHO OFF
FOR /F "delims=" %%A IN ('DIR /B /S "C:\test\New folder\*"') DO (
DEL /S /Q "%%~fA"
RD /S /Q "%%~fA"
)
PAUSE
What are "The system cannot find the file specified." here referring to?
The codes do what I want (as demonstrated above - delete all the directories and files / subdirectories except the root folder itself).
But how could I get rid of those error lines?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 03 Apr 2012 07:52
Try this: (untested)
Code: Select all
@ECHO OFF
pushd "C:\test\New folder\" && (
FOR /F "delims=" %%A IN ('DIR /A:D /B') DO RD /S /Q "%%~fA"
FOR /F "delims=" %%A IN ('DIR /A:-D /B') DO DEL /S /Q "%%~fA"
)
popd
PAUSE
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 03 Apr 2012 07:54
Or this: (untested)
Code: Select all
@ECHO OFF
pushd "C:\test\New folder\" && (
RD /S /Q "C:\test\New folder\" 2>nul
)
popd
PAUSE
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#5
Post
by tinfanide » 03 Apr 2012 08:41
foxidrive wrote:Or this: (untested)
Code: Select all
@ECHO OFF
pushd "C:\test\New folder\" && (
RD /S /Q "C:\test\New folder\" 2>nul
)
popd
PAUSE
I've got most of it.
But one thing not sure...
2>nul (no redirection?) the nul device?
Why is it needed after RD?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#6
Post
by foxidrive » 03 Apr 2012 18:10
The code will generate an error because the top level folder can't be deleted, and 2>nul removes the harmless error message from the screen.
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#7
Post
by tinfanide » 03 Apr 2012 21:13
foxidrive wrote:The code will generate an error because the top level folder can't be deleted, and 2>nul removes the harmless error message from the screen.
The process cannot access the file because it is being used by another process.
Press any key to continue . . .
Yes, it seems that RD cannot process the top level folder because we PUSHD the top level folder?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#8
Post
by foxidrive » 04 Apr 2012 00:23
Yup. This all assumes you didn't want to remove the top level folder.