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

[文件操作] [已解决]批处理如何复制文件同名不覆盖,且重命名?

[复制链接]
发表于 2024-6-7 21:45:41 | 显示全部楼层
  1. @echo off
  2. set f1=d:\from
  3. set f2=d:\to

  4. cd /d "%f1%"
  5. for /f "delims=" %%i in ('2^>nul dir /b /s /a-d *.txt *.rar') do (
  6.         set f=%%i
  7.         set nx=%%~nxi
  8.         set x=%%~xi
  9.         set n=1
  10.         call :1 "%%~nxi"
  11. )
  12. pause
  13. exit

  14.         :1
  15.         for /f "tokens=1* delims=(" %%a in ("%~1") do (
  16.                 if "%%b" neq "" (
  17.                         call :1 "%%b"
  18.                 ) else (
  19.                         for /f "tokens=* delims=0123456789" %%c in ("%%a") do (
  20.                                 if "%%c" equ ")%x%" (
  21.                                         call set "str=%%nx:(%%a=%x%%%"
  22.                                 ) else (
  23.                                         set "str=%nx%"
  24.                                 )
  25.                                 setlocal enabledelayedexpansion
  26.                                 for /f "delims=" %%e in ("!str!") do (
  27.                                         endlocal
  28.                                         call :2 "%%e"
  29.                                 )
  30.                         )
  31.                 )
  32.         )
  33.         exit /b

  34.         :2
  35.         set /a n+=1
  36.         if not exist "%f2%\%~n1%~x1" (
  37.                 copy "%f%" "%f2%\%~n1%~x1"
  38.         ) else if not exist "%f2%\%~n1(%n%)%~x1" (
  39.                 copy "%f%" "%f2%\%~n1(%n%)%~x1"
  40.         ) else (
  41.                 goto :2
  42.         )
  43.         exit /b
复制代码
发表于 2024-6-8 17:02:52 | 显示全部楼层
本帖最后由 aloha20200628 于 2024-6-8 17:10 编辑


本帖楼主的原意是将多个子目录中的同名文件复制到目标目录时须作更名处理,其更名格式如 *(1).*,因源文件不包含类似 *(1).* 的文件,故解法要比另帖(http://www.bathome.net/thread-69152-1-1.html)来的轻快,但若假定源文件名可能包含 !&^ 等特殊字符时,其解法则须另辟捷径了》既要放弃延迟变量(以便兼容源文件名中的特殊字符),又要在循环中采用计数器(以便顺序匹配目标文件名),还要逃避复合语块中%n%变量的预处理...

  1. @echo off &set "d1=c:\源目录" &set "d2=d:\目标目录"
  2. pushd "%d2%"
  3. for /f "delims=" %%a in (' dir /b/s/a-d "%d1%\*.txt" ') do (
  4.         if not exist "%%~nxa" (copy "%%a") else (
  5.                 set/a "n=1" &set "F=%%a" &set "nF=%%~na" &set "xF=%%~xa"
  6.                 (call :copy#n)
  7.         )
  8. )       
  9. popd &pause &exit/b
  10. :copy#n
  11.         if exist "%nF%(%n%)%xF%" (set/a "n+=1" &goto :copy#n)
  12.         copy "%F%" "%nF%(%n%)%xF%" &exit/b
复制代码

评分

参与人数 1技术 +1 收起 理由
77七 + 1 感谢分享!很久没注意^字符了。

查看全部评分

发表于 2024-6-8 21:04:28 | 显示全部楼层
powershell 不必纠结 bat 特殊字符
  1. $sour = ".\old"
  2. $dest = ".\new"
  3. $ext = "*.txt"

  4. Get-ChildItem -Path $sour -Filter $ext | ForEach-Object {
  5.         if ( -Not (Test-Path ( Join-Path  -Path $dest  -ChildPath $_.Name )) ) {
  6.                 Copy-Item  -Path $_.FullName  -Destination ( Join-Path -Path $dest -ChildPath $_.Name )
  7.         } else {
  8.                 $i = 0
  9.                 do {
  10.                         $i++
  11.                 }
  12.                 while ( Test-Path ( Join-Path -Path $dest -ChildPath ($_.BaseName + $i + $_.Extension) ) )
  13.                 Copy-Item  -Path $_.FullName  -Destination ( Join-Path -Path $dest -ChildPath ($_.BaseName + $i + $_.Extension) )
  14.         }
  15.        
  16. }
复制代码
发表于 2024-6-8 22:15:02 | 显示全部楼层
本帖最后由 aloha20200628 于 2024-6-9 00:11 编辑

回复 18# newswan

每家都有几曲乡愁,彼此彼此,一方水土养一方人,苦乐痛痒其实一脉相通...
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-18 00:16 , Processed in 0.018074 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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