\\ Amazon パソコン・周辺機器 クーポンをチェックしよう!! //

How to Create Folders Using Batch Files|Utilize the mkdir Command!

To streamline your tasks on a Windows environment, you can use batch files to automate folder creation. In this article, we will explore the most effective command for folder creation—mkdir (or md), and provide a step-by-step guide to creating batch files, complete with illustrations. Whether you’re a programming beginner or an experienced user, this guide will help you easily automate folder creation.

目次

What is the mkdir Command?

The mkdir command (short for make directory) and its shorthand version md are used to create folders in the Windows Command Prompt. Both commands perform the same function, allowing users to create directories either manually or through automation with batch files.

Key Features:

  • Create single or multiple folders at once
  • Specify paths to create folders in any location
  • Automate folder creation using batch files

How to Create a Folder Using the Command Prompt

Instead of manually creating folders, you can use the Command Prompt for quick and efficient folder creation. Follow these steps:

Step 1: Open the Command Prompt

  1. Open the Start Menu.
  2. Type cmd and select Command Prompt to launch it.

Step 2: Execute the mkdir Command

To create a folder, type the following command:

mkdir FolderName

Example:

mkdir NewFolder

This command will create a folder named “NewFolder” in the current directory.


Steps to Create Folders with a Batch File

A batch file allows you to execute multiple commands automatically. Here’s how you can create a batch file to automate folder creation:

Step 1: Create a Text File

  1. Right-click on your Desktop and select NewText Document.

Step 2: Save as a Batch File

  1. Right-click on the created text file and select Rename.
  2. Change the file name to create_folders.bat and press Enter.
  • By changing the extension to .bat, the file becomes a batch file.

Step 3: Edit the Batch File

  1. Right-click on the create_folders.bat file and select Edit.
  2. Enter the following content:
@echo off
mkdir Folder1
mkdir Folder2
mkdir Folder3
echo Folder creation is complete.
pause
  1. Save and close the file.

Step 4: Run the Batch File

  1. Double-click the create_folders.bat file.
  2. The specified folders will be created, and a confirmation message will appear.

Creating Multiple Folders at Once

Using the mkdir command, you can create multiple folders in a single line by separating folder names with spaces:

mkdir FolderA FolderB FolderC

You can add this to your batch file to create multiple folders simultaneously.


Creating Folders in Parent or Subdirectories

The mkdir command also allows you to create folders in higher directories or subdirectories.

Creating a Folder in the Parent Directory

mkdir ..\NewFolder

The .. indicates the parent directory. This command creates the folder “NewFolder” one level up from the current directory.

Creating Subfolders

mkdir MainFolder\SubFolder

This command creates “SubFolder” inside “MainFolder” in the current directory.


Handling Encoding Issues with Folder Names

When creating folders with non-ASCII characters (like Japanese), you may encounter encoding issues. To prevent this, save your batch file with ANSI encoding.

Steps:

  1. Right-click the batch file and select Edit.
  2. In Notepad, go to FileSave As.
  3. Under Encoding, select ANSI and save the file.

This ensures that folder names in non-ASCII characters won’t appear garbled.


Conclusion

In this article, we have explained how to create folders using the mkdir command and automate the process with batch files. From basic folder creation in the Command Prompt to automating tasks with batch files, we covered everything you need to know, including handling encoding issues. Apply these techniques to streamline your daily tasks and improve efficiency.


Frequently Asked Questions (FAQ)

Q1: What’s the difference between mkdir and md?

A1: There’s no functional difference between mkdir and md; both commands are used to create directories. md is simply a shorter version of mkdir and is often more convenient to use.


Q2: Do I need admin privileges to run batch files for folder creation?

A2: You typically do not need admin privileges to create folders with a batch file. However, if you’re trying to create folders in system-protected directories, you may need administrator permissions.


Q3: How do I create folders with spaces in the name?

A3: If your folder name contains spaces, enclose the name in quotation marks:

mkdir "New Folder"

Q4: How can I conditionally create folders in a batch file?

A4: You can use an if statement in your batch file to conditionally create folders. For example, if a folder doesn’t exist, you can create it as follows:

if not exist "FolderName" (
    mkdir "FolderName"
)

Q5: How do I handle errors in a batch file?

A5: You can use the ERRORLEVEL variable to check for errors and handle them accordingly. For instance:

mkdir "FolderName"
if %ERRORLEVEL% neq 0 (
    echo Failed to create the folder.
    exit /b %ERRORLEVEL%
)

Batch files provide a convenient way to automate folder creation, helping you save time and effort. By following this guide, you can easily set up your own batch file to create folders efficiently!


Tamaglo

Thank you for reading!

Comments

To comment

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here

目次