标题: [文件操作] 【分享】批处理把俩个文件名称互相交换 [打印本页]
作者: zhengzhenhui 时间: 2024-7-26 23:37 标题: 【分享】批处理把俩个文件名称互相交换
我看论坛没有,所以分享
比较好奇的是运用场景在哪里啊.....
把俩个文件名称交换【拖动到bat处理】- @echo off
- if "%~2"=="" (
- echo Drag two files into this bat to swapping files names
- pause
- exit /b
- )
- if not "%~3"=="" (
- echo Can only swap two files names
- pause
- exit /b
- )
- set "file1=%~n1"
- set "file2=%~n2"
- set "ext1=%~x1"
- set "ext2=%~x2"
- set "temp=%random%"
- ren "%~1" "%temp%%ext1%"
- ren "%~2" "%file1%%ext2%"
- ren "%temp%%ext1%" "%file2%%ext1%"
复制代码
作者: ShowCode 时间: 2024-7-27 12:50
回复 1# zhengzhenhui
4个set去掉.bat- @echo off
- if "%~2"=="" (
- echo Drag two files into this bat to swapping files names
- pause
- exit /b
- )
- if not "%~3"=="" (
- echo Can only swap two files names
- pause
- exit /b
- )
- set "temp=%random%"
- ren "%~1" "%temp%%~x1"
- ren "%~2" "%~n1%~x2"
- ren "%temp%%~x1" "%~n2%~x1"
复制代码
作者: ShowCode 时间: 2024-7-27 12:53
回复 1# zhengzhenhui
temp是默认环境变量建议改为自定义变量.bat- @echo off
- if "%~2"=="" (
- echo Drag two files into this bat to swapping files names
- pause
- exit /b
- )
- if not "%~3"=="" (
- echo Can only swap two files names
- pause
- exit /b
- )
- set "MyTemp=%random%"
- ren "%~1" "%MyTemp%%~x1"
- ren "%~2" "%~n1%~x2"
- ren "%MyTemp%%~x1" "%~n2%~x1"
复制代码
作者: 77七 时间: 2024-7-27 15:21
本帖最后由 77七 于 2024-7-27 15:23 编辑
用随机数作为临时文件名存在文件名重复可能吧,数字文件名还是挺常见的。- @echo off
- if "%~3" equ "" (
- if "%~2" neq "" (
- ren "%~2" "$"
- ren "%~1" "%~nx2"
- ren "%~dp2\$" "%~nx1"
- ) else (
- echo Drag two files into this bat to swapping files names
- )
- ) else (
- echo Can only swap two files names
- )
- pause
复制代码
作者: aloha20200628 时间: 2024-7-28 11:16
本帖最后由 aloha20200628 于 2024-7-28 11:26 编辑
回复 1# zhengzhenhui
以下代码可以接受同目录中两个文件一次拖入,亦可接受两个不同目录中的文件逐个拖入...
- @echo off & if "%~2"=="" set/p="命令行须拖入或输入两个文件参数"&exit/b
- ren "%~1" "%~nx2.tmp" && ren "%~2" "%~nx1" && ren "%~dp1%~nx2.tmp" "%~nx2"
- pause&exit/b
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |