編輯:更新得更清楚,并澄清我只想將檔案 001 改組到 049 并將檔案 050 保留為 050,并在本文末尾添加了“理想的最終目標概念”
我正在尋找一種方法來使用我的 Windows 批處理檔案對當前目錄中的 49 個檔案進行隨機播放。所有檔案都命名為 .001、.002 等,一直到 .049,使用 9 或 10 個數字作為變數輸入到批處理檔案中。我需要能夠使用相同的 9 或 10 個數字作為變數輸入,以便能夠使用單獨的批處理檔案將檔案“解散”回其原始狀態。任何想法和代碼示例將不勝感激。
這是我的批處理檔案命令列示例:
我在名為 testfile.exe.001 到 .050 的檔案夾中有 50 個檔案(我只想隨機播放 001 - 049)
shuffle.bat testfile.exe 8 5 2 9 4 1 3 7 6
確保我沒有兩次使用相同的數字并且不在同一個地方(即不要將“1”作為第一個變數,否則它只會將 .001 重命名為 .001),以便前 9 個檔案被打亂.
然后我做這樣的事情:
rem temporarily renaming .001 - .050 files to .001a - .050a to prevent collisions
ren %1.0?? %1.0??a
ren %1.001a %1.00%2
rem which renames testfile.exe.001a to testfile.exe.008
ren %1.002a %1.00%3
rem which renames testfile.exe.002a to testfile.exe.005
ren %1.003a %1.00%4
rem which renames testfile.exe.003a to testfile.exe.002
rem etc. all the way up to %1.009a
然后我想,我可以對檔案 .011 - .019 做同樣的事情,然后對檔案 .021 - 0.29 等做同樣的事情,這樣可以,但它不會在 49 個檔案的整個范圍內打亂檔案。例如 .005 不能變成 .045 等。
要取消隨機播放,我必須在 unshuffler 批處理檔案中輸入相同的 9 或 10 個數字變數,該批處理檔案只是反向執行所有操作。因此,如果沒有這 9 或 10 個數字(充當序列或“某種”密碼),檔案將無法恢復為其原始名稱。
有沒有辦法改進我的代碼或更好的方法來使用批處理檔案變數(數字)對這 50 個檔案進行混洗,這會導致整個 49 個檔案的混洗,并且使用相同的 9 或 10 個數字作為變數完全可逆?謝謝你的幫助!
理想的最終目標概念:
為了能夠在所有這些都為真的情況下隨機播放這 49 個檔案:
- 能夠在命令列(變數)上輸入代碼,該代碼唯一地影響并且是洗牌順序的關鍵,并允許解洗
- 每個檔案的原始順序已更改為另一個順序
- 原始順序中沒有兩個檔案在打亂的順序中一個接一個
- 所有檔案都可以在整個 001-049 集合中打亂(例如,005 可以變成 045,032 可以變成 016,等等)
uj5u.com熱心網友回復:
setlocal enabledelayedexpansion
set "sourcedir=c:\testso"
set "filename=%1"
:: clear any variables starting #
for /f "tokens=1delims==" %%e in ('set # 2^>nul') do set "%%e="
:: number of files
set /a #files=8
set /a #files1k=1000 #files
set "parm= %* "
set /a #count=0
set /a #randomadd=0
call :readparms %parm%
:: if 0 or 1 parameter, shuffle randomly
if %#count% lss 2 goto shuffle
:: unshuffling
echo unshuffling
call :shuffleren
goto :eof
:shuffle
set /a #remain=#files1k
for /L %%e in (1001,1,%#remain%) do set "#%%e="
:another
set /a #choice=((%random% #randomadd) %% #files) 1001
if defined #%#choice% goto another
if %#remain%%#choice%==10011001 goto shuffle
if %#choice% equ %#remain% goto another
set /a #%#choice%=%#remain%
if %#remain%==1001 goto doneshuffle
set /a #remain-=1
goto another
:doneshuffle
set "#report="
set #lastnum=0
for /L %%e in (1001,1,%#files1k%) do (
set /a #num=!#%%e!-1000
set /a #lastnum =1
if !#num!==!#lastnum! goto shuffle
set /a #lastnum=#num
set "#report=!#report! !#num!
)
call :shuffleren
echo sequence=%#report:~1%
goto :eof
:shuffleren
for /L %%e in (1001,1,%#files1k%) do (
set "#oldname=%%e"
set "#newname=!#%%e!"
ren "%sourcedir%\%filename%*.!#oldname:~-3!" "%filename%*.!#oldname:~-3!.!#newname:~-3!.t"
)
for %%b in ("%sourcedir%\%filename%*.t") do ren "%%b" "%%~nb"
goto :eof
:readparms
shift
set "parm=%1"
if not defined parm goto :eof
set "parm=9%1"
for /L %%e in (0,1,9) do set "parm=!parm:%%e=!"
if defined parm echo ignored "%1" &goto readparms
set /a #randomadd=%1
set /a parm=1000 %1
set /a #count =1
set /a #count1k=1000 #count
set /a #%parm%=#count1k
goto readparms
我們從設定目錄開始,從引數串列中檢查和讀取檔案名;然后清除所有開始的變數#。
確定要操作的檔案數量 - 我已經看到了修訂后的規范。這應該是 49,而不是 50。我用 8 進行測驗。
計算 1000 #files 以保存重復計算。初始化找到的數字引數的計數,并使用:readparms. 我有一些控制選項來方便測驗,所以在傳遞引數串列的兩端添加了一個空格%*。
讀取并存盤引數后,檢查#count- 0 或 1 表示洗牌,更多表示取消洗牌。
所以,洗牌:
首先,將所有變數設定#1001為#1050(它會自動調整到#files指定)為nothing。
接下來,生成一個亂數 1001..1050,如果尚未分配,則將剩余的數字從 1001..1050 分配到#the randomnumber并減少剩余的數字,直到達到 1001(最后一個分配)。如果兩者都是#count,那么我們正在嘗試重命名為,所以我們重新開始。如果和否則相同,請選擇另一個數字#remain1001.001.001#remain#choice
當我們完成 shuffle 后,我們需要構建 unshuffle 字串。我們依次從#1000to抓取每個數字#1050,將其轉換為自然數(無前導零)并將其與最后處理的數字進行比較 - 如果差為 1,那么我們正在嘗試重新排序 2 個連續的檔案編號,這是不允許的- 所以重新開始洗牌。
最后,我們有一組變數,例如
#1001=1004
#1002=1003
#1003=1002
#1004=1001
這意味著 file.001 將變為 .004 等。
因此,執行:shuffleren重命名檔案。
我選擇通過附加額外的 2 個擴展名(新名稱和 a t)來重命名檔案,然后洗掉t. 這顯示了舊名稱和新名稱的跟蹤(檔案名.002 在上面變成了檔案名.002.003)。更改"%filename%*.!#oldname:~-3!.!#newname:~-3!.t"為"%filename%*.!#newname:~-3!.t"會將 filename.002 更改為 filename.003。
最后,unshuffle 機制依賴于一串空格分隔的數字引數,這些引數與shuffle機制列印的串列相匹配。它實際上只是更多相同,但未經檢查,因此提供重復或不足的數字無疑會造成混亂。從引數構建一個類似上面的表并呼叫
:shuffleren例程將數字改回。
當然,您應該在虛擬區域中測驗例程 - 不要將其直接應用于實時檔案。
由于該random函式是從那時開始播種的,因此我嘗試通過在數字選擇機制中添加一個可以在命令列上指定的數字來應用一些可變性,因此可以使用 0 或 1 個數字引數 - 如果提供了一個數字引數,該引數被添加到亂數中,因此如果您碰巧在兩天內同時運行例程,您可以有一點變化。
- - 修訂
@echo off
setlocal enabledelayedexpansion
set "sourcedir=c:\testso"
set "filename=%1"
:: clear any variables starting #
for /f "tokens=1delims==" %%e in ('set # 2^>nul') do set "%%e="
:: number of files
set /a #files=50
set /a #files1k=1000 #files
:: length of code to generate
set /a #lencode=10
set "parm= %* "
set /a #count=0
set /a #randomadd=0
set /a #assigned=0
call :readparms %parm%
:: if 0 or 1 parameter, shuffle randomly
if %#count% gtr 2 goto extendkey
:shuffle
set "#shuffle=y"
for /L %%e in (1,1,%#files%) do set "#%%e="
set "#report="
set /a #assigned=0
: nextrandom
call :assignrandom
if %#assigned% lss %#lencode% goto nextrandom
set "#key=%#report%"
:: Extend the key currently in #Report
:extendkey
set /a #accum=1
:nextmore
call :addmore %#report%
if %#assigned% equ %#files% goto donerandom
if %#accum% neq 10 goto nextmore
:: attempt to assign all unassigned
for /L %%e in (%#files,-1,1) do (
set /a #choice=%%e-1
call :addreport
)
if %#assigned% neq %#files% if defined #shuffle goto shuffle
if %#assigned% neq %#files% echo Fail&goto :eof
:donerandom
if defined #shuffle echo key is %#key%
: now build rename-list
for /L %%e in (1000,1,%#files1k%) do set "#%%e="
set /a #position=1000
:brlloop
set /a #position =1
set /a #value=%#report% 2>nul
set /a #value =1000
if defined #shuffle (set /a #%#position%=#value
) else set /a #%#value%=#position
set "#report=%#report:* =%"
if defined #report goto brlloop
:shuffleren
for /L %%e in (1001,1,%#files1k%) do (
set "#oldname=%%e"
set "#newname=!#%%e!"
ren "%sourcedir%\%filename%*.!#oldname:~-3!" "%filename%*.!#oldname:~-3!.!#newname:~-3!.t"
)
for %%b in ("%sourcedir%\%filename%*.t") do ren "%%b" "%%~nb"
goto :eof
:readparms
shift
set "#choice=%1"
if not defined #choice goto :eof
set "#choice=9%1"
for /L %%e in (0,1,9) do set "#choice=!#choice:%%e=!"
if defined #choice echo ignored "%1" &goto readparms
set /a #randomadd=%1
set /a #count =1
set /a #choice=%1-1
call :addreport
goto readparms
:: Assign a new random number to #report
:assignrandom
set /a #choice=%random% #randomadd
:: Caution - flow-through
:: Add and count an entry into #report
:: don't add if #choice is already in #report or #assigned
:addreport
set /a #choice=(#choice %% #files) 1
set /a #assigned =1
for %%y in (%#assigned% %#report%) do if %#choice%==%%y set /a #assigned-=1&goto :eof
set "#report=%#report%%#choice% "
goto :eof
:: Add some more numbers derived from the existing list
:addmore
set /a #accum =1
:movel
if "%1"=="" goto :eof
set /a #choice=0
for /L %%y in (1,1,%#accum%) do call set /a #choice=#choice %%%%y 2>nul
call :addreport
shift
goto movel
此代碼將“密鑰”限制為#lencode。
第一步是處理提供的引數,但這次將每個引數構建到#report 中(名稱是早期處理的遺留物)
如果提供的引數少于 2 個,則這是一個 suffle 操作并#shuffle已設定。在這種情況下,#lencode 亂數被添加到新的#report 中。如果#shuffle未設定,則該鍵已在命令列中提供并已分配給#report。
以此#report為基礎,應用特定功能,添加新數字。這意味著#report 中的結果對于這兩種情況(洗牌或非洗牌)將是相同的,然后需要將1000 添加到數字以進行子串化并構建#1xxx 陣列;用于洗牌或解洗;然后像以前一樣運行重命名代碼。
如果提供了不正確的代碼,則解洗將導致混亂。檔案重命名跟蹤驗證系統保留了早期代碼。
-- 另一個版本
@echo off
setlocal enabledelayedexpansion
set "sourcedir=c:\testso"
set "filename=%1"
:: clear any variables starting #
for /f "tokens=1delims==" %%e in ('set # 2^>nul') do set "%%e="
:: number of files
set /a #files=50
set /a #files1k=1000 #files
:: length of code to generate
set /a #lencode=10
set "parm= %* "
:: Set flag #shuffle
for %%e in (shuffle) do (
if "!parm!" neq "!parm:/%%e=!" set "#%%e=Y"&set "parm=!parm:/%%e=!"
)
set /a #count=0
set /a #randomadd=0
set /a #assigned=0
call :readparms %parm%
:: if 0 or 1 parameter, shuffle randomly
if %#count% gtr 2 goto extendkey
:shuffle
set "#shuffle=y"
for /L %%e in (1,1,%#files%) do set "#%%e="
set "#report="
set /a #assigned=0
: nextrandom
call :assignrandom
if %#assigned% lss %#lencode% goto nextrandom
set "#key=%#report%"
:: Extend the key currently in #Report
:extendkey
set /a #accum=1
:nextmore
call :addmore %#report%
if %#assigned% equ %#files% goto donerandom
if %#accum% neq 10 goto nextmore
:: attempt to assign all unassigned
for /L %%e in (%#files,-1,1) do (
set /a #choice=%%e-1
call :addreport
)
if %#assigned% neq %#files% if defined #shuffle goto shuffle
if %#assigned% neq %#files% echo Fail&goto :eof
:donerandom
if defined #shuffle if defined #key echo key is %#key%
: now build rename-list
for /L %%e in (1000,1,%#files1k%) do set "#%%e="
set /a #position=1000
:brlloop
set /a #position =1
set /a #value=%#report% 2>nul
set /a #value =1000
if defined #shuffle (set /a #%#position%=#value
) else set /a #%#value%=#position
set "#report=%#report:* =%"
if defined #report goto brlloop
:shuffleren
for /L %%e in (1001,1,%#files1k%) do (
set "#oldname=%%e"
set "#newname=!#%%e!"
ren "%sourcedir%\%filename%*.!#oldname:~-3!" "%filename%*.!#oldname:~-3!.!#newname:~-3!.t"
)
for %%b in ("%sourcedir%\%filename%*.t") do ren "%%b" "%%~nb"
goto :eof
:readparms
shift
set "#choice=%1"
if not defined #choice goto :eof
set "#choice=9%1"
for /L %%e in (0,1,9) do set "#choice=!#choice:%%e=!"
if defined #choice echo ignored "%1" &goto readparms
set /a #randomadd=%1
set /a #count =1
set /a #choice=%1-1
call :addreport
goto readparms
:: Assign a new random number to #report
:assignrandom
set /a #choice=%random% #randomadd
:: Caution - flow-through
:: Add and count an entry into #report
:: don't add if #choice is already in #report or #assigned
:addreport
set /a #choice=(#choice %% #files) 1
set /a #assigned =1
for %%y in (%#assigned% %#report%) do if %#choice%==%%y set /a #assigned-=1&goto :eof
set "#report=%#report%%#choice% "
goto :eof
:: Add some more numbers derived from the existing list
:addmore
set /a #accum =1
:movel
if "%1"=="" goto :eof
set /a #choice=0
for /L %%y in (1,1,%#accum%) do call set /a #choice=#choice %%%%y 2>nul
call :addreport
shift
goto movel
/shuffle這一次,如果您提供密鑰并希望隨機播放,則需要添加一個開關。如果沒有/shuffle開關,指定少于 2 個數字引數將隨機打亂,超過 2 個則取消打亂。
請注意,#key只會為隨機洗牌建立,因此這是唯一一次報告它(否則,提供了密鑰或它正在解洗)。
uj5u.com熱心網友回復:
你的問題不清楚。但是,我對 9 個檔案的部分“理解了一半”,所以這是我的部分解決方案。
這是shuffle.bat:
@echo off
setlocal
set "basefile=%1"
set "i=0"
md "renamed"
:loop
shift
if "%1" equ "" goto endLoop
set /A i =1
move
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507895.html
上一篇:如何在批處理檔案中運行此PowerShell腳本?[復制]
下一篇:=未被批量讀取
