文档库

最新最全的文档下载
当前位置:文档库 > bat常用实例整理-1

bat常用实例整理-1

001 显示1-5
@echo off
for /l %%i in (1,1,5) do @echo %%i
pause
-----------------------------------------------------------------------
002 复制文件夹
拷贝:
在E盘下有文件夹0,创建文件夹1并将0中所有内容拷贝到1中:
@echo off
::创建1文件夹
md E:\1
::拷贝0文件夹所有内容到1文件夹
xcopy E:\0 E:\1 /s

拷贝:
在E盘下有文件夹0,创建文件夹1-5并将0中所有内容拷贝到1-5中:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%i in (1 1 5) do (
set var=%%i
md E:\!var!
xcopy \0 E:\!var! /s)
pause
-------------------------------------------------------------------
003 批量copy命令 生成bat文件 并运行bat

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%i in (1 1 10) do (
set var=%%i
echo md E:\!var!>>test.bat
echo xcopy \0 E:\!var! /s>>test.bat)
::运行bat文件
test.bat
pause
---------------------------------------------------------------------
004 替换字符串
注E:\0下有文件yy.txt,把其内容中所有的0替换成AAA
@echo off
setlocal enabledelayedexpansion
set fn=E:\0\yy.txt
(for /f "tokens=*" %%i in (%fn%) do (
set s=%%i
set s=!s:0=AAA!
echo !s!))>temp.txt
move /y temp.txt "%fn%"
---------------------------------------------------------------------
005 copy并替换字符串
E:\0下有文件yy.txt,要求拷贝文件夹0到文件夹1,并替换文件夹1中yy.txt内容中所有0为AAA

@echo off
md E:\1
xcopy \0 E:\1 /s

setlocal enabledelayedexpansion
set fn=E:\1\yy.txt
(for /f "tokens=*" %%i in (%fn%) do (
set s=%%i
set s=!s:0=AAA!
echo !s!))>temp.txt

move /y temp.txt "%fn%"
---------------------------------------------------------------------
006 复制多个文件夹 使最后一个文件夹中的某文件 的某内容被替换
E:\0下有文件yy.txt,要求创建文件夹1-5,并拷贝文件夹0到文件夹1-5,替换文件夹5中yy.txt内容中所有0为AAA

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%i in (1 1 5) do (
set var=%%i
md E:\!var!
xcopy \0 E:\!var! /s
)

setlocal enabledelayedexpansion
set fn=E:\!var!\yy.txt
(for /f "tokens=*" %%j in (%fn%) do (
set s=%%j
set s=!s:0=AAA!
echo !s!))>temp.txt
copy /y temp.txt "%fn%"
pause

---------------------------------------------------------------------
007 把文件123456.txt的第16行ws.sendkeys后面“”里面的内容,改成自己的文件名

原文件123456.txt:
set ws = CreateObject("wscript.shell")
ws.run "notepad"
ws.AppActivate "notepad"
wscript.sleep 1000
ws.sendkeys "H"
wscript.sleep 100
ws.sendkeys "e"
wscript.sleep 100
Ws.SendKeys "l"
wscript.sleep 100
Ws.SendKeys "l"
wscript.sleep 100
Ws.SendKeys "e"
wscript.sleep 100
Ws.SendKeys "o"
ws.sendkeys "1930000000"
wscript.sleep 100
ws.sendkeys "{Enter}"

-------------
bat文件
@echo off&pushd "e:\文件\"
for /f "delims=" %%a in ('dir /b *.txt') do (
setlocal enabledelayedexpa

bat常用实例整理-1

(共4页)