清空檔案的方法
Problem
在Windows上如果採用砍掉重建或copy NUL的方法,有可能會因為檔案正在被使用而無法操作:
copy /y NUL file.txt
How to?
以下方式都是會清空,但最上方會有空白行:
echo. > file.txt echo( > file.txt echo[ > file.txt完全清空可以透過以下幾種方法:
break > file.txt type NUL > file.txt但這些方法依然會受到檔案使用中的限制;最後發現可以透過poweshell去清空內容,搭配batch使用的方法:
powershell.exe -ExecutionPolicy Bypass -Command "Clear-Content file.txt"
Reference