windows bat系列2:创建文件_创建指定大小文件

方式1:echo重定向

如,在当前脚本目录下重定向创建文件:

set redirect=%~dp0\redirect.txt
echo "This a redirect line">>%redirect%

方式2:fsutil工具

备注:fsutil工具在C:\Windows\System32下,需要管理员权限才能执行。所以,若写成.bat,请以管理员身份运行脚本。如,分别创建1k,1M大小的文件:

::设置文件名
set onekfile=%~dp0\1k.txt
set onemfile=%~dp0\1m.txt
::设置文件大小变量1K
set onek=1024
:: /a表示是个表达式  1M
set /a onem=1024*1024

fsutil file createnew %onekfile% %onek%
fsutil file createnew %onemfile% %onem%

测试(fsutil.bat)

::关闭echo回显
@echo off 

::当前盘符
echo current pan : %~d0

::当前路径
echo current path : %cd%

::当前执行命令行
echo current cmd : %0

::当前bat文件路径
echo the bat's path : %~dp0

::当前bat文件短路径
echo the bat's short path : %~sdp0

::进入脚本所在路径
cd %~dp0

::创建文件1: 追加重定向>>或重定向>都行
set redirect=%~dp0\redirect.txt
echo "This a redirect line">>%redirect%

::创建文件2: fsutil工具,创建指定大小文件
::设置文件名
set onekfile=%~dp0\1k.txt
set onemfile=%~dp0\1m.txt
::设置文件大小变量1K
set onek=1024
:: a表示是个表达式  1M
set /a onem=1024*1024

fsutil file createnew %onekfile% %onek%
fsutil file createnew %onemfile% %onem%

::暂停bat脚本
Pause

测试结果(以管理员身份运行脚本)



猜你喜欢

转载自blog.csdn.net/zhaogang1993/article/details/80444974