用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
复制代码 代码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
复制代码
|