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 ...

命令行上的参数

 1 @echo off 2 3 set a=%1 4 echo %a% 5 6 pause 

 1 C:UsersscientificDesktop>3.bat abc 2 abc 3 请按任意键继续. . . 

判断是否相等,存在,定义

https://zhidao.baidu.com/question/872612194823360892.html

if else

 1 if xxx ( 2 3 ) else (  ::else必须在这行 4 5 )

输出换行符

 1 echo. 

注释

https://blog.csdn.net/wh_19910525/article/details/8125762

转义字符

 1 echo ^& 

在linux下用-e 输出转义字符 等。在windows下暂时找不到。

非常重要!

延迟变量

setlocal enabledelayedexpansion :  http://blog.sina.com.cn/s/blog_a9cdad020102wugf.html

for循环里, () 一个整体

字符串截取 长度为变量 (for循环内不行,目前尚未找到解决方法)

1 @echo off
2 setlocal enabledelayedexpansion
3 
4 set str=12345
5 set /a delta1=-3
6 set str1=!str:~0,%delta1%! 外面必须是!!
7 echo %str1%
8 
9 pause

应用:

文件夹下所有文件夹重新命名为1,2,3,...

目录有‘*’,一定要加上

1 @echo off
2 setlocal enabledelayedexpansion
3 set /a ind=0
4 for /d %%i in (C:UsersscientificDesktop	est*) do (
5     set /a ind+=1
6     ren "%%i" "!ind!"
7 )
8 
9 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,...

 1 @echo off
 2 setlocal enabledelayedexpansion
 3 set /a ind=0
 4 set str="code"
 5 for /d %%i in (C:UsersscientificDesktop	est*) do (
 6     set /a ind+=1
 7     ren "%%i" "%str%!ind!"
 8 )
 9 
10 pause

文件夹下所有文件重新命名为xxx1,xxx2,xxx3,...

 1 @echo off
 2 setlocal enabledelayedexpansion
 3 set /a ind=0
 4 set str="z"
 5 for %%i in (C:UsersscientificDesktop	est*) do (
 6     set /a ind+=1
 7     ren "%%i" "%str%!ind!"
 8 )
 9 
10 pause

文件夹下名称含有yyy的文件夹重新命名为xxx1,xxx2,xxx3,...

>nul 不输出

!errorlevel!

 1 @echo off
 2 setlocal enabledelayedexpansion
 3 
 4 for /d %%i in (C:UsersscientificDesktop	est*) do (
 5     echo %%i | findstr "code" >nul
 6     if !errorlevel! equ 0 (
 7         set /a ind+=1
 8         ren "%%i" "!ind!"
 9     )
10 )
11 
12 pause

输出每个文件夹有多少文件

 1 @echo off
 2 setlocal enabledelayedexpansion
 3 
 4 for /d %%i in (C:UsersscientificDesktop	est*) do (
 5     set /a g=0
 6     for %%j in (%%i*) do (
 7         set /a g+=1
 8     )
 9     echo %%i !g!
10 )
11 
12 pause

复制某个文件夹多次(命名为xxx001,xxx002,xxx003,...)

/e:复制目录和子目录,包括空的

/y:不需要确认修改文件

 1 C:UsersscientificDesktop>tree code  /f
 2 文件夹 PATH 列表
 3 卷序列号为 EC4C-63EE
 4 C:USERSSCIENTIFICDESKTOPCODE
 52.txt
 6  7 └─1
 8         1.txt
 9 
10 
11 C:UsersscientificDesktop>tree test /f
12 文件夹 PATH 列表
13 卷序列号为 EC4C-63EE
14 C:USERSSCIENTIFICDESKTOPTEST
15 ├─code001
16 │  │  2.txt
17 │  │
18 │  └─1
191.txt
20 21 ├─code002
22 │  │  2.txt
23 │  │
24 │  └─1
251.txt
26 27 ├─code003
28 │  │  2.txt
29 │  │
30 │  └─1
311.txt
32 33 ├─code004
34 │  │  2.txt
35 │  │
36 │  └─1
371.txt
38 39 └─code005
402.txt
41 42     └─1
43             1.txt
 1 @echo off
 2 setlocal enabledelayedexpansion
 3 
 4 set addr1="C:UsersscientificDesktopcode"
 5 set addr2="C:UsersscientificDesktop	est"
 6 
 7 for /l %%i in (1,1,5) do (
 8     echo %%i
 9     set /a ind+=1
10     set num=00%%i
11     set num=!num:~-3!
12     echo d | xcopy %addr1% %addr2%code!num! /e /y
13 )
14 
15 pause

c++项目 一个文件夹。多个题目,拷贝文件夹多份

 1 @echo off
 2 setlocal enabledelayedexpansion
 3 
 4 set addr1=".asic"
 5 set addr2="."
 6 
 7 for /l %%i in (1,1,60) do (
 8     echo %%i
 9     set /a ind+=1
10     set num=00%%i
11     set num=!num:~-3!
12     echo d | xcopy %addr1% %addr2%code!num! /e /y
13 )
14 
15 pause

测试命名是否正确,每个文件夹里的文件的名字是否包含文件夹的名字

 1 C:UsersscientificDesktop	est>tree /f
 2 文件夹 PATH 列表
 3 卷序列号为 EC4C-63EE
 4 C:.
 5 │  scr.txt
 6 │  test1.bat
 7  8 ├─code1
 9 │      code1_1.txt
10 │      code1_2.txt
11 │      code1_3.txt
12 13 ├─code2
14 │      code2_1.txt
15 │      code2_2.txt
16 17 └─code3
18         2.txt
19         code3_1.txt
 1 @echo off
 2 setlocal enabledelayedexpansion
 3 
 4 set addr=C:UsersscientificDesktop	est
 5 for /f "delims=" %%i in ('dir %addr% /b') do (
 6         for /f "delims=" %%j in ('dir %addr%\%%i /b') do (
 7             echo %%j | findstr %%i >nul
 8             if !errorlevel! equ 1 (
 9                 echo %addr%\%%i\%%j
10             )
11         )
12     )
13 )
14 
15 pause

at last  from http://tieba.baidu.com/p/2683225056

bat优点:简单易懂 缺点:运行效率慢(解释性语言)
c优点:功能强大、运行效率高 缺点:命令过于复杂(某些)

原文地址:https://www.cnblogs.com/cmyg/p/10473658.html