1

I need to call a batch file in another directory from a parent directory but the called batch file needs to run in the called directory not parent directory.

Example

C:\Batch\DeleteAll.bat

DeleteAll.bat calls another DeleteAll.bat in another directory C:\Batch\1\DeleteAll.bat

Then it should delete all files in C:\Batch\1\

How can I do this?

I have tried but it runs the C:\Batch\1\DeleteAll.bat in the parent directory, not the child directory

===

Okay I managed to solve this problem with the space in the folder name

the problem was I didn't add the quotes in the child Batch file

to solve the problem, I need to add quotes to the command

DEL "%~dp0*.txt"

If you are still reading this.. Thanks Tone, your solution works.

tyler.durden
  • 83
  • 1
  • 10
  • 1
    See [Get directory containing the currently executed batch script](http://serverfault.com/questions/255291/get-directory-containing-the-currently-executed-batch-script). The answer also links to more info at [Microsofts site](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true). – Tone Jun 29 '16 at 00:18
  • i don't think that links solves my problem.. I don't need to get the directory, I just need it to run the commands in the called directory. Can u provide some real example? – tyler.durden Jun 29 '16 at 00:25

1 Answers1

2

As a real world example.

If you have the following batch file in your parent directory

call "directory 1\DeleteAll.bat"

Then in subdirectory "directory 1" (the child directory) you have another batch file called DeleteAll.bat

del "%~dp0\*.txt"

The parent batch file calls the child batch file, which deletes all .txt the the child directory ("directory 1"). Bear in mind you don't want to try and delete the batch file itself (I assume) so you'll need to specify whatever masks you're looking to delete in the child batch files.

Information based on the serverfault answer Get directory containing the currently executed batch script

Note:

If no files match the specified mask (e.g. .txt) it is normal to get a Could Not Find message. The batch file will continue to run and you can ignore this message if these files may or may not exist. If you'd prefer, you can suppress the message as shown in How do I prevent the “Could Not Find” error message from del *.txt?


Alternative syntax for child batch files

If you have a lot of masks to delete and/or you wish to simplify the delete syntax, you could use the following syntax in your child batch files. Based on the information at How do I find the current directory of a batch file, and then use it for the path?

pushd "%cd%"
cd "%~dp0"

del 1.txt
del 2.mpg
del "name with spaces.mpg"
del *.tmp

popd

In that example pushd is used to save the current directory (initially where you ran the parent batch file from). cd is used to switch to directory containing the child batch file, then popd restores the saved directory

Community
  • 1
  • 1
Tone
  • 1,701
  • 1
  • 17
  • 18
  • i just test this and its works in your example but i can't get it to run in my own directory. I think cos my parent directory has a space in it. e.g. C:\Batch File\ Error The system cannot find the path specified. – tyler.durden Jun 29 '16 at 00:54
  • You just need to quote your paths. I updated my answer to reflect. The link posted by TessellatingHeckler has potential to be a cleaner way of doing it but I can't think how I'd apply it at the moment. – Tone Jun 29 '16 at 01:00
  • I already quoted my path (before u fix it) but it is giving same error. M:\Batch File>DEL M:\Batch File\1\\*.mpg The system cannot find the path specified... There is a double backslash before the *.mpg. But stackoverflow removed it. I am trying to delete videos files btw. – tyler.durden Jun 29 '16 at 01:21
  • Are there definitely .mpg files in that directory? (M:\Batch File\1) – Tone Jun 29 '16 at 01:29
  • 1
    Okay I managed to solve this problem with the space in the folder name.The problem was I didn't add the quotes in the child Batch file. To solve the problem, I need to add quotes to the command DEL "%~dp0\*.txt" If you are still reading this.. Thanks Tone, your solution works. – tyler.durden Jun 29 '16 at 12:54