找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 19065|回复: 9

[文件操作] 复制文件和文件夹的批处理报错此时不应有 )。请问是哪里的问题呢?

[复制链接]
发表于 2023-3-17 13:39:48 | 显示全部楼层 |阅读模式
  1. @echo off
  2. setlocal enabledelayedexpansion

  3. set source=%cd%
  4. set destination=C:\test

  5. set excludes=Folder1 Folder2 File1.txt File2.txt

  6. if not exist "%destination%" (
  7.     echo Error: test directory does not exist.
  8.     pause
  9.     exit
  10. )

  11. for /f "delims=" %%i in ('dir /b /a-d "%source%"') do (
  12.     set exclude=false
  13.     for %%j in (%excludes%) do (
  14.         if /i "%%i"=="%%j" (
  15.             set exclude=true
  16.             goto :exclude
  17.         )
  18.     )
  19.     if exist "%destination%\%%i" (
  20.         echo Skipping file: %%i
  21.     ) else (
  22.         robocopy "%source%" "%destination%" "%%i" /s /njh /njs /ndl /np /xf %excludes%
  23.     )
  24.     :exclude
  25. )

  26. for /f "delims=" %%i in ('dir /b /ad "%source%"') do (
  27.     set exclude=false
  28.     for %%j in (%excludes%) do (
  29.         if /i "%%i"=="%%j" (
  30.             set exclude=true
  31.             goto :exclude
  32.         )
  33.     )
  34.     if exist "%destination%\%%i" (
  35.         echo Skipping folder: %%i
  36.     ) else (
  37.         robocopy "%source%" "%destination%" "%%i" /s /njh /njs /ndl /np /xf %excludes%
  38.     )
  39.     :exclude
  40. )

  41. echo Done!
  42. pause
  43. exit
复制代码
发表于 2023-3-17 13:59:42 | 显示全部楼层
第 20、28、36、44行,goto 跳转在for语句内,这个地方应该错了
 楼主| 发表于 2023-3-17 15:29:16 | 显示全部楼层
回复 2# 77七
怎么个错法?
发表于 2023-3-17 15:41:08 | 显示全部楼层
回复 1# oyr520


发表于 2023-3-17 16:20:06 | 显示全部楼层
回复 3# oyr520


   

你这代码chatgpt写的?
exclude 这个既是变量又是标签,定义来定义去,不知道什么意思,我是没看明白

发表于 2023-3-17 16:44:08 | 显示全部楼层
回复 5# 77七


    看着不像。AI应该不会犯这种错误
发表于 2023-3-17 17:50:50 | 显示全部楼层
回复 1# oyr520


在你的代码基础上简化一下,使用 findstr /v 排除指定文件和文件夹:
  1. @echo off
  2. setlocal enabledelayedexpansion

  3. set "source=%cd%"
  4. set "destination=C:\test"
  5. set "excludes=Folder1 Folder2 File1.txt File2.txt"

  6. if not exist "%destination%" (
  7.     echo Error: test directory does not exist.
  8.     pause
  9.     exit /b
  10. )

  11. for /f "delims=" %%i in ('dir /b /a-d "%source%" ^| findstr /v "%excludes%"') do (
  12.     if exist "%destination%\%%i" (
  13.         echo Skipping file: %%i
  14.     ) else (
  15.         robocopy "%source%" "%destination%" "%%i" /njh /njs /ndl /np
  16.     )
  17. )

  18. for /f "delims=" %%i in ('dir /b /ad "%source%" ^| findstr /v "%excludes%"') do (
  19.     if exist "%destination%\%%i" (
  20.         echo Skipping folder: %%i
  21.     ) else (
  22.         robocopy "%source%\%%i" "%destination%\%%i" /s /njh /njs /ndl /np
  23.     )
  24. )

  25. echo Done!
  26. pause
  27. exit /b
复制代码
 楼主| 发表于 2023-4-8 10:09:12 | 显示全部楼层
回复 5# 77七
是的
 楼主| 发表于 2023-4-8 10:09:58 | 显示全部楼层
回复 6# Batcher

是用ChatGPT生成的
 楼主| 发表于 2023-4-8 10:17:45 | 显示全部楼层
回复 7# Batcher


    可以,谢谢大佬
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-19 00:02 , Processed in 0.028753 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表