How to empty files size in a directory ?

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
vindiou
Junior Member
Junior Member
Posts: 7
Joined: 2011-07-14, 20:21 UTC

How to empty files size in a directory ?

Post by *vindiou »

Hi,

I have a directory with a lot of files.
I want to keep their file names but reset/empty their size to 0 byte.

How can I do that?

Thanks!
User avatar
Dalai
Power Member
Power Member
Posts: 9945
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

A simple batch script can help with that:

Code: Select all

@echo off

REM How to empty files size in a directory ?
REM http://ghisler.ch/board/viewtopic.php?t=42001

if "%~1"=="" goto :EOF

pause

for /F "tokens=* delims=" %%f IN (%~1) DO (
    echo %%~f
    copy NUL "%%~f"> NUL
)
title All files done
pause
Call this script with a button like this (adapt the button to your needs):

Code: Select all

TOTALCMD#BAR#DATA
e:\42001.cmd
%L
shell32.dll,-153
42001


-1
Mark the files you want to clear the sizes of and then press the button.
Be aware that the script overwrites all marked files in this directory without warning and prompt! So make sure to run the script with some test files first!

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
vindiou
Junior Member
Junior Member
Posts: 7
Joined: 2011-07-14, 20:21 UTC

Post by *vindiou »

Thanks but it doesn't work with file names with accents. Examples:

BEFORE :
impôts.pdf 25 000 000 bytes
boite à cigarres.ts 250 000 000 bytes

AFTER:
impôts.pdf 25 000 000 bytes
imp¶ts.pdf 0 bytes
boite à cigarres.ts 250 000 000 bytes
boite Ó cigarres.ts 0 bytes

Any idea how to correct this?

Thanks!
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

vindiou wrote:Any idea how to correct this?
Try to change the used code page in "DOS" console to the one from Windows.

To do that:


1) Check which code page your Windows system use.
Browse the Registry for, or execute this DOS commando:
[face=timesnewroman]reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /V ACP[/face]

For example, I get for Germany this 1252:
C:\Temp\>reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /V ACP

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
ACP REG_SZ 1252


C:\Temp\>



2) Open a "DOS-Box" and use [face=timesnewroman]CHCP[/face] to see the used code page in the console... probably 863 (I have 850)?

Next you would change the console code page (temporary, for this session) to the Windows cp:
[face=timesnewroman]CHCP 1252[/face] (or what your cp is for you, probably 1252 too?)
From now on, the console will use this codepage for the output. (but now the text in the console is "rubbish", that's normal)
If you close the console, the CP setting is gone too.


For your case scenario you can use this command "chcp 1252" in Dalais' script after the "@echo off " statement:
@echo off
chcp 1252
...
...



HTH?   8)


EDIT:

found some more ideas:

A) just use UTF-8 code page, regardless what you Win CP is:
[face=timesnewroman]REM change CHCP to UTF-8
CHCP 65001
[/face]

or

B) start the console in UNICODE mode (see cmd /? for info):
[face=timesnewroman]cmd /C /U myBatch.cmd[/face]
(Note: Unicode use two bytes for each sign/char, so the resultig file size will be doubled)

.
Last edited by Stefan2 on 2015-03-28, 17:56 UTC, edited 1 time in total.
User avatar
Dalai
Power Member
Power Member
Posts: 9945
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

vindiou wrote:Thanks but it doesn't work with file names with accents.
Any idea how to correct this?
Yup. Automated version:

Code: Select all

@echo off

REM How to empty files size in a directory ?
REM http://ghisler.ch/board/viewtopic.php?t=42001

if "%~1"=="" goto :EOF

set skip=4
set r=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage
REM --- reg.exe on Windows 7 prints less lines compared to XP
reg.exe /? | findstr "Version 3.0" > NUL || set skip=2

:GET_CODEPAGE
for /f "skip=%skip% tokens=3" %%i in (
  'reg.exe query "%r%" /v "ACP"'
) do (
  set active_cp=%%i
)

for /f "skip=%skip% tokens=3" %%i in (
  'reg.exe query "%r%" /v "OEMCP"'
) do (
  set oem_cp=%%i
) 

pause

REM --- if-command: Workaround for Windows XP64, where there's no chcp on 32 bit CMD
if "[%ProgramFiles(x86)%]" == "[]" chcp %active_cp% > NUL

for /F "tokens=* delims=" %%f IN (%~1) DO (
    echo %%~f
    copy NUL "%%~f"> NUL
)

REM --- if-command: Workaround for Windows XP64, where there's no chcp on 32 bit CMD
if "[%ProgramFiles(x86)%]" == "[]" chcp %oem_cp% > NUL

title All files done

pause
I hope I didn't forget anything...

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
MVV
Power Member
Power Member
Posts: 8711
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

It would be much easier using %WL and for with type command:

Code: Select all

for /f "usebackq delims=" %%f in (`type %1`) do echo %%f
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Post by *milo1012 »

MVV wrote:It would be much easier using %WL
Indeed, no need for some hackish code page setting.
Cmd.exe can work with Unicode.
(the only drawback is that the visible text might get garbaged)

Sth. like this started with %WL Parameter should work for all kind of file names, due to UTF-16:

Code: Select all

@echo off
set tt=
for /f "usebackq delims=" %%f in (`type %1`) do (
  copy NUL "%%~f" > tt
  if ERRORLEVEL 1 (
    echo %%~f
    type tt
  )
)
It would also work for an UTF-8 (%UL) file, but unfortunately TC won't skip the BOM in that case.
TC plugins: PCREsearch and RegXtract
vindiou
Junior Member
Junior Member
Posts: 7
Joined: 2011-07-14, 20:21 UTC

Post by *vindiou »

perfect!! Thanks!!
Post Reply