0

I am working with a batch script in which the user can automate the creation of folders and subfolders on windows.

Here's the actual code:

@echo off

:: MAKES A TEMPORARY FOLDER FIRST 
mkdir "New Folder 123"

:: LET THE USER INPUT PREFERED FOLDER NAME AND THIS WILL CHANGE THE TEMPORARY INTO THE NEW ONE
:rename
cls
SET /p comm=Please enter your prefered folder name:  

IF /i "%comm%" == "%comm%" ren "New Folder 123" "%comm%"
IF /i "" == "%comm%" goto :rename
IF /i " " == "%comm%" goto :rename

:: COMMAND WILL OPEN THE NEWLY RENAMED FOLDER AND CREATES SUBFOLDER(S) 
cd "%comm%"
md "Folder 1" "Folder 2" 

exit

Now, the problem is... If the user accidentally exits the command prompt window, the temporary folder which I initially wrote from the beginning of the code remains.

:: MAKES A TEMPORARY FOLDER FIRST 
mkdir "New Folder 123"

This will create a conflict if the user runs the batch script the second time since that temporary folder already exists and I don't want the user to manually right click and delete it cause that would be a hassle.

So I'm hoping if anyone could provide a code that will automatically delete the initial folder as the user (accidentally) exits the console? I've been looking for similar solutions here that are closely related to mine, but they all seems to delete the batch file itself instead of a folder, I'm also really new into making programs and I had a hard time understanding some batch codes, so please be easy with me, Any help would be greatly appreciated, Thank you so much everyone.

  • 2
    The only apparent reason for making the temporary directory appears to be to rename it. Why not just ask for the new name and then create that new name -and the subdirectories - directly? Oh, btw - `rename` is a poor name for a label. `ren` is actually an alias for the `rename` command, and it's not a good idea to use a commandname as a label. – Magoo Jan 05 '21 at 12:40
  • 1
    The solution to your problem is quite simple: don't create first a folder and next prompt the user for the folder name which is used to rename the already created folder. It would be better to first prompt the user for the folder name, then check if a folder or file does not already exists with that name and if this condition is true, create the folder with user input name and verify if that was successful at all before doing the next steps. [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) – Mofi Jan 05 '21 at 14:04
  • Note that the command line `mkdir "New Folder 123"` fails if the current directory on execution of `cmd.exe` interpreting the batch file is not a directory on which the user has the permission to create a directory. You might expect that the current directory is the directory containing the batch file and the user has always the necessary permission to create a subdirectory in that directory. This is very often a completely wrong expectation. In fact the current directory on execution of a batch file can be any directory. There are usually more than 25.000 directories on a Windows PC. – Mofi Jan 05 '21 at 14:08
  • The command line `mkdir "New Folder 123"` also fails if the current directory contains already a __file__ with name `New Folder 123`. So I strongly recommend not first creating blind without verification a directory in current directory with a fixed name defined by you. Prompt first the user for the folder name, verify that the input string is a valid folder name, verify if there is no file/folder with such a name in current directory, create the directory and verify if directory creation was successful at all. – Mofi Jan 05 '21 at 14:12
  • Microsoft documentation about [Naming Files, Paths, and Namespaces](https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file) could be helpful. It explains which characters are valid for a file or folder name. You should check if the user input string is not an empty string and does not contain one of the disallowed ASCII characters before checking if a file/folder exists already with that name and before creation of the directory with that name. – Mofi Jan 05 '21 at 14:14
  • 1
    The complexity grows if the directory is on a shared network location. Two or more users could be running the .bat script at the same time. What if they both create the same directory? Is that valid? – lit Jan 05 '21 at 14:22
  • Sorry eveyone for the late rerponse and thank you for your answers, as for @Magoo and Mofi Yes I did what you've mentioned and I didn't realize all this time I was just doing it the hard way, **not** creating a folder first was indeed a better structure than I previously made. really appreciate your help on this. Also, thank you for dropping some additional useful notes for proper coding, it makes my work a lot more easier as newbie. – White Hat 101 Nov 08 '21 at 18:09

1 Answers1

0

Just add this at the beggining of the file after @echo off

rmdir "New folder 123" /s /q >NUL 2>&1
Dharman
  • 30,962
  • 25
  • 85
  • 135
vlOd
  • 29
  • 7