How to empty files size in a directory ?
Moderators: Hacker, petermad, Stefan2, white
How to empty files size in a directory ?
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!
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!
A simple batch script can help with that:Call this script with a button like this (adapt the button to your needs):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
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
Code: Select all
TOTALCMD#BAR#DATA
e:\42001.cmd
%L
shell32.dll,-153
42001
-1
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
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64
Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
Try to change the used code page in "DOS" console to the one from Windows.vindiou wrote:Any idea how to correct this?
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?

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.
vindiou wrote:Thanks but it doesn't work with file names with accents.
Yup. Automated version:Any idea how to correct this?
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
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
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64
Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
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
Indeed, no need for some hackish code page setting.MVV wrote:It would be much easier using %WL
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
)
)
TC plugins: PCREsearch and RegXtract