Monday 15 January 2018

Batch file to remove folders

There was a post by Eric G quite some time ago about this, which went like so:


@echo off 
REM set the name of the directory you would like to target for deleting
set dirname=SAMPLE

REM set the following to "true" if you want to select any directory that includes the name, e.g., a wildcard match
set usewildcard=true


REM --DO NOT EDIT BELOW THIS LINE---------------

REM sentinel value for loop
set found=false

REM If true surround in wildcards
if %usewildcard% == true (
set dirname=*%dirname%*
)

REM use current working directory or take the directory path provided as the first command line parameter
REM NOTE: do not add a trailing backslash, that is added in the for loop, so use "C:" not "C:\"
set directorytosearch=%cd%
if NOT "%1%" == "" (
set directorytosearch=%1%
)
echo Searching for %dirname% in %directorytosearch%


REM /r for files
REM /d for directories
REM /r /d for files and directories
for /d %%i in (%directorytosearch%\%dirname%) do (
IF EXIST %%i (
REM change the sentinel value
set found=true

echo Deleting the folder %%i
REM Delete a folder, even if not empty, and don't prompt for confirmation
rmdir /s /q %%i
)
)

REM logic to do if no files were found
if NOT "%found%" == "true" (
echo No directories were found with the name of %dirname%
)

I've slightly altered it to this:


@echo off 

REM set the name of the directory you would like to target for deleting
set dirname=QBBackupTemp

REM set the following to "true" if you want to select any directory that includes the name, e.g., a wildcard match
set usewildcard=true


REM --DO NOT EDIT BELOW THIS LINE---------------

REM sentinel value for loop
set found=false

REM If true surround in wildcards
if %usewildcard% == true (
set dirname=*%dirname%*
)

REM echo Folder to delete is %dirname%

REM use current working directory or take the directory path provided as the first command line parameter
REM NOTE: do not add a trailing backslash, that is added in the for loop, so use "C:" not "C:\"
REM set directorytosearch=%cd%
REM if NOT "%1%" == "" (
REM set directorytosearch=%1%
set directorytosearch=D:\Quickbooks

pause

echo %directorytosearch%

REM )
echo Searching for %dirname% in %directorytosearch%

pause

REM /r for files
REM /d for directories
REM /r /d for files and directories
for /d %%i in (%directorytosearch%\%dirname%) do (
IF EXIST %%i (
REM change the sentinel value
set found=true

echo Deleting the folder %%i

pause

REM Delete a folder, even if not empty, and don't prompt for confirmation
rmdir /s /q %%i
)
)

REM logic to do if no files were found
if NOT "%found%" == "true" (
echo No directories were found with the name of %dirname%
)

pause

Unfortunately, it does not work and I get the following output:



Press any key to continue...
D:Quickbooks
Searching for *QBBackupTemp* in D:\Quickbooks
Press any key to continue...
Deleting the folder D:\Quickbooks\QBBackupTemp Mon, Dec 19 2016 10 01 24 PM
Press any key to continue...
The system cannot find the file specified.
(That line is repeated another 8 times)
Deleting the folder D:\Quickbooks\QBBackupTemp Mon, Dec 19 2016 10 05 32 PM
Press any key to continue...
The system cannot find the file specified.
(That line is repeated another 8 times)
Press any key to continue...

What is wrong with code?

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...