在 Windows 中快速交换文件的实用程序或技术

在 Windows 中快速交换文件的实用程序或技术

我经常需要将一个文件与另一个文件交换,而不覆盖原始文件。

假设有两个文件foo_new.dllfoo.dll。我通常按以下方式重命名它们:

  1. foo.dll-> foo_old.dll
  2. foo_new.dll-> foo.dll
  3. [对替换的文件进行一些操作],
  4. foo.dll-> foo_new.dll
  5. foo_old.dll-> foo.dll

对于单个文件交换来说这是可以的,但是当一次交换多个文件时就会变得繁琐。

是否有一个 Windows(7 和最好是 XP)实用程序或技术可以简化此任务并且在交换多个文件时运行良好?

我希望能够在 FreeCommander 中使用它,但 Windows 资源管理器也可以。

答案1

如果您更改文件名约定,则可以轻松地通过使用通配符的 RENAME 来管理多个文件的交换。

不要调用你的文件,而是foo_new.dll调用它们foo.dll.new。初始交换后,原始文件foo.dll将被称为foo.dll.old

然后您可以使用:

rem First swap in the new files

  rem The following command appends .old to all files with .dll extension (foo.dll -> foo.dll.old)
  ren *.dll *?.old

  rem The following command removes all .new extensions (foo.dll.new -> foo.dll)
  ren *.new *.

rem Now you can work with the new replacements

rem Finally swap out the new files and restore the old ones

  rem Add .new to the dll files (foo.dll -> foo.dll.new)
  ren *.dll *?.new

  rem Remove .old extensions (foo.dll.old -> foo.dll)
  ren *.old *.

Windows RENAME 命令如何解释通配符?以获得有关 RENAME 如何处理通配符的完整解释。

上述的一个限制是它假设您目录中的每个 .dll 文件都有一个替代品。

使用 FOR 命令的简单扩展可用于交换 .dll 文件的子集(仅限已定义 *.dll.new 的文件)。将命令放在一对批处理脚本中可能是最简单的方法。

使用以下swapInNew.bat批处理文件来交换新文件:

@echo off
for %%F in (*.new) do (
  move "%%~nF" "~nF.old"
  move "%%F" "%%~nF"
)

现在您可以根据需要使用替换文件

最后,使用以下restoreOld.bat批处理文件恢复旧文件:

@echo off
for %%F in (*.old) do (
  move "%%~nF" "%%~nF.new"
  move "%%F" "%%~nF"
)

答案2

对于(1)

>for /f "delims=" %f in ('dir *.cmd /a-d /on /b') do @ren %f %~nf_old%~xf

对于(2)

>for /f "delims=" %f in ('dir *_new.cmd /a-d /on /b') do @(@set nf=%~nf & @ren %f %nf:~0,-5%%~xf)

对于(3)

rem Anything You Can Do!

对于(4)

>for /f "delims=" %f in ('dir *.cmd /a-d /on /b') do @ren %f %~nf_new%~xf

对于(5)

>for /f "delims=" %f in ('dir *_old.cmd /a-d /on /b') do @(@set nf=%~nf & @ren %f %nf:~0,-5%%~xf)

希望有特定需求来设计特定脚本。欢迎任何改进想法。:)

相关内容