Windows 10: How to zero size all files in a folder

Page 2 of 2 FirstFirst 12
  1. tcebob's Avatar
    Posts : 387
    Windows 10 pro 1803/17134.285
       24 May 2018 #11

    Indeed, it is possible to create and save 0-sized files. I just tried with Notepad, thinking it would pop up an error, but no, there it is: 0.txt. Perhaps you could craft a script that would copy such a file from your Documents directory, rename it to the desired filename and save it where you want it. I'm pretty sure you could use a batch file instead of Power Shell.
      My ComputerSystem Spec

  2. Bree's Avatar
    Posts : 8,585
    10 Home x64 (1803) (10 Pro on 2nd pc)
       24 May 2018 #12

    tcebob said: View Post
    I'm pretty sure you could use a batch file instead of Power Shell.
    It's probably more elegant to do it in powershell, in a batch file you don't have the commands available that can zero the length of an existing file, or create a new file. It's a bit of a kludge, but it can be done.

    DIR can make a list of the filenames, and a FOR loop can work through that list. What you can do is pipe the output of a command that has no output text to a file (so you are making a zero length file, just as you can in Notepad). If that happens to be an existing file it will overwrite it with a new zero length file of the same name. There are very few commands that can be tricked into outputting nothing, but I have found one that will do the job.

    This, copied and pasted into a batch file called ZERO.BAT will first list all the files in the current folder to a file called files.txt, then overwrite each of them with a zero length file of the same name. The 'dir /b *.xxx' command was chosen for this as it will produce no output (unless of course you actually do have files with the .xxx extension). It's a one-shot suicidal batch file, as it will in the process zero itself!

    Code:
    @dir /b *.* > files.txt
    @for /f "delims=" %%n in (files.txt) do @dir /b *.xxx>"%%n"
    The result is....

    Click image for larger version. 

Name:	zero.png 
Views:	41 
Size:	27.9 KB 
ID:	190086

    As I said, it's a kludge, it's messy because the 'dir /b *.xxx' echos a 'file not found' to the screen, and it leaves behind the files.txt and zero.bat files - but it does work.
      My ComputersSystem Spec

  3. Bree's Avatar
    Posts : 8,585
    10 Home x64 (1803) (10 Pro on 2nd pc)
       24 May 2018 #13

    bro67 said: View Post
    ...it would mess up the File Table and MBR, if it saw zero sized files.
    It's perfectly legitimate to create an ntfs directory entry for a zero length file. You do it every time you right-click and select New > Text document.
      My ComputersSystem Spec

  4. lehnerus2000's Avatar
    Posts : 1,761
    W7 Ultimate SP1 (64 bit), LM 18.3 MATE (64 bit), W10 Home 1703 (64 bit), W10 Pro 1703 (64 bit) VM
       25 May 2018 #14

    stub said: View Post
    @margrove55 - The purpose is I'm continually receiving new files into this folder, and when after I have processed them, I want to convert the original files in the folder to zero bytes, so that I cannot receive the same files twice and process them twice.
    Why not use another folder called "processed" and move the processed files there?
      My ComputerSystem Spec

  5.    25 May 2018 #15

    Extending what polar nettles said in post six, you could select non-zero sized files and then process them. Something like
    Code:
    $Path='C:\Temp\'
    $Files=Get-ChildItem -Path $Path -File -Recurse | Where-Object  {($_.Length) -gt 0}
    ForEach ($File in $Files) { New-Item $File.FullName -Force -Value '' }
    The first line sets the root directory you want to process. The second line selects all objects of type file in this and its subdirectories where size is greater than zero - if you don't want to process subdirectories drop the -Recurse. The third line overwrites the files selected in line 2 with objects of the same name and no content.


    So first time it processes everything (in root of selected directory and subdirectories) :
    Click image for larger version. 

Name:	Capture1.PNG 
Views:	1 
Size:	45.9 KB 
ID:	190117

    Second time, if you added one file it just processes that - you can see size changed from 9 to 0 :
    Click image for larger version. 

Name:	Capture2.PNG 
Views:	0 
Size:	21.7 KB 
ID:	190118

    If you don't want to type the 3 lines every time you can save it in a file called AnythingYouWant.ps1 and call that instead.
      My ComputerSystem Spec

  6.    25 May 2018 #16

    The purpose is I'm continually receiving new files into this folder, and when after I have processed them, I want to convert the original files in the folder to zero bytes, so that I cannot receive the same files twice and process them twice.
    I've always thought it was the filename that prevents a folder from having two of the same file, not the size of the files. By leaving the original files in the folder unzeroed, you shouldn't be getting the same file twice anyway.

    Also, you are probably processing copies of the original files. Thus, the dates of the original files are unchanged. The date should be the criterium to determine whether these files have been processed or not, if you process first in, first out.

    The only usefulness I can see of zeroing out the files is to recover disk space, if these files are large in size.
      My ComputerSystem Spec


 
Page 2 of 2 FirstFirst 12

Related Threads
Solved Folder size in General Support
In W10, is there a way to always make folders open maximized. I always close them maximized, but the next time I start computer and open them, they are minimized. Thanks for looking.
Solved Size of Users folder in General Support
Hello! I have just a little query really. I have a 175GB C: Windows drive. At the moment it has 102GB used. However 47.5GB is the C: Windows/Users folder. I have moved all the pictures, videos and music from this folder to their own dedicated...
size of c:\windows folder in General Support
When I use file explorer and select c:\windows and then select 'properties' it shows size on disk = 16.6GB. When I enter that folder and select ALL files including hidden ones , the properties show size on disk at 6.4GB. chkdsk /F indicates...
Folder Size in General Support
Hi Group, I have an internal hard drive, 1 TB, just for data. If I add up the sizes of all the folders, it is about 75 GB. But properties for the drive show it has 455 GB of data. I know it has nowhere near that. The red line appears showing it...
Windows Folder Size greater than Size on Disk in Performance & Maintenance
My 32GB Windows 10 Home PC (recently upgraded from 8.1) is low on disk space. I deleted Windows.old and I have hibernation disabled. Apart from a few browsers, very little else is installed, yet I only have 11.7GB left? One reason may be the size...
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:47.
Find Us