iT邦幫忙

0

BAT批次檔修改資料夾名稱並新建一個資料夾

A資料夾中有很多層子資料夾,
今天想把A資料夾中每一層子資料夾的檔案刪除,
並且將每一層子資料夾中名為x、y、z的資料夾名稱改名為a、b、c,
並且在有這些a、b、c資料夾的地方新增一個d資料夾,
目前小弟寫到語法如下,卡在「新增一個d資料夾」的地方不知該如何下手:

del /F /S /Q D:\A*.*
for /r D:\A\ %%a in (x) do rename %%a a
for /r D:\A\ %%a in (y) do rename %%a b
for /r D:\A\ %%a in (z) do rename %%a c

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
jeles51
iT邦研究生 3 級 ‧ 2020-09-02 16:39:00
最佳解答

透過 Powershell.
以下另存為 test.ps1
在 cmd 下執行方式 "Powershell -File test.ps1"
在 powershell 下執行方式 " .\test.ps1"

cmd /c "del /F /S /Q d:\test\*.*"
cmd /c "for /r d:\test %a in (x) do rename %a a"
cmd /c "for /r d:\test %a in (y) do rename %a b"
cmd /c "for /r d:\test %a in (z) do rename %a c"

$dest='d:\test'

#主資料夾檢查是否有 abc 同時存在,若有,則建立 d
if ((Test-Path $dest\a ) -and (Test-Path $dest\b )-and (Test-Path $dest\c) )
	{New-Item -ItemType directory -Path $dest\d	-ErrorAction SilentlyContinue}
	
#列出所有資料夾物件
$folders=Get-ChildItem -Path $dest -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName

#檢查是否有 abc 同時存在,若有,則建立 d
 foreach($a in $folders)
 {   
 $AA=$a.Fullname+'\a'
 $BB=$a.Fullname+'\b'
 $CC=$a.Fullname+'\c'
 $DD=$a.Fullname+'\d'
 if ((Test-Path $AA ) -and (Test-Path $BB )-and (Test-Path $CC) )
	{
	New-Item -ItemType directory -Path $DD -ErrorAction SilentlyContinue
	}
 }
看更多先前的回應...收起先前的回應...
yuki0402 iT邦新手 5 級 ‧ 2020-09-07 10:48:55 檢舉

參考了一下,還是有點困難阿@@
主要還有卡在要新增的資料夾路徑是未知的@@

yuki0402 iT邦新手 5 級 ‧ 2020-09-14 12:33:44 檢舉

參考值行後,還是未執行新增一個d資料夾@@
我看執行好像只停在cmd /c "for /r D:\test %a in (x) do rename %a a"這一行,沒有執行後續的指令QQ

jeles51 iT邦研究生 3 級 ‧ 2020-09-14 13:22:07 檢舉

上面內容,全部存成一個副檔名為 d:\test.ps1 的檔案,
在 cmd 執行方式

powershell -File "d:\test.ps1"
yuki0402 iT邦新手 5 級 ‧ 2020-09-14 13:37:32 檢舉

OK了~~感謝

yuki0402 iT邦新手 5 級 ‧ 2020-09-14 13:44:43 檢舉

可以再請問一下嗎?為何將abc換成中文檔名就不行了呢@@

jeles51 iT邦研究生 3 級 ‧ 2020-09-14 14:08:38 檢舉
yuki0402 iT邦新手 5 級 ‧ 2020-09-14 16:13:08 檢舉

感謝,成功了!

我要發表回答

立即登入回答