Batch文件操作命令

曹乐意
2023-12-01

创建文件:

http://stackoverflow.com/questions/210201/how-to-create-empty-text-file-from-a-batch-file

Makes a 0 byte file a very clear, backward-compatible way:

type nul >EmptyFile.txt

idea via: anonymousDanny Backett, possibly others, myself inspired by JdeBP's work

A 0 byte file another way, it's backward-compatible-looking:

REM. >EmptyFile.txt

idea via: Johannes

A 0 byte file 3rd way backward-compatible-looking, too:

echo. 2>EmptyFile.txt

idea via: TheSmurf

A 0 byte file the systematic way probably available since Windows 2000:

fsutil file createnew EmptyFile.txt 0

idea via: Emm

A 0 bytes file overwriting readonly files

ATTRIB -R filename.ext>NUL
(CD.>filename.ext)2>NUL

idea via: copyitright

A single newline (2 bytes: 0x0D 0x0A in hex notation, alternatively written as \r\n):

echo.>AlmostEmptyFile.txt

Note: no space between echo. and >.

idea via: How can you echo a newline in batch files?


edit It seems that any invalid command redirected to a file would create an empty file. heh, a feature! compatibility: uknown

TheInvisibleFeature <nul >EmptyFile.txt

A 0 bytes file: invalid command/ with a random name (compatibility: uknown):

%RANDOM%-%TIME:~6,5% <nul >EmptyFile.txt

via: great source for random by Hung Huynh

edit 2 Andriy M points out the probably most amusing/provoking way to achieve this via invalid command

A 0 bytes file: invalid command/ the funky way (compatibility: unknown)

*>EmptyFile.txt

idea via: Andriy M


删除文件:

http://stackoverflow.com/questions/13764103/batch-script-to-delete-files

del “文件路径和文件名”


检测文件存在:

http://stackoverflow.com/questions/4340350/how-to-check-if-a-file-exists-from-inside-a-batch-file

if exist 文件路径/名() else ()

if exist c:\autoexec.bat notepad c:\autoexec.bat

检测文件不存在:

if not exist


写入文件:

http://www.bleepingcomputer.com/forums/t/446804/write-to-file-batch-script/

echo ^<?xml version="1.0" encoding="ISO-8859-1"?^> >> file.xml
echo ^<nexgen_audio_export^> >> file.xml

创建文件夹:

http://superuser.com/questions/717143/how-to-create-new-folders-on-desktop-with-a-batch-script

mkdir C:\path\to\dersired\directory

检测文件夹是否存在:

http://superuser.com/questions/219050/how-to-check-if-a-directory-exists-in-windows

if exist 文件夹名/ () else()


进入文件夹:

cd


我写的:

if not exist "target\" (mkdir target)
cd target
if not exist "surefire-reports\" (mkdir surefire-reports)
cd surefire-reports
if exist report.xml (
  del "report.xml"
)





 类似资料: