0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

BoxDrive等のユーザーによってファイルパスが異なる場合のショートカット

Last updated at Posted at 2025-02-20

ユーザーごとにファイルパスが異なる場合には、フォルダやファイルのショートカットを作成しても作成者本人にしか利用できません。
その場合のフォルダのショートカットの共有方法を考えてみました。

Windowsバッチファイル
@echo off
rem 目的:『C:\Users\12345\Desktop』を表示する。
rem ※『C:\Users\12345』部分は環境変数のため、それ以降を指定
rem echo %USERPROFILE%
set "targetPath=%USERPROFILE%\Desktop"

if not exist "%targetPath%" (
    echo フォルダが見つかりません。
    pause
    exit /b
)
start "" "%targetPath%"

実行前に実行してよいか確認したいなら、下記のコードです。

Windowsバッチファイル
@echo off
setlocal
rem 目的:『C:\Users\12345\Desktop』を表示する。
rem ※『C:\Users\12345』部分は環境変数のため、それ以降を指定

set "targetPath=%USERPROFILE%\【外】OneDrive"

:: フォルダのパスを表示
:RESTART
echo 指定されたフォルダ: "%targetPath%"

:: ユーザー入力を取得
set "choice="
set /p "choice=このフォルダを開きますか? (Y/N): " 
set "choice=%choice:~0,1%"  & rem 先頭1文字のみ取得
set /a "choice=0x%choice%" >nul 2>&1 & if errorlevel 1 set choice=%choice:~0,1%

:: 大文字・小文字の区別なくYなら実行、それ以外は中止
if /i "%choice%"=="Y" (
    if exist "%targetPath%" (
        start "" "%targetPath%"
    ) else (
        echo フォルダが見つかりません。
        pause
    )
) else if /i "%choice%"=="N" (
    echo 処理を中止しました。
    pause
) else (
    echo もう一度入力してください。
    goto RESTART
)

また、上記のようにフォルダパスではなく、Excel等のファイルの場合も基本は同じです。

Windowsバッチファイル
@echo off
rem 目的:『C:\Users\81805\Downloads\テスト.xlsx』を表示する。
rem ※『C:\Users\12345』部分は環境変数のため、それ以降を指定
rem echo %USERPROFILE%
set "targetPath=%USERPROFILE%\Downloads\テスト.xlsx"

rem echo 指定されたフォルダ: %targetPath%

if not exist "%targetPath%" (
    echo フォルダが見つかりません。
    pause
    exit /b
)

start "" "%targetPath%"

Windowsでショートカットを相対パスで記載する方法もあるようですが、結構クセがあるため、より簡単なバッチファイルでの解決を考えてみました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?