0

First time asking here so apologies if the title is a bit confusing.

I am trying to make a batch file to delete all files within a folder and within that folder's subfolders.

So far, this is my batch code:

@echo off
:home
cls
color 02
title STUDENT FILE CLEAN SLATE PROTOCOL
echo Where are the files to be deleted?
call selectFolder.vbs
set %1=objFolder
cd %1
cls
color 02
echo Deleting...
del *.* /q /s
goto done

:done
echo.
echo Deleting process is now complete
echo Press Enter to close this program
pause > nul
exit

:empty
cls
echo You did not specify a directory
echo Please try again
pause > nul
goto home

And this is my VBScript code:

Dim objShell
Public objFolder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Please select the folder.", 1, "")
shell.run "delete.bat objFolder"

The VBScript code isn't fully done. I am having an error at line 5, char 1 so that's the shell.run part. I'm getting an Object required error. May I know how can I fix this and still how to push the value of objFolder to my batch file?

UPDATE: I've decided to focus this batch code to just delete files. Gonna make a separate batch file to deal with the remnant folders after file deletion inside.

  • Neither the Windows Command Processor nor any [Windows command](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) supports a graphical user interface like a graphical dialog window for selection of a directory by the user. So why do you want to do the task with a batch file if the script interpreter `cmd.exe` does not support what your task requires? Why do you not do the task with a VBScript script file interpreted by GUI version of Windows Script Host `wscript.exe` supporting all you need for the task? – Mofi Jun 02 '22 at 05:38
  • I recommend to study [An A-Z Index of Windows VBScript commands](https://ss64.com/vb/) and code a `.vbs` script file which is executed with a shortcut file (`.lnk` file) having the shortcut property __Target__ configured with `%SystemRoot%\System32\wscript.exe "C:\Full\Path\Script File.vbs" //B //Nologo`. By the way: If a user browses in a Windows File Explorer window to a folder containing files and subfolders to delete, the user has just to press __Ctrl+A__ and __Shift+Del__ and the task is done. I don't know what should be the benefit of using the script you try to code. – Mofi Jun 02 '22 at 05:42
  • PS: I recommend to read [How to delete files/subfolders in a specific directory at the command prompt in Windows?](https://stackoverflow.com/a/50656521/3074564) Then you know that your batch file does not really delete all files and all subfolders in the specified directory. Or do you really want to delete just the files and not the folder tree in specified folder? I don't know any practical use case where it makes sense to delete all files in a folder tree, but not the folders itself leaving behind lots of empty folders. – Mofi Jun 02 '22 at 05:49
  • @Mofi Actually, I initially thought all I needed to clear out are the files. I was informed I also needed to clear the folders at a certain sublevel of the directory. – Ranier Albert Lizada Jun 02 '22 at 06:03
  • @Mofi Something like this: Parent folder A has 2 subfolders, A1 and A2. Each subfolder has their own subfolders, in this case, A1.1 and A1.2. Those subfolders also have a subfolder of their own, A1.1.1 and A1.1.2. I need to clear files of the subfolder A1.1.1 and any stray folders inside it. – Ranier Albert Lizada Jun 02 '22 at 06:12
  • I suggest to look on the folder `%APPDATA%\Microsoft\Windows\SendTo`. It contains usually several shortcut files with file extension `.lnk`. Now right click on any file or folder with Windows File Explorer and open the context submenu __Send to__. What do you see? Yes, menu items with the names of the shortcut files. So if you want the file deletion job easy for the users and want to use a batch file, create a shortcut file to run the batch file with the arguments passed by `explorer.exe` to `cmd.exe` passing it further to the batch file for processing the arguments. – Mofi Jun 02 '22 at 19:12
  • The user has the freedom to right click on a single folder or select even multiple folders and `explorer.exe` runs on clicking on the menu item in __Send to__ submenu the __Target__ (batch file) with the fully qualified name of the right clicked directory or all selected directories. Well, the user can right click also on a file and then click on the menu item in context menu. So the batch file should better check every argument string on referencing a directory or a file. The arguments passed to the batch file are processed with a loop using `for %%I in (%*) do ( rem some commands )`. – Mofi Jun 02 '22 at 19:16
  • `set %1=objFolder`? Think about it... – Stephan Jun 03 '22 at 13:15
  • You stated that you had an error on line five of your vbscript. Clearly as you've defined and set `objShell`, then line five should use it instead of `shell`, i.e. `objShell.run`. – Compo Jun 04 '22 at 13:09

0 Answers0