iT邦幫忙

1

文件夾第二層複製到新的資料夾 bat檔 求救

  • 分享至 

  • xImage

文件夾第二層複製到新的資料夾 bat檔
我舉例:

如果原文件夾中有以下目錄結構:
C:\Users\cally\Desktop\output\A\aaa
C:\Users\cally\Desktop\output\B\bbb
C:\Users\cally\Desktop\output\VD\aaa
C:\Users\cally\Desktop\output\mss\aaa
C:\Users\cally\Desktop\output\m21d\aaa
C:\Users\cally\Desktop\output\2mo\ccc
C:\Users\cally\Desktop\output\9id\bbb

那麼將在目標文件夾C:\Users\cally\Desktop\newoutput中複製上方的第二層結構
如果出現相同檔名的話就新增墜詞1,2,3......以此類推。
C:\Users\cally\Desktop\newoutput\aaa
C:\Users\cally\Desktop\newoutput\bbb
C:\Users\cally\Desktop\newoutput\aaa_1
C:\Users\cally\Desktop\newoutput\aaa_2
C:\Users\cally\Desktop\newoutput\aaa_3
C:\Users\cally\Desktop\newoutput\ccc
C:\Users\cally\Desktop\newoutput\bbb_1

參考的bat程式碼如下:
@echo off
setlocal EnableDelayedExpansion

set sourceFolder=C:\Users\cally\Desktop\output
set targetFolder=C:\Users\cally\Desktop\newoutput

if not exist "%targetFolder%" mkdir "%targetFolder%"

for /d %%i in ("%sourceFolder%*") do (
set count=0
for /d %%j in ("%%i*") do (
set "folderName=%%~nxj"
if not exist "%targetFolder%!folderName!" (
xcopy "%%j" "%targetFolder%!folderName!" /E /I
) else (
set /A count+=1
set "newFolderName=!folderName!!count!"
:checkName
if exist "%targetFolder%!newFolderName!" (
set /A count+=1
set "newFolderName=!folderName!
!count!"
goto checkName
)
xcopy "%%j" "%targetFolder%!newFolderName!" /E /I
)
)
)

echo All done!
pause

發生的問提
程式往往在複製勘查的時候,如果文件夾有相同的時候超過3個以上,則程式就會出現
"找不到檔案 - %j"
也就是只會出現
C:\Users\cally\Desktop\newoutput\aaa_1
C:\Users\cally\Desktop\newoutput\aaa_2
死都不跑出來"C:\Users\cally\Desktop\newoutput\aaa_3"
然後程式就出現錯誤卡住了。

求高手幫忙阿~~~是否哪迴圈或是代碼沒寫好!!!

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

2 個回答

1
JamesDoge
iT邦高手 1 級 ‧ 2023-03-07 23:38:40
@echo off
setlocal EnableDelayedExpansion

set sourceFolder=C:\Users\cally\Desktop\output
set targetFolder=C:\Users\cally\Desktop\newoutput

if not exist "%targetFolder%" mkdir "%targetFolder%"

for /d %%i in ("%sourceFolder%") do (
set count=0
for /d %%j in ("%%i") do (
set "folderName=%%~nxj"
if not exist "%targetFolder%!folderName!" (
xcopy "%%j" "%targetFolder%!folderName!" /E /I
) else (
set /A count+=1
set "newFolderName=!folderName!!count!"
set "maxCount=3" // 設定上限值
:checkName
if exist "%targetFolder%!newFolderName!" (
if !count! == !maxCount! ( // 當 count 值達到上限時,跳出循環
echo Cannot copy folder "%%j", reached maximum count of !maxCount!.
goto :eof
)
set /A count+=1
set "newFolderName=!folderName!!count!"
goto checkName
)
xcopy "%%j" "%targetFolder%!newFolderName!" /E /I
)
)
)

echo All done!
pause
0
re.Zero
iT邦研究生 5 級 ‧ 2023-03-10 01:39:29

發文語法請參考:Markdown說明 - iT邦幫忙

:BOF
@echo off
setlocal EnableDelayedExpansion
@rem 
set "sourceFolder=%USERPROFILE%\Desktop\output"
set "targetFolder=%USERPROFILE%\Desktop\newoutput"
@rem 
if exist "%targetFolder%" (echo "%targetFolder%" exist; ) else (mkdir "%targetFolder%" )
@rem 
for /d %%i in ("%sourceFolder%\*") do (
  for /d %%j in ("%%~i\*") do (
    set "DestName=%%~nxj"
    call :setDestName
    xcopy "%%~j" "%targetFolder%\!DestName!" /E /I
  )
)
@rem 
echo All done!
pause
@rem 
endlocal
goto :EOF
@rem 
:setDestName
if not exist "%targetFolder%\!DestName!" goto :EOF
set /A "count=0"
:setDestNameBOLoop
set /A "count+=1"
set "newDestName=!DestName!_!count!"
if exist "%targetFolder%\!newDestName!" goto :setDestNameBOLoop
set "DestName=!newDestName!"
goto :EOF
@rem 
:EOF

我要發表回答

立即登入回答