标题: [文件操作] 复制文件和文件夹的批处理报错此时不应有 )。请问是哪里的问题呢? [打印本页]
作者: oyr520 时间: 2023-3-17 13:39 标题: 复制文件和文件夹的批处理报错此时不应有 )。请问是哪里的问题呢?
- @echo off
- setlocal enabledelayedexpansion
-
- set source=%cd%
- set destination=C:\test
-
- set excludes=Folder1 Folder2 File1.txt File2.txt
-
- if not exist "%destination%" (
- echo Error: test directory does not exist.
- pause
- exit
- )
-
- for /f "delims=" %%i in ('dir /b /a-d "%source%"') do (
- set exclude=false
- for %%j in (%excludes%) do (
- if /i "%%i"=="%%j" (
- set exclude=true
- goto :exclude
- )
- )
- if exist "%destination%\%%i" (
- echo Skipping file: %%i
- ) else (
- robocopy "%source%" "%destination%" "%%i" /s /njh /njs /ndl /np /xf %excludes%
- )
- :exclude
- )
-
- for /f "delims=" %%i in ('dir /b /ad "%source%"') do (
- set exclude=false
- for %%j in (%excludes%) do (
- if /i "%%i"=="%%j" (
- set exclude=true
- goto :exclude
- )
- )
- if exist "%destination%\%%i" (
- echo Skipping folder: %%i
- ) else (
- robocopy "%source%" "%destination%" "%%i" /s /njh /njs /ndl /np /xf %excludes%
- )
- :exclude
- )
-
- echo Done!
- pause
- exit
复制代码
作者: 77七 时间: 2023-3-17 13:59
第 20、28、36、44行,goto 跳转在for语句内,这个地方应该错了
作者: oyr520 时间: 2023-3-17 15:29
回复 2# 77七
怎么个错法?
作者: Batcher 时间: 2023-3-17 15:41
回复 1# oyr520
作者: 77七 时间: 2023-3-17 16:20
回复 3# oyr520
你这代码chatgpt写的?
exclude 这个既是变量又是标签,定义来定义去,不知道什么意思,我是没看明白
作者: Batcher 时间: 2023-3-17 16:44
回复 5# 77七
看着不像。AI应该不会犯这种错误
作者: Batcher 时间: 2023-3-17 17:50
回复 1# oyr520
在你的代码基础上简化一下,使用 findstr /v 排除指定文件和文件夹:- @echo off
- setlocal enabledelayedexpansion
-
- set "source=%cd%"
- set "destination=C:\test"
- set "excludes=Folder1 Folder2 File1.txt File2.txt"
-
- if not exist "%destination%" (
- echo Error: test directory does not exist.
- pause
- exit /b
- )
-
- for /f "delims=" %%i in ('dir /b /a-d "%source%" ^| findstr /v "%excludes%"') do (
- if exist "%destination%\%%i" (
- echo Skipping file: %%i
- ) else (
- robocopy "%source%" "%destination%" "%%i" /njh /njs /ndl /np
- )
- )
-
- for /f "delims=" %%i in ('dir /b /ad "%source%" ^| findstr /v "%excludes%"') do (
- if exist "%destination%\%%i" (
- echo Skipping folder: %%i
- ) else (
- robocopy "%source%\%%i" "%destination%\%%i" /s /njh /njs /ndl /np
- )
- )
-
- echo Done!
- pause
- exit /b
复制代码
作者: oyr520 时间: 2023-4-8 10:09
回复 5# 77七
是的
作者: oyr520 时间: 2023-4-8 10:09
回复 6# Batcher
是用ChatGPT生成的
作者: oyr520 时间: 2023-4-8 10:17
回复 7# Batcher
可以,谢谢大佬
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |