Batch programming help

Associate
Joined
22 Sep 2009
Posts
2,085
Location
Leicester
I'm using Windows and trying to recursively go through folders and delete all images and various other files in a directory (I.E. not leak up and remove all my photos :D)

Not done much batch on Windows so I'm having issues (took me a few minutes to figure out /?)

Code:
DEL /S /ARH *.jpg
I would expect this to delete all jpg images in all subdirectories, whether hidden or not, however it just gives me
Could Not Find [DIRECTORY]*.jpg
Is my wildcard wrong or something?
 
it's looking for a directory.

Try ./*.jpg

Also, generally it's better to supply that first i.e. DEL ./*.jpg /S /ARH


BUT I wouldn't do it like that .

I'd use a for loop and then parse the out put similar to this. Much safer and allow more control.

Code:
@echo off
for /f %%A IN ('dir %1 /B /S /A-D /ON') DO (
    CALL :del_files %%A
)

goto :eof

:del_files
ECHO %1
CD /D  "%~dp1"
ECHO DEL "%~nx1"
goto :eof


Something like that.. remove the second echo to actually delete the files.
Put all that in a batch file e.g. mydelete.bat and run it supplying the 'root path' to delete from e.g.
Code:
mydelete.bat "c:\docuemnts\my pictures"
 
Last edited:
./*.jpg gives an error (invalid switch).

The rest of the stuff you posted confused me... off to read on batch coding to stop being such a noob, I find a lot of times batch would be easier and quicker than manual/compiled code. Hopefully I'll make sense of what you posted by the time I've finished :)
 
Code:
@echo off
don't print anything unless explicitly echoed to the screen
Code:
for /f %%A IN ('dir %1 /B /S /A-D /ON') DO (
for loop which takes the output of the dir command as the 'list of things to loop through'

The dir command takes the first parameter supplied you can hard code that to the root of your file tree if you like e.g. dir "c:\docs\pictures" /B /S /A-D /ON
The switches do BASIC, SUBDIRECTORIES, Don't print DIRECTORIES, and ORDER by NAME (Dir /? for more info) try just running the dir command on it's own to see what it does.
Code:
    CALL :del_files %%A
calls, (like a subroutine), it jumps to the label :del_files in the script, and passes the current files to it as the first parameter. I.e the first file listed, then the next etc etc
Code:
)
end of for loop
Code:
goto :eof
program ends here otherwise it will run the next part of the batch - you don't want that.

Code:
:del_files
ECHO %1
batch label, (prefixed with : )
and echos (prints) the supplied parameter, i.e. the current filename from the dir command
Code:
CD /D  "%~dp1"
changes directory to the drive letter and path of the current file
Code:
ECHO DEL "%~nx1"
deletes the fileName.Xtension of the current file (already in the dir, so no need to supply the whole thing) At this point here you could perform ANY operation on each file e.g. check filesize, or date, or open it, etc etc
Code:
goto :eof
end of routine, at the point it jumps back to the for loop and processes the next filename.
 
Thanks for the reply, I read through the documentation and figured out a few other things, however I'm having an issue getting the script working in folders where there is a space in the name, and CMD /X doesn't appear to work when calling a script, other than that I'm not sure what I can do.

Also, is the correct way to run this
delete_images *.jpg
Just want to make sure that I don't end up killing everything.

Cheers again.
 
Thanks for the reply, I read through the documentation and figured out a few other things, however I'm having an issue getting the script working in folders where there is a space in the name,

That is always a major PITA with batch scripting. You have to make sure your paths are encased in quotes e.g. "c:\docs and settings" and not c:\docs and settings If it's not possible ans some programs even don't like that, you have to do workarounds like using temp dirs and other things. One way is to CD /D into the directory with spaces and work there, rather than supplying the whole path.

and CMD /X doesn't appear to work when calling a script, other than that I'm not sure what I can do.
what is cmd /x and what do you expect it to do?

Also, is the correct way to run this
delete_images *.jpg
Just want to make sure that I don't end up killing everything.

Cheers again.

what is delete_images? If you are meaning my script then no, it expects a path, as this is the path that is supplied to DIR in the for loop.

If you want to only list jpg files you could modify the dir command to read dir %1\*.jpg /b /s /a-d /on

or you could supply two params, e.g. mydelete.bat "C:\docs and settings" jpg and have the dir command read dir %1\*.%2 /b /s /a-d /on and it'll search for *.jpg or whatever you supply as param 2.




You could also go about it on a directory level rather than a file level. So it find all directories below the supplied root, and change into them allowing you to perform operations on each directory like this:

Code:
@echo off
for /f %%A IN ('dir %1 /B /S /AD /O-N') DO (
    CALL :proc_dir %%A
)

goto :eof

:proc_dir
ECHO Working in this directory: %1
CD /D  "%1"
ECHO DEL *.jpg
goto :eof

Subtly different but may be more what you want?
 
Last edited:
You want to delete all files that end in .JPG from all subfolders of a folder right?

Well then try this VBScript:

Create a text file, copy and paste the code in below, rename the file to something.vbs where "something" is whatever you want. Make sure the file is copied over to the root directory of wherever you want to delete the files from. Then you can simply double click to run it.

Code:
'use FileSystemObject
Set objFSO = createobject("Scripting.FileSystemObject")

'Used to get Current Directory path
CurrentDir = objFSO.GetAbsolutePathName("")

'Bind to the current folder
ShowSubfolders objFSO.GetFolder(CurrentDir)

'Code to look through folder structure
Sub ShowSubFolders(Folder)

    For Each Subfolder in Folder.SubFolders

       	Set Files = Subfolder.Files
	
		For each ObjFile in Files
			Extension = ObjFSO.GetExtensionName(ObjFile)

			If Extension = "jpg" then
				ObjFSO.DeleteFile ObjFile
			End If

			ObjFile = ""

		Next

        ShowSubFolders Subfolder

    Next

End Sub

msgbox "File Deletion Completed"

Note: I would suggest copying over the folder structure to test this first, I have tested it and it seems to work OK but I don't want to be responsible for you losing anything unexpectedly!
 
CMD /X is extensions, I'm pretty sure it used to be used to stop CMD treating spaces as a delimiter, however doesn't seem the case on Vista...

How do I supply the address of each subfolder enclosed with ""'s? This is being the main issue, folders without spaces work nicely but as the majority have spaces in it doesn't work.

To be fair I think I know too little about CMD to make much sense of a lot of those sites and I don't really have the patients to learn it for this. If I'm just getting this all back to front let me know and I'll switch to C, might be extra work but saves wasting your time :)
 
CMD /X is extensions, I'm pretty sure it used to be used to stop CMD treating spaces as a delimiter, however doesn't seem the case on Vista...

How do I supply the address of each subfolder enclosed with ""'s? This is being the main issue, folders without spaces work nicely but as the majority have spaces in it doesn't work.

To be fair I think I know too little about CMD to make much sense of a lot of those sites and I don't really have the patients to learn it for this. If I'm just getting this all back to front let me know and I'll switch to C, might be extra work but saves wasting your time :)

Try my VBScript mate :)
 
Cheers, you must have got that other post in just before me :p

It worked, maybe VB scripting is the way forward, shame there's no Cscript :rolleyes:
 
CMD /X is extensions, I'm pretty sure it used to be used to stop CMD treating spaces as a delimiter, however doesn't seem the case on Vista...

How do I supply the address of each subfolder enclosed with ""'s? This is being the main issue, folders without spaces work nicely but as the majority have spaces in it doesn't work.

To be fair I think I know too little about CMD to make much sense of a lot of those sites and I don't really have the patients to learn it for this. If I'm just getting this all back to front let me know and I'll switch to C, might be extra work but saves wasting your time :)


It seems you have a solution, but you don't supply each folder, you upply the top level folder i.e. the root and the script searches each sub folder ...
 
That's what I was referring to, the script is collecting the subfolders but supplying them without ""'s so it's not working. I was just wondering how I could get around that.
 
Back
Top Bottom