How to blank files in batch
How would I go about reading an entire directory and blanking files with a specific extension? I have an application that reads the content of a specific folder and returns an error if a file is missing, however it does't check to see if the files are valid, so I want to make them NULL to get arou开发者_开发百科nd the checks.
if by 'blanking' you mean truncating them, you could use the following:
for /f %%a in ('dir *.[my ext]') do (echo . > %%a)
note that the double %
is for use within a batch file. if you are running this from a command line, use a single %
.
EDIT:
to incorporate @Loadmaster's improvement:
for /f %%a in ('dir *.[my ext]') do (type nul > %%a)
First, create an empty file. Call it "blank". You can use Notepad to just save an empty file, for example.
Let's suppose the specific extension is ".xyz". Run this:
for %f in (*.xyz) do copy /y blank %f
The for loop sets variable "%f" to each file name in turn, and runs the copy command. The copy command copies the blank file on top of each matching file.
By the way, you can find out more about the for
command using the help
command:
help for
You could make the batch file take the filter and then it's much more useful.
@for %%i in (%1) do cd.>%%i
Usage (if you call it empty.bat):
empty *.c
精彩评论