[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

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

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

第 20、28、36、44行,goto 跳转在for语句内,这个地方应该错了
bat小白,请多指教!谢谢!

TOP

回复 2# 77七
怎么个错法?

TOP

回复 1# oyr520


我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 3# oyr520


   

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

bat小白,请多指教!谢谢!

TOP

回复 5# 77七


    看着不像。AI应该不会犯这种错误
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 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
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 5# 77七
是的

TOP

回复 6# Batcher

是用ChatGPT生成的

TOP

回复 7# Batcher


    可以,谢谢大佬

TOP

返回列表