簡體   English   中英

切換兩個特定文件(或文件夾)的名稱

[英]Toggle the names of two specific files (or folders)

我需要切換本地驅動器上特定路徑上存在的兩個特定文件的名稱,並想知道.bat或.vbs是否可以實現這一目標。

換句話說,執行腳本一次將 “文件A”與“文件B”交換(“ C:\\ Path A \\ File A.txt”與“ C:\\ Path A \\ File B.txt”),然后再次運行它將再次交換它們。

我也很想知道是否可以這樣做:

1)在這種情況下->>“ C:\\ Some Path \\ File A.txt”和“ D:\\ Some Other Path \\ File B.txt”

2)如果我想切換兩個文件夾,而不是兩個文件。

創建以下批處理文件,然后根據需要命名。 我使用的名稱是“ myRename.bat”。

:: myRename.bat
@echo off
SETLOCAL

:: verify the first file exists
if not exist "%~1" ( echo ERROR: File not found "%~1" & goto endofscript )

:: verify the second file exists
if not exist "%~2" ( echo ERROR: File not found "%~2" & goto endofscript )

:: Create a guaranteed unique string for temporarily naming one file
set instance=%date:~-4,4%%date:~-10,2%%date:~-7,2%
set instance=%instance%-%time:~0,2%%time:~3,2%%time:~6,2%%time:~9,2%
set instance=%instance%-%RANDOM%

:: rename the first file to a temporary name
ren "%~1" "%~nx1.%instance%"
:: rename the second file to the first file name
ren "%~2" "%~nx1"
:: rename teh first file to the second file name
ren "%~1.%instance%" "%~nx2"

:endofscript

假設這兩個文件存在於此路徑中:

  • c:\\ temp \\重命名test \\ File A.txt
  • c:\\ temp \\重命名test \\ File B.txt

然后,您可以運行以下命令,它們將交換名稱:

myRename "c:\temp\Rename test\File A.txt" "c:\temp\Rename test\File B.txt"

如果找不到文件A或文件B,則會在屏幕上報告該錯誤,並且過程將停止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM