0

I am just trying to create many folders and sub folders from a batch file. After I have done that, I need to delete the .bat file.  How can I do this?

This fails:

@echo off
md Folder_01
Folder_02
02_SubFolder_01
Folder_03
03_SubFolder_01

del "C:\Users\username\Desktop\Test for Bat.bat"
4
  • 3
    stackoverflow.com/a/20333575/3536342
    – DavidPostill
    Jun 23, 2018 at 15:43
  • @AFH: I cannot confirm/reproduce your claim.  I find that %~f0 is still the correct full path to the batch file even after it has done a cd. (Windows 7 SP 1, version 6.1.7601.17504.) Jun 25, 2018 at 18:32
  • @Scott - I have only Win10 these days, and it is certainly true when I tested there. Certainly, my suggested work-round is safe, however the expression is handled in different releases.
    – AFH
    Jun 25, 2018 at 22:32
  • 1
    @Scott - The system I tested on has just gone phut after a power cut, and I have just tested again on another W10 system, only to confirm your findings. I shall need to come back to this after I have got the failed machine running again. I have removed my comments until I can investigate further.
    – AFH
    Jun 25, 2018 at 22:50

2 Answers 2

1

The OP found a solution based upon the answers to the SO question How to make a batch file delete itself?, which he originally edited into the question:

I have found this solution:

start /b "" cmd /c del "%~f0"&exit /b
0
0

Why would you do something like that? If you only use the command for creating the folders once, you could just do it via cmd.

you could determine bat file location with %0 and delete it- but it's not wise to delete a script were the reason to write it is to use it multiple times…

9
  • Because when I start my new project, I create around 20 folders and sub folders. That folders names exactly same for each new projects. I really spend a lot of times to create many folder for each new project. That is why I need to do it via .bat file. And I have not any idea how to use CMD.
    – Max-Enrik
    Jun 23, 2018 at 15:34
  • bat files are collections of commands you'd otherwise type in cmd. So if you are able to write bat files - you are able to use cmd
    – kai-dj
    Jun 23, 2018 at 15:38
  • I just tried to write something in .bat file for creates many folders.
    – Max-Enrik
    Jun 23, 2018 at 15:39
  • but it seems you aren't looking for how to delete a bat file after execution - you are looking for a way to create the project folders automaticly - if so you should modify your question (and google for "batch create folders" it will very likely be already answered).
    – kai-dj
    Jun 23, 2018 at 15:42
  • I just did it, but can't create many folders like sub folders. And after created folders, then delete .bat file.
    – Max-Enrik
    Jun 23, 2018 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.