bat 复制文件夹,文件名递增 等操作
句尾无';'
@echo off : 回显,使命令不在dos中一行一行输出
pause : 暂停,以便看到输出结果
变量
%% 与 % % : https://zhidao.baidu.com/question/518580373.html
%xxx%
%%i (for) 变量名:单字符 ???
/r /d : https://blog.csdn.net/ab7434588/article/details/53055890
还有其它符号
算数运算 : https://blog.csdn.net/sanqima/article/details/37902463
%1 %2 %3 ...
命令行上的参数
@echo off set a=% echo %a% pause
C:\Users\scientific\Desktop>.bat abc abc 请按任意键继续. . .
判断是否相等,存在,定义
https://zhidao.baidu.com/question/872612194823360892.html
if else
if xxx ( ) else ( ::else必须在这行 )
输出换行符
echo.
注释
https://blog.csdn.net/wh_19910525/article/details/8125762
转义字符
echo ^&
在linux下用-e 输出转义字符\t等。在windows下暂时找不到。
非常重要!
延迟变量
setlocal enabledelayedexpansion : http://blog.sina.com.cn/s/blog_a9cdad020102wugf.html
for循环里, () 一个整体
字符串截取 长度为变量 (for循环内不行,目前尚未找到解决方法)
@echo off
setlocal enabledelayedexpansion set str=
set /a delta1=-
set str1=!str:~,%delta1%! 外面必须是!!
echo %str1% pause
应用:
文件夹下所有文件夹重新命名为1,2,3,...
目录有‘*’,一定要加上
@echo off
setlocal enabledelayedexpansion
set /a ind=
for /d %%i in (C:\Users\scientific\Desktop\test\*) do (
set /a ind+=
ren "%%i" "!ind!"
) pause
文件夹构:
1 --xxx
2
3 --yyy1
4
5 --file1
6
7 --file2
8
9 ...
10
11 --yyy2
12
13 ....
14
15 --yyy3
16
17 ....
18
19 ...
文件夹下所有文件夹重新命名为xxx1,xxx2,xxx3,...
@echo off
setlocal enabledelayedexpansion
set /a ind=
set str="code"
for /d %%i in (C:\Users\scientific\Desktop\test\*) do (
set /a ind+=
ren "%%i" "%str%!ind!"
) pause
文件夹下所有文件重新命名为xxx1,xxx2,xxx3,...
@echo off
setlocal enabledelayedexpansion
set /a ind=
set str="z"
for %%i in (C:\Users\scientific\Desktop\test\*) do (
set /a ind+=
ren "%%i" "%str%!ind!"
) pause
文件夹下名称含有yyy的文件夹重新命名为xxx1,xxx2,xxx3,...
>nul 不输出
!errorlevel!
@echo off
setlocal enabledelayedexpansion for /d %%i in (C:\Users\scientific\Desktop\test\*) do (
echo %%i | findstr "code" >nul
if !errorlevel! equ (
set /a ind+=
ren "%%i" "!ind!"
)
) pause
输出每个文件夹有多少文件
@echo off
setlocal enabledelayedexpansion for /d %%i in (C:\Users\scientific\Desktop\test\*) do (
set /a g=
for %%j in (%%i\*) do (
set /a g+=
)
echo %%i !g!
) pause
复制某个文件夹多次(命名为xxx001,xxx002,xxx003,...)
/e:复制目录和子目录,包括空的
/y:不需要确认修改文件
C:\Users\scientific\Desktop>tree code /f
文件夹 PATH 列表
卷序列号为 EC4C-63EE
C:\USERS\SCIENTIFIC\DESKTOP\CODE
│ .txt
│
└─
.txt C:\Users\scientific\Desktop>tree test /f
文件夹 PATH 列表
卷序列号为 EC4C-63EE
C:\USERS\SCIENTIFIC\DESKTOP\TEST
├─code001
│ │ .txt
│ │
│ └─
│ .txt
│
├─code002
│ │ .txt
│ │
│ └─
│ .txt
│
├─code003
│ │ .txt
│ │
│ └─
│ .txt
│
├─code004
│ │ .txt
│ │
│ └─
│ .txt
│
└─code005
│ .txt
│
└─
.txt
@echo off
setlocal enabledelayedexpansion set addr1="C:\Users\scientific\Desktop\code"
set addr2="C:\Users\scientific\Desktop\test" for /l %%i in (,,) do (
echo %%i
set /a ind+=
set num=%%i
set num=!num:~-!
echo d | xcopy %addr1% %addr2%\code!num! /e /y
) pause
c++项目 一个文件夹。多个题目,拷贝文件夹多份
@echo off
setlocal enabledelayedexpansion set addr1=".\basic"
set addr2=".\" for /l %%i in (,,) do (
echo %%i
set /a ind+=
set num=%%i
set num=!num:~-!
echo d | xcopy %addr1% %addr2%\code!num! /e /y
) pause
测试命名是否正确,每个文件夹里的文件的名字是否包含文件夹的名字
C:\Users\scientific\Desktop\test>tree /f
文件夹 PATH 列表
卷序列号为 EC4C-63EE
C:.
│ scr.txt
│ test1.bat
│
├─code1
│ code1_1.txt
│ code1_2.txt
│ code1_3.txt
│
├─code2
│ code2_1.txt
│ code2_2.txt
│
└─code3
.txt
code3_1.txt
@echo off
setlocal enabledelayedexpansion set addr=C:\Users\scientific\Desktop\test
for /f "delims=" %%i in ('dir %addr% /b') do (
for /f "delims=" %%j in ('dir %addr%\%%i /b') do (
echo %%j | findstr %%i >nul
if !errorlevel! equ (
echo %addr%\%%i\%%j
)
)
)
) pause
at last from http://tieba.baidu.com/p/2683225056
bat优点:简单易懂 缺点:运行效率慢(解释性语言)
c优点:功能强大、运行效率高 缺点:命令过于复杂(某些)
最新文章
- HTML 的 meta 标签
- Django 发布时间格式化
- 远程debug hadoop
- 2015年Java开发岗位面试题归类
- Hadoop设置环境变量注意事项
- sdut 2413:n a^o7 !(第三届山东省省赛原题,水题,字符串处理)
- 基于jQuery的AJAX和JSON的实例
- 关于echo `git branch | grep \*`
- ASP.NET - Eval数据绑定
- 闭包(匿名函数) php
- Linux内核互斥锁--mutex
- Centos 6.5安装Python3.6
- 【转】教你开发jQuery插件
- Download and Install Apache Zookeeper on Ubuntu
- redhat 6.5安装ansible
- 新闻API接口
- Windows上安装 TensorFlow及简单命令
- 生成并下载txt类型的文件
- 联想电脑t450,t460p,t470等安装好ubuntu后启动找不到系统
- Java调整JVM内存大小——(八)