Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
phoenix_Rising
- Posts: 38
- Joined: 04 Apr 2012 03:11
#1
Post
by phoenix_Rising » 04 Apr 2012 03:13
Hi guys,
Im new to the board so please go easy!
Basically i'm writing a batch file to delete folders that DONT have a file with a certain extension type in them...
so for example if i have the following folder
C:\MUSIC\FLAC\JAZZ\Jazzysong.ape
i need a bit of code that can delete the folder JAZZ because .ape is the wrong file type for the FLAC folder.
Do you follow? Any help would be awesome guys as i've tried variants of findstr, exist and writing to text file and trying to read the path using dir /s /b
Im lost
![Sad :(](./images/smilies/icon_sad.gif)
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 04 Apr 2012 05:01
Your description is a bit confused.
You said you want to delete folders that DON'T have a specific extension in them
and
You said you want to delete folders that DO have .ape in them.
Think about what you need to do and lay out the rules.
-
abc0502
- Posts: 1007
- Joined: 26 Oct 2011 22:38
- Location: Egypt
#3
Post
by abc0502 » 04 Apr 2012 16:00
If u trying to remove the folder us the "RD" command and if you are trying to remove files use "del"
but if u are trying to remove specific file type use the "FOR" command
RD Command: Remove the folder and its content "quitly"
Code: Select all
@echo off
cls
set path=C:\MUSIC\FLAC\JAZZ
RD /S /Q "%path%
Del Command: delete the .ape file in the folder JAZZ "Quitly"
Code: Select all
@echo off
cls
set path=C:\MUSIC\FLAC\JAZZ
Del /F /S /Q "%path%\*.ape"
Using FOR command to search for all files with the type of ape in the folder MUSIC and delete if found use this:
Code: Select all
@echo off
cls
set path="C:\MUSIC"
for %%a in (ape) do (
del /f /s /q "%path%\*.%%a
)
pause
hope it will help
![Smile :)](./images/smilies/icon_smile.gif)
just remember to adjust the directory in the path variable and be carfull be cuse it delete files
-
trebor68
- Posts: 146
- Joined: 01 Jul 2011 08:47
#4
Post
by trebor68 » 04 Apr 2012 19:02
If you want to check if a folder and subfolder is empty, use this batch file.
Code: Select all
@echo off
cls
:: cd /d %1
echo Folder: %cd%
echo.
for /f "tokens=1 delims=" %%r in ('dir /s /b /a:d') do (
set hlp=NO
for /f "tokens=1 delims=" %%t in ('dir /b "%%r" /a') do set hlp=YES
if !hlp!==NO echo Empty folder: %%r
)
Copy this file in the folder that you will check. Any empty folder will display. If there hidden files in the folder is this folder not empty.
-
phoenix_Rising
- Posts: 38
- Joined: 04 Apr 2012 03:11
#5
Post
by phoenix_Rising » 05 Apr 2012 03:47
Thanks guys... sorry foxi if it wasnt too clear i was sooo tired when i wrote it!
basically the example would be.
IF the subfolders folders within C:\MUSIC\FLAC do NOT have a file with a .ape extension in them (Or any of there subdirectorys) THEN i want to delete it there root folder.
As a further example if folder C:\MUSIC\FLAC\JAZZ does NOT have a file with .ape extension in it... then i want to delete its folder and subfolders
C:\MUSIC\FLAC\JAZZ
C:\MUSIC\FLAC\JAZZ\CUE
C:\MUSIC\FLAC\JAZZ\Images
C:\MUSIC\FLAC\JAZZ\Misc
I want the command to run over ALL folders within the C:\MUSIC\FLAC folder.
Sorry for the poor examples but i think you giys get the gist!
-
trebor68
- Posts: 146
- Joined: 01 Jul 2011 08:47
#6
Post
by trebor68 » 05 Apr 2012 04:13
I have change my batch file.
This batch will check any subfolder from this folder. If you use a parameter then will check the subfolder in the parameter.
The results are displayed the subfolder and counted the files and subfolder in this.
Batch file: FilesInFolder.bat
Code: Select all
@echo off
setlocal ENSABLEEXTENSIONS ENSABLEDELAYEDEXPANSION
cls
if "%~1"=="" (set here=%cd%) else (set here=%~1)
echo Folder: "%here%\"
echo.
for /f "tokens=1 delims=" %%r in ('dir /s /b /a:d "%here%"') do (
set hlp=0
set hlp2=%%r
for /f "tokens=1 delims=" %%t in ('dir /a /b "%%r"') do set /a hlp+=1
echo "!hlp2:%here%\=!" ### !hlp! files
)
To check your folder use the command: FilesInFolder C:\MUSIC\FLAC\JAZZ
If you will see only the empty folder change the last ECHO command:
Code: Select all
if !hlp!==0 echo Empty folder: "!hlp2:%here%\=!"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 05 Apr 2012 05:33
EDIT: I used the reverse logic for some reason. Deleted.
This is untested. It should log which of the folders in C:\MUSIC\FLAC have a .ape file and where it is, in the subdirectories.
If it does what you need, then changing it to delete the entire folder branch in C:\MUSIC\FLAC can be done.
Last edited by
foxidrive on 09 Apr 2012 02:25, edited 1 time in total.
-
Fawers
- Posts: 187
- Joined: 08 Apr 2012 17:11
-
Contact:
#8
Post
by Fawers » 08 Apr 2012 21:57
phoenix_Rising wrote:IF the subfolders folders within C:\MUSIC\FLAC do NOT have a file with a .ape extension in them (Or any of there subdirectorys) THEN i want to delete it there root folder.
I thought of this:
Code: Select all
@echo off
cd /d C:\MUSIC\FLAC\JAZZ
for /f "delims=" %%a in ('dir /b /s /ad') do (
if not exist "%%a\*.ape" (
>>folders.log echo %%a
rd /s /q "%%a"
)
)
It's pretty simple compared to the examples above, but it might work.
Since the current directory of the batch file is C:\MUSIC\FLAC\JAZZ, folders.log will be created in there.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 09 Apr 2012 02:32
This is designed to check each folder in "C:\MUSIC\FLAC" and if the folder-and-subdirectories does not have any .ape files then it will be echo'd to the screen along with the rd command.
If you can verify that it is correct then remove the echo before the RD command to delete the folders.
Code: Select all
@echo off
pushd "C:\MUSIC\FLAC" && (
for /f "delims=" %%a in ('dir /ad /b') do (
set "flag="
for /f "delims=" %%b in ('dir "%%a\*.ape" /a-d /b /s 2^>nul') do set flag=1
if not defined flag (
echo "%%a" has no .ape files in it or the subdirectories
echo rd /s /q "%%a"
pause
)
)
)
popd
Edited to functionally add a last closing bracket - )
Edited to add hiding of 'file not found' message.
Edited to correct /b switch (missing slash)
Last edited by
foxidrive on 10 Apr 2012 03:15, edited 3 times in total.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#10
Post
by foxidrive » 09 Apr 2012 02:33
Fawers wrote:phoenix_Rising wrote:IF the subfolders folders within C:\MUSIC\FLAC do NOT have a file with a .ape extension in them (Or any of there subdirectorys) THEN i want to delete it there root folder.
It's pretty simple compared to the examples above, but it might work.
It will delete only the folders without .ape files - not the parent and all subdirectories.
-
phoenix_Rising
- Posts: 38
- Joined: 04 Apr 2012 03:11
#11
Post
by phoenix_Rising » 09 Apr 2012 12:40
Thanks guys, i've tried all the example code and nothings done it yet but i think were getting closer!
@Foxidrive - I tried your example code a miunute ago, recreating the exact folder structure and loaded the c:\MUSIC\FLAC folder with a number of subdirectories and created dummy files with and without .ape extensions but didnt get anything echo'd info back from it... Any ideas?
to define further and it may already be doing this? If we have an example folder that has a .ape file in it and some other sub-directories that don't have .ape files in them then it must IGNORE this folder and move onto the next folder:
C:\MUSIC\FLAC\some_random_album\album.ape
C:\MUSIC\FLAC\some_random_album\CUE
C:\MUSIC\FLAC\some_random_album\MISC
C:\MUSIC\FLAC\some_random_album\IMAGES
If it then moves onto our 2nd example folder and the folder DOSN'T have ANY .ape files in ANY of the folders:
C:\MUSIC\FLAC\some_random_album2\album.wv
C:\MUSIC\FLAC\some_random_album2\CUE
C:\MUSIC\FLAC\some_random_album2\MISC
C:\MUSIC\FLAC\some_random_album2\IMAGES
then it is to delete: (C:\MUSIC\FLAC\some_random_album2) and ALL of its sub-directories.
The folder structure will ALWAYS be tiered in the same way so it is a given that the folders wil ALWAYS be laid out in this way.
-
Fawers
- Posts: 187
- Joined: 08 Apr 2012 17:11
-
Contact:
#12
Post
by Fawers » 09 Apr 2012 19:56
It's kind of difficult to think of a batch code that can do such specific things.
I mean, in the start you told us you wanted the parent folder deleted if the current folder hasn't got any .ape's in it.
Let's say we've got 3 folders: A, B and C.
Both B and C are sub-folders of A.
So the paths we would have would be:
C:\A
C:\A\B
and
C:\A\C
And say we've got a .ape file in C and another in A.
Most "for /f" commands we've seen here used the "dir /b /s" command in order to look for .ape files.
Now say we run one these scripts: we look for .ape files in the 3 folders. This is what we get:
.ape in A = TRUE
.ape in B = FALSE
.ape in C = TRUE
Because DIR command standard is to search alphabetically, it would run the script in A, then in A\B and then in A\C.
The whole A folder would be removed when the program would find out that there's no .ape in A\B.
Sorry for the long post, but I just want to know if I got your request right.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#13
Post
by foxidrive » 10 Apr 2012 00:02
phoenix_Rising wrote:@Foxidrive - I tried your example code a miunute ago, recreating the exact folder structure and loaded the c:\MUSIC\FLAC folder with a number of subdirectories and created dummy files with and without .ape extensions but didnt get anything echo'd info back from it... Any ideas?
Try it again - I had left the closing bracket off.
-
phoenix_Rising
- Posts: 38
- Joined: 04 Apr 2012 03:11
#14
Post
by phoenix_Rising » 10 Apr 2012 02:00
@Fawers - I know what your saying... I do like a challenge though! In the example that you gave i would say that because an .ape file was found the script should move on the to the next logical folder after c:\A and to ignore C:\A and all of its sib-directories.
Given the same folder examples If a .ape file was found in C:\A\B folder only. The script should ignore the folder C:\A and move onto the next logical folder (For example C:\AA)
If NO .ape files were in:
C:\A
C:\A\B
C:\A\C
then C:\A and all of its files, and sub-directories should be deleted.
@Foxidrive - Thanks for the ammendment, i tried running it again and i got the following output:
The system cannot find the file specified.
" Volume in drive C has no label." has no .ape files in it or the subdirectories
rd /s /q " Volume in drive C has no label."
Press any key to continue . . .
The system cannot find the file specified.
" Volume Serial Number is 74A1-EFF3" has no .ape files in it or the subdirectories
rd /s /q " Volume Serial Number is 74A1-EFF3"
Press any key to continue . . .
The filename, directory name, or volume label syntax is incorrect.
" Directory of C:\MUSIC\FLAC" has no .ape files in it or the subdirectories
rd /s /q " Directory of C:\MUSIC\FLAC"
Press any key to continue . . .
C:\MUSIC\FLAC
There are no .ape files in any of the 3 test folders i have created within C:\MUSIC\FLAC
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#15
Post
by foxidrive » 10 Apr 2012 03:12
phoenix_Rising wrote:@Foxidrive - Thanks for the ammendment, i tried running it again and i got the following output:
The system cannot find the file specified.
" Volume in drive C has no label." has no .ape files in it or the subdirectories
Try copy and pasting it again - somehow i wrote b instead of /b