How to zero size all files in a folder

Page 2 of 2 FirstFirst 12

  1. Posts : 481
    Windows 10 pro 1903 1862.145
       #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 Computer


  2. Posts : 31,651
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #12

    tcebob said:
    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....

    How to zero size all files in a folder-zero.png

    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 Computers


  3. Posts : 31,651
    10 Home x64 (22H2) (10 Pro on 2nd pc)
       #13

    bro67 said:
    ...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 Computers


  4. Posts : 1,811
    W7 Ultimate SP1 (64 bit), LM 19.2 MATE (64 bit), W10 Home 1703 (64 bit), W10 Pro 1703 (64 bit) VM
       #14

    stub said:
    @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 Computer


  5. Posts : 5,478
    2004
       #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) :
    How to zero size all files in a folder-capture1.png

    Second time, if you added one file it just processes that - you can see size changed from 9 to 0 :
    How to zero size all files in a folder-capture2.png

    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 Computer


  6. Posts : 287
    win 10 home
       #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 Computer


  7. Posts : 157
    Win10 v20H2 Build 19042.804
    Thread Starter
       #17

    Creating Zero Byte Files of a folder of large sized files


    Hang onto your hats. This is going to be a bumpy ride.

    I understand where the OP is coming from. I also want to download large graphics files, on maybe a daily basis, and store them in a folder, and I want the presence of the zero byte files to prevent a download to this folder with the same name, but to download with the next available increment. For example. I might have filename.jpg and filename (1).jpg etc. So the next download of a filename.jpg or filename (1).jpg to be downloaded as filename (2).jpg.

    As an aside. I think that is how both File Explorer and Filezilla function. If I'm not mistaken.

    Then I want to visually compare the files which are not zero byte in this download folder with the filename*.jpg in my master folder of full size filenames, and if I have that picture already, to simply delete the filename in the download folder, or if I don't have the picture to copy it to my master folder of full size filenames, and then zero byte that file in my download folder.

    Now I'm not an expert with Windows, so this structure might be achieved with some alternative better structuring. Why not compare the filename and size to be downloaded with a list of this filename*.jpg in my master folder of full size filenames, and if there is a file of the exact same size, to abort the filename download. If not, then to download to the zero byte folder, as described above. Now why to do this? It will prevent many unnecessary processing of files in the download files, and therefore less manual processing by me. This only exacerbates with the more files I store. Currently around 5K of files. By having the list of zero byte files in the download folder is only a tool in this process, but having these filenames with an without sizes, it is easy to see which files require further processing. These could be 1byte in size if it makes things easier in any way, like when deleting files etc.

    Now I'm open to any questions or clarifications on what I'm trying to automate.
      My Computer


  8. Posts : 16,946
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #18

    stub,

    I know that this was all a long time ago but, if you are still looking for a solution, take a peek at
    garlin #22 - TenForums
    You will only be interested in the vbs command garlin has posted.
    That vbs runs a PowerShell command and the PS is what you might be interested in extracting.
    The rest of garlin's post deals with that OP's desire to have the zero-ing command available in the context menu.

    All the best,
    Denis
      My Computer


  9. Posts : 773
    Windows 7
       #19

    This snippet uses Clear-Content (which zeroes a file), and "% { }" is a short-hand for Where-Object.
    I check if the file is read-only, and temporarily fix it allow truncation.

    Code:
    Get-ChildItem -File -Recurse $args[0] | % {
        if ($_.IsReadOnly -eq $true) {
            $_.IsReadOnly = $false
            Clear-Content $_.FullName
            $_.IsReadOnly = $true
        }
        else {
            Clear-Content $_.FullName
        }
    }
      My Computer


 

  Related Discussions
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 21:38.
Find Us




Windows 10 Forums