iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪟

Creating a Temporary Virtual Drive for Work on Windows

に公開

Introduction

In this article, I will explain how to create a virtual temporary drive using the Windows subst command.
I will also explain how to automatically create a temporary drive using Windows Task Scheduler.
With a temporary drive, you can safely perform tests or save temporary files at any time.
This will be a step towards improving your productivity.

1. What is subst?

subst[1] is a Windows command that treats a specified directory as a virtual drive.

1.2 Usage of subst

subst is used with the following syntax:

subst [virtual drive]: [full path]

For example, entering the following maps the c:\tmp directory as the Z drive.

subst z: c:\tmp

This allows you to access the contents of c:\tmp through the Z drive.
For detailed options and use cases, please refer to the official subst documentation.

2. Temporary Drive Creation Script

This section explains the script used to create a temporary drive.

2.1 Script Description

This script creates a temporary drive every time Windows starts.
Therefore, save it with the specific name "c:\bin\init\zdrive.cmd".

zdrive.cmd
@echo off
rem
rem create Z drive for temporary
rem
rem @Author atsushifx
rem

path c:\windows\system32;%path%

setlocal
set _tmpdrv=C:
set _tmpdir=%tmpdrv%\tmp

rem Create working directory
if not exist %_tmpdir%\NUL goto endtmpdir
  rmdir /s /q %_tmpdir%
:endtmpdir
mkdir %_tmpdir%

rem Create temporary drive
if exist Z:\NUL goto endtmpdrive
  C:\WINDOWS\system32\subst.exe z: %_tmpdir%
:endtmpdrive

rem Create subdirectories for work
mkdir z:\temp
mkdir z:\works

3. Automatically Creating a Temporary Drive Using Task Scheduler

By adding a task to Task Scheduler[2], you can have Windows automatically call the script created in section 2.
This ensures that the temporary "Z drive" is available automatically every time you use Windows.

3.1. Creating a Task to Create a Temporary Drive

To automatically create a temporary drive, create a task using the following steps.
This task is called during Windows startup and logon to automatically create the temporary drive.

  1. Launch Task Scheduler:
    Launch [Task Scheduler] from the Start menu to open [Task Scheduler].
    Task Scheduler

  2. Create a New Task:
    Select [Action] - [Create Task] to open the [Create Task] dialog.
    Create Task

  3. Set the Task Name:
    Select the [General] tab and enter zdrive in [Name]. Also, check "Run whether user is logged on or not".
    Task Name Settings

  4. Set the Triggers:
    Select the [Triggers] tab and click the [New] button to create a trigger. Select [At startup] and [At log on] for the triggers.
    Trigger Settings

  5. Set the Actions:
    Move to the [Actions] tab and click the [New] button. Register "c:\bin\init\zdrive.cmd" as the script.
    Action Settings

  6. Exit Task Scheduler:
    Click [OK] to close each dialog. Then, close Task Scheduler by selecting [File] - [Exit].

This completes the task setup.
A temporary drive will now be created automatically every time Windows starts.

3.2. Exporting the Task for Creating a Temporary Drive

Export the created task. By doing this, you will be able to use the temporary drive immediately when rebuilding your Windows environment.
Export the task using the following steps.

  1. Select the Task:
    In the [Task Scheduler Library] screen of Task Scheduler, select the created "zdrive".
    Task Scheduler Library

  2. Export the Task:
    Select [Action] - [Export] to export the task.

  3. Save the Export File:
    Save the exported task to a file.

This completes the task export.
With the exported file "zdrive.xml", the temporary drive will be available when rebuilding your Windows environment.

3.3. Importing the Task for Creating a Temporary Drive

You can import the task using the "zdrive.xml" file exported in section 3.2.
This allows you to use the temporary drive even immediately after a fresh Windows installation.
Import the task using the following steps.

  1. Task List:
    Open the [Task Scheduler Library] screen of Task Scheduler.
    Task Scheduler Library

  2. Import the Task:
    Select [Action] - [Import Task] and select the created "zdrive.xml" file.

  3. Task Settings:
    The [Create Task] dialog will be displayed, so click [OK].
    Task Name Settings

This completes the task import.

Conclusion

In this article, I introduced how to create a temporary drive using the Windows subst command. I also introduced how to automatically create a temporary drive using Task Scheduler.

Now you can try out small scripts and programs on a temporary drive.
This should make your programming and development work more efficient.

Happy Hacking!

References

Websites

脚注
  1. subst: A Windows command that allows you to treat a specified directory as a virtual drive. ↩︎

  2. Task Scheduler: A Windows feature that allows you to execute pre-configured tasks based on specific conditions or times. ↩︎

GitHubで編集を提案

Discussion