View unanswered posts | View active topics It is currently 20 Oct 2014 11:55



Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next
Batch question on deleting! 
Author Message

Joined: 04 Apr 2012 03:11
Posts: 38
Post Batch question on deleting!
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 :(


04 Apr 2012 03:13
Profile

Joined: 10 Feb 2012 02:20
Posts: 4302
Post Re: Batch question on deleting!
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.


04 Apr 2012 05:01
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Batch question on deleting!
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:
@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:
@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:
@echo off
cls
set path="C:\MUSIC"
for %%a in (ape) do (
  del /f /s /q "%path%\*.%%a
)
pause


hope it will help :)
just remember to adjust the directory in the path variable and be carfull be cuse it delete files


04 Apr 2012 16:00
Profile

Joined: 01 Jul 2011 08:47
Posts: 114
Post Re: Batch question on deleting!
If you want to check if a folder and subfolder is empty, use this batch file.
Code:
@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.


04 Apr 2012 19:02
Profile

Joined: 04 Apr 2012 03:11
Posts: 38
Post Re: Batch question on deleting!
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!


05 Apr 2012 03:47
Profile

Joined: 01 Jul 2011 08:47
Posts: 114
Post Re: Batch question on deleting!
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:
@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:
  if !hlp!==0 echo Empty folder: "!hlp2:%here%\=!"


05 Apr 2012 04:13
Profile

Joined: 10 Feb 2012 02:20
Posts: 4302
Post Re: Batch question on deleting!
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.



05 Apr 2012 05:33
Profile

Joined: 08 Apr 2012 17:11
Posts: 187
Post Re: Batch question on deleting!
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:
@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.


08 Apr 2012 21:57
Profile WWW

Joined: 10 Feb 2012 02:20
Posts: 4302
Post Re: Batch question on deleting!
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:
@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.



09 Apr 2012 02:32
Profile

Joined: 10 Feb 2012 02:20
Posts: 4302
Post Re: Batch question on deleting!
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.


09 Apr 2012 02:33
Profile

Joined: 04 Apr 2012 03:11
Posts: 38
Post Re: Batch question on deleting!
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.


09 Apr 2012 12:40
Profile

Joined: 08 Apr 2012 17:11
Posts: 187
Post Re: Batch question on deleting!
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.


09 Apr 2012 19:56
Profile WWW

Joined: 10 Feb 2012 02:20
Posts: 4302
Post Re: Batch question on deleting!
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.


10 Apr 2012 00:02
Profile

Joined: 04 Apr 2012 03:11
Posts: 38
Post Re: Batch question on deleting!
@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


10 Apr 2012 02:00
Profile

Joined: 10 Feb 2012 02:20
Posts: 4302
Post Re: Batch question on deleting!
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


10 Apr 2012 03:12
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 29 posts ]  Go to page 1, 2  Next


Who is online

Users browsing this forum: Yahoo [Bot] and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.