2

This is Windows 10. The question is very simple, I simply don't know the proper syntax to do this.

This will list all files ending in .log:

dir /s /p *.log

This will truncate a file:

echo|set /p=>"myfile.log"

What I would like to do is pipe the output of the first operation to the second operation, thus truncating the matching files returned by dir. How would I do this?

2
  • So you want to clear every log in a directory of any input it could have?
    – Seth
    Commented Nov 22, 2016 at 13:34
  • Yes Seth that is correct. Commented Nov 22, 2016 at 13:56

3 Answers 3

1

Piping for the normal cmd isn't that common in Windows. So for the cmd you'd probably have to get a list of those files in variable and use a for loop to process them. Maybe you could also directly use the dir command in the loop.

I'd opt for PowerShell, which does a similar thing but is way more readable in my opinion:

Get-ChildItem -Recurse -Filter "*.log" | Clear-Content

If you want to confirm every single file (the /P on your dir does pause every screen) you could use the -Confirm switch on Clear-Content.

If you'd want to use a for loop for whatever reason and use echo you could use:

Get-ChildItem -Recurse -Filter "*.log" | %{ echo $null > $_; }
3
  • I will look into PowerShell. Perhaps a link to that for future viewers of this post. Thanks. Commented Nov 22, 2016 at 15:06
  • I tried this and added -Recurse succesfully, and -Filter exactfilename.log successfully, however, PowerShell doesn't seem to like the * character Commented Nov 22, 2016 at 16:23
  • What do you mean it doesn't like the asterisk? Did you put it in quotes?
    – Seth
    Commented Nov 23, 2016 at 6:15
0

To just collect the dir output in the log file, drop the /p and redirect dir /s *.log >myfile.log

That will create a new file, deleting any previous contents.

If you want the full path on each line, instead of listing by subdirectory, use /b dir /s /b *.log >myfile.log

1
  • Leo, that is not what i want to do. i want to truncate the files the dir returns to 0 bytes - not write anything to myfile.log. There is no myfile.log - that is just an example. Edited my post to clarify this. Commented Nov 22, 2016 at 13:59
0

How do I truncate the matching files returned by dir /s /p *.log?

Use the following batch file (test.cmd):

@echo off
setlocal
for /f "tokens=*" %%f in ('dir /b /s *.log') do (
  echo|set /p=>"%%f"
  )
endlocal

Example usage:

> dir /s /p *.log
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

02/10/2016  17:02             2,305 test.log
               1 File(s)          2,305 bytes

 Directory of F:\test\test

02/10/2016  17:02             2,305 test.log
               1 File(s)          2,305 bytes

 Directory of F:\test\test with space

02/10/2016  17:02             2,305 test.log
               1 File(s)          2,305 bytes

     Total Files Listed:
               3 File(s)          6,915 bytes
               0 Dir(s)  1,737,619,595,264 bytes free

> test

> dir /s /p *.log
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

24/11/2016  10:39                 0 test.log
               1 File(s)              0 bytes

 Directory of F:\test\test

24/11/2016  10:39                 0 test.log
               1 File(s)              0 bytes

 Directory of F:\test\test with space

24/11/2016  10:39                 0 test.log
               1 File(s)              0 bytes

     Total Files Listed:
               3 File(s)              0 bytes
               0 Dir(s)  1,737,619,607,552 bytes free

Further Reading

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .