[APORTE] [BATCH] Filesize Truncator

Iniciado por Eleкtro, 14 Abril 2014, 05:01 AM

0 Miembros y 1 Visitante están viendo este tema.

Eleкtro

Filesize Truncator

Este Script trunca el tamaño de los archivos que estén en el directorio de trabajo actual y en sus subdirectorios.
Nota: El directorio y otras propiedades se pueden modificar en las variables documentadas del código.

En realidad lo que hace es eliminar permanentemente el archivo, y luego se crea un archivo de 0 bytes con el mismo nombre y en la misma ubicación que el que se borró.

Esto es util por ejemplo si necesitamos crear una estructura de carpetas y archivos pero estos pesan demasiado tamaño y solo necesitamos mantener los nombres de los archivos y sus ubicaciones...

El Script tiene 2 métodos de búsqueda de archivos, el modo normal y el modo que incluye archivos ocultos.

Imágenes:




Source:
Código (dos) [Seleccionar]
@Echo OFF



REM =================
REM Console Settings:
REM =================

Title Filesize Truncator - By Elektro
Mode Con Cols=150 Lines=50
CHCP 1252 1>NUL & REM Windows-1252, Spanish-Latin.



REM =====
REM Info:
REM =====

Echo+
Echo  ------------------------------------------------------------------------------------------------------------
Echo  This script will truncate to Zero the size of the files inside the working directory and it's subdirectories
Echo  ------------------------------------------------------------------------------------------------------------
Echo  Take extreme caution, truncating the filesize has the same effect as deleting the file !!
Echo  -----------------------------------------------------------------------------------------
Echo+



REM ====================
REM User defined values:
REM ====================

REM This value indicates the directory where to list it's file structure.
Set "WorkingDir=%CD%"

REM This value indicates the ubication of the logfile that will record info.
Set "Logfile=%CD%\%~n0.log"

REM This value indicates the File-Extensions to exclude during the process.
REM ( Use an ';' delimiter to separate multiple extensions )
Set "ExcludeExts=.bat;.cmd"

REM This value indicates the filenames to exclude during the process.
REM ( Use an ';' delimiter to separate multiple filenames )
Set "ExcludeNames=%~nx0;Files.txt;Files2List.log"

REM This value indicates the files to exclude during the process.
REM ( Use an ';' delimiter to separate multiple files )
Set "ExcludeFiles=%Logfile%"

REM This value indicates how the hidden files are threated when listing the file structure.
REM  True = List files and also files with the 'hidden' attribute.
REM False = List only normal files without the 'hidden' attribute.
Set "IncludeHiddenFiles=True"



REM =====
REM Main:
REM =====

:: Warning question.
Call :CreateLog
Echo+
Choice /C "YN" /M "This will delete the files, you want to continue?"
If %ErrorLevel% NEQ 1 (Exit)
Echo+

:: Call Methods.
Call :CheckErrors
Call :StartTimer
Call :TruncateFiles "%IncludeHiddenFiles%"
Call :StopTimer
Call :EndLog
Pause&Exit



REM ========
REM Methods:
REM ========

:CreateLog
:: Create the Script Logfile and record starting info on it.
FSutil.exe File CreateNew "%LogFile%" "0" 1>NUL
(
Echo+
Echo    Filesize Truncator
Echo =========================
Echo   %DATE% ^| %TIME:~0,-3%
Echo /\/\/\/\/\/\/\/\/\/\/\/\/
Echo+
Echo [i] Working Directory...: %WorkingDir%
Echo [i] Include Hidden Files: %IncludeHiddenFiles%
Echo [i] Excluded Extensions.: %ExcludeExts%
Echo [i] Excluded Filenames..: %ExcludeNames%
Echo [i] Excluded Files......: %ExcludeFiles%
Echo [i] Log file............: %LogFile%
Echo+
Echo =========================
)>"%LogFile%"

:: Display starting log info.
Type "%LogFile%" | MORE

:: Continue writting log.
(
Echo+
Echo Excluded Files:
Echo ===============
Echo+
)>>"%LogFile%"

Goto:EOF


:EndLog
(
Echo+
Echo [i] Done!
Echo [i] Truncated Files: %TruncatedFileCount% files.
Echo [i] Excluded Files.: %ExcludedFileCount% files.
Echo [i] Elapsed Time...: %ElapsedTime%
Echo+
)>>"%LogFile%"

