
[已解決]拖拉文件到bat腳本執行復制到指定目錄代碼修改求助用ChatGPT生成了拖拉文件到bat執行復制到指定目錄的代碼。生成了2個不同的代碼:代碼1 只可以復制單個文件、目錄可以保持原目錄結構復制到指定目錄;代碼2 可以復制多個文件、目錄只把下一層的內容復制到指定目錄。請教一下大佬們要怎么修改代碼2能解決目錄不改變結構復制到指定目錄?謝謝代碼1,文件只能單個復制,目錄能保持原結構復制。
- @echo off
- setlocal
- rem check if the script received a valid file or directory path
- if not %~1== (
- set source=%~1
- ) else (
- echo No file or directory path specified.
- pause
- exit /b 1
- )
- rem check if the source path exists
- if not exist %source% (
- echo The source path does not exist: %source%
- pause
- exit /b 1
- )
- rem create or validate the test directory
- set testdir=E:test
- if not exist %testdir% (
- md %testdir%
- )
- rem copy the file or directory to the test directory
- if exist %source%*.* (
- xcopy /E /Y %source% %testdir%%~nx1
- ) else (
- copy /Y %source% %testdir%
- )
- echo Successfully copied %source% to %testdir%
- pause
- exit /b
nclick="copycode($('code0'));">復制代碼
代碼2,可以復制多個文件,目錄只復制下一層內容到目標目錄。
- @echo off
- set dest=E:test
- :loop
- IF %~1== GOTO done
- IF EXIST %dest%%~nx1 (
- set /p overwrite=File %~nx1 already exists in destination folder. Overwrite? (y/n):
- IF /i %overwrite%==y (
- copy /y %~1 %dest%
- shift
- Goto loop
- ) ELSE (
- shift
- Goto loop
- )
- ) ELSE (
- copy /y %~1 %dest%
- shift
- Goto loop
- )
- :done
- echo File(s) copied to %dest%
- exit
nclick="copycode($('code1'));">復制代碼