我需要一種方法來創建指向一個檔案夾中多個檔案的符號鏈接,所有檔案都列在 .txt 檔案中。串列中的檔案名缺少檔案擴展名。我曾經使用以下腳本進行復制,但未能將復制命令替換為符號鏈接創建。
@echo off
chcp 65001 > nul
for /f "usebackq delims=" %%i IN ("selection_list.txt") DO (
xcopy "..\%%i.zip" "..\selection\%%i.zip*"
)
pause
使用相對路徑,因為我希望能夠在多個檔案夾中使用它。.txt 檔案中的檔案名不包括檔案擴展名。例如,假設我想在檔案夾“F:\assets”中使用它,我會將腳本與名為 selection_list.txt 的 .txt 檔案一起放在檔案夾“F:\assets\selection_script”中。啟動腳本后,它將創建一個檔案夾“F:\assets\selection”,其中包含我想要的所有檔案。
我嘗試使用此語法示例將 xcopy 命令替換為 mklink /D
mklink /D "C:\Link To Folder" "C:\Users\Name\Original Folder"
新腳本如下所示
@echo off
chcp 65001 > nul
for /f "usebackq delims=" %%i IN ("selection_list.txt") DO (
mklink /D "..\selection_links\%%i.zip*" "..\%%i.zip"
)
pause
顯然這沒有奏效。說系統找不到檔案 selection_list.txt 我試圖手動運行具有相對路徑的單個命名檔案的命令并且它有效,所以我的問題是讓它在帶有串列的函式中作業。在我看來,從 .txt 串列中的檔案名頂部添加檔案擴展名可能是問題所在,但不知道如何解決它。我嘗試了一些我發現的語法變體,但沒有成功
我對此非常陌生,因此將不勝感激任何幫助!
uj5u.com熱心網友回復:
讓我先解釋一下要完成的任務。有以下檔案夾和檔案:
F:\資產
- 選擇
- 開發和測驗(!).zip
- ;示例壓縮檔案.zip
- selection_script
- create_selection.cmd
- selection_list.txt
文本檔案selection_list.txt包含以下行:
Development & Test(!)
;Example Zip File
Not existing file
的執行create_selection.cmd應導致以下檔案夾和檔案:
F:\資產
- 選擇
- 開發和測驗(!).zip
- ;示例壓縮檔案.zip
- 選擇鏈接
- 開發和測驗(!).zip
- ;示例壓縮檔案.zip
- selection_script
- create_selection.cmd
- selection_list.txt
目錄條目Development & Test(!).zip和;Example Zip File.zip創建的目錄selection_links是符號鏈接,而不是目錄中兩個檔案的副本selection。
可以使用F:\assets\selection_script\create_selection.cmd以下命令列來完成此符號鏈接創建任務:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=*" %%G in ('%SystemRoot%\System32\chcp.com') do for %%H in (%%G) do set /A "CodePage=%%H" 2>nul
%SystemRoot%\System32\chcp.com 65001 >nul 2>&1
for %%I in ("%~dp0..\selection_links") do set "LinksFolder=%%~fI"
if not exist "%LinksFolder%\" md "%LinksFolder%" 2>nul
if not exist "%LinksFolder%\" echo ERROR: Failed to create directory: "%LinksFolder%"& goto EndBatch
pushd "%LinksFolder%"
if exist "%~dp0selection_list.txt" for /F "usebackq eol=| delims=" %%I in ("%~dp0selection_list.txt") do if exist "..\selection\%%I.zip" if not exist "%%I.zip" mklink "%%I.zip" "..\selection\%%I.zip" >nul
popd
:EndBatch
%SystemRoot%\System32\chcp.com %CodePage% >nul
endlocal
首先完全定義了所需的執行環境,前兩個命令列設定了一個本地執行環境,關閉了命令回顯模式,啟用了命令擴展,并根據該任務的需要禁用了延遲變數擴展。
接下來使用Compo在 DosTips 論壇主題[Info] Saving current codepageCodePage上發布的命令列確定當前活動的代碼頁并存盤在環境變數中。然后將活動代碼頁更改為 UTF-8,盡管該示例并不真正需要。
There is next determined once the full path of the folder in which the symbolic links should be created which is the folder selection_links being a subfolder of the parent folder F:\assets of the folder selection_script containing the batch script. It does not matter if this folder already exists or not on determining the fully qualified folder name.
There is next verified if the target folder exists. The folder selection_links is created on not existing with checking once again if the folder really exists now. A useful error message is output on creation of folder failed and the batch file restores the initial code page and the initial execution environment.
The target folder is made the current directory by using the command PUSHD which should not fail anymore now after verification that the target folder exists.
There are next processed the lines in the text file selection_list.txt referenced with its fully qualified file name by using %~dp0 which expands to drive and path of argument 0 which is the full path of the batch file always ending with a backlash.
Each non-empty line not starting with the character | is assigned completely one after the other to the loop variable I. The character | is not valid for a file name as explained in the Microsoft documentation about Naming Files, Paths, and Namespaces. There is verified next if there is really a ZIP archive file with that name in the folder selection and if there is no directory entry with same name in current folder selection_links.
If these two conditions are both true, MKLINK is executed to create in current directory selection_links a file symbolic link to the ZIP file in the directory selection.
Please note that a ZIP archive file is not a directory and for that reason the usage of MKLINK option /D to create a directory symbolic link cannot work ever.
Finally the initial current directory is restored using POPD and the initial code page and the initial execution environment are also restored by the batch file before it ends.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?... explains%~dp0... drive and path of argument 0 – the batch file pathchcp /?echo /?endlocal /?for /?goto /?if /?md /?mklink /?popd /?pushd /?set /?setlocal /?
See also:
- Microsoft documentation about Using command redirection operators
- Single line with multiple commands using Windows batch file
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428991.html