:: Display ending information.
CLS
Type "%LogFile%"
Goto:EOF


:StartTimer
Set "StartingDate=%Date%"
Set "StarttingTime=%Time:~0,-3%"
Goto :EOF


:StopTimer
(
Echo Minutes = DateDiff^("n", "%StartingDate% %StarttingTime%", Now^)
Echo WScript.Echo Minutes ^& " Minutes"
)>"%TEMP%\%~n0 MinuteDiff.vbs"

For /F "Tokens=*" %%# In (
'Cscript.exe /Nologo "%TEMP%\%~n0 MinuteDiff.vbs"'
) Do (
Set "ElapsedTime=%%#"
)

Goto:EOF


:TakeOwn
(
Takeown.exe /F "%~f1"
ICacls.exe "%~f1" /Grant "%UserName%":"F"
Attrib.exe -R -A -S -H -I "%~f1"
)>NUL || (Exit /B 1)
Goto:EOF


:TruncateFiles
Echo [+] Truncating files with size greater than Zero, this operation could take some minutes long, please wait...
If /I "%~1" EQU "True"  (Call :TruncateHiddenFiles)
If /I "%~1" EQU "False" (Call :TruncateNormalFiles)
Goto:EOF


:TruncateNormalFiles
FOR /R "%WorkingDir%" %%# in ("*") DO (

If %%~z# NEQ 0 (

Set "Exclude="

If Defined ExcludeExts (
(
Echo "%ExcludeExts%" | Find.exe /I "%%~x#" 1>NUL 2>&1
) && (
Call Set "Exclude=True"
)
)

If Defined ExcludeNames (
(
Echo "%ExcludeNames%" | Find.exe /I "%%~nx#" 1>NUL 2>&1
) && (
Call Set "Exclude=True"
)
)

If Defined ExcludeFiles (
(
Echo "%ExcludeFiles%" | Find.exe /I "%%~f#" 1>NUL 2>&1
) && (
Call Set "Exclude=True"
)
)

If Not Defined Exclude (
Set "File=%%~f#"
Call Echo [.] Truncating: %%File:%WorkingDir%=.%%
Call :TakeOwn "%%~f#"
Del /Q "%%~f#"
FSUtil.exe File CreateNew "%%~f#" "0" 1>NUL
Set /A "TruncatedFileCount+=1"
) Else (
Set "File=%%~f#"
Call Echo %%File:%WorkingDir%=.%%>>"%LogFile%"
Set /A "ExcludedFileCount+=1"
)
)
)
Goto:EOF


:TruncateHiddenFiles
FOR /F "Tokens=* Delims=" %%# in ('Dir /B /S /A-D "%WorkingDir%\*"') DO (

If %%~z# NEQ 0 (

Set "Exclude="

If Defined ExcludeExts (
(
Echo "%ExcludeExts%" | Find.exe /I "%%~x#" 1>NUL 2>&1
) && (
Call Set "Exclude=True"
)
)

If Defined ExcludeNames (
(
Echo "%ExcludeNames%" | Find.exe /I "%%~nx#" 1>NUL 2>&1
) && (
Call Set "Exclude=True"
)
)

If Defined ExcludeFiles (
(
Echo "%ExcludeFiles%" | Find.exe /I "%%~f#" 1>NUL 2>&1
) && (
Call Set "Exclude=True"
)
)

If Not Defined Exclude (
Set "File=%%~f#"
Call Echo [.] Truncating: %%File:%WorkingDir%=.%%
Call :TakeOwn "%%~f#"
Del /Q "%%~f#"
FSUtil.exe File CreateNew "%%~f#" "0" 1>NUL
Set /A "TruncatedFileCount+=1"
) Else (
Set "File=%%~f#"
Call Echo %%File:%WorkingDir%=.%%>>"%LogFile%"
Set /A "ExcludedFileCount+=1"
)

)
)
Goto:EOF



REM ===============
REM Error Controls:
REM ===============

:CheckErrors
:: 'IncludeHiddenFiles' Value check.
If /I "%IncludeHiddenFiles%" NEQ "True" If /I "%IncludeHiddenFiles%" NEQ "False" (
Echo [x] Error parsing parameter 'IncludeHiddenFiles',
Echo     value '%IncludeHiddenFiles%' is not a Boolean value.
Pause&Exit
)

Goto:EOF


Saludos