Batch file that returns folder size - size

I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.

I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0

Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/

Related

Batch file to zip Apache Tomcat for windows file names that do not equal to today

We have a number of Apache Tomcat for Windows logs - for example..
server.log.2014-02-04-10
server.log.2014-02-04-11
server.log.2014-02-04-12
server.log.2014-02-05-13
server.log.2014-02-05-14
server.log.2014-02-05-15
These are defined with a file extension of .YYYY-MM-DD-HH and by default Apache has them by date: .YYYY-MM-DD however, we have to define the HH breakover because of system usage and other reasons.
I am trying to do something very simple..
If the file extension does not equal today, to archive (zip) it up..
Here is what I have and no matter what I do is that it shows: 2014-02-05 even though the script is showing .2014-02-04.
Here is the script and I am not sure if I need SETLOCAL enabledelayexpansion..
rem http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
SETLOCAL enabledelayedexpansion
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set today=!MyDate:~0,4!-!MyDate:~4,2!-!MyDate:~6,2!
rem today=%MyDate:~0,8%
for %%i in (D:\11\*.*) do (
SET FILETIME=%%~xi
rem SET Dt=!FILETIME:~0,11!
rem IF NOT "!fldt!" == ".!today!" (
rem echo %%~ni%%~xi
rem )
)
pause
Thanks
This filters the filenames by the date string in the name and
will echo the filenames that do not have todays date in them.
#echo off
rem http://www.dostips.com/DtTipsStringManipulation.php#Snippets.Replace
SETLOCAL enabledelayedexpansion
for /f %%x in ('wmic os get localdatetime ^|find "." ') do set "MyDate=%%x"
set "today=!MyDate:~0,4!-!MyDate:~4,2!-!MyDate:~6,2!"
rem set "today=%MyDate:~0,8%"
for /f "delims=" %%a in ('dir "D:\11\*.*" /b ^|find /v ".%today%-" ') do (
echo "%%~fa"
)
pause

Batch Help -- Move File To Directory, If Exist Filename, Rename 001

I have a batch file that checks a root directory every 10 seconds for PDF files and copies those files to their corresponding subfolders of the exact same name.
What I'm missing is that I need my batch file to check the target subdirectory for the named file, and if the named file exists, to rename the new file to be copied to !filename!001.pdf and have the 001 get incremented as duplicate file names are copied. Here's what I got:
:loop
setlocal enabledelayedexpansion
cls
pushd c:\files\
for /f "tokens=*" %%1 in ('dir /a-d /b c:\files\*.pdf') do (
set filename=%%~n1&set dirname=!filename:~0,7!
dir c:\files /b *.pdf > location1list.tmp
for /f %%a in (location1list.tmp) do dir c:\files\%%a > location2list.tmp
if not exist c:\files\!dirname! (md c:\files\!dirname!)
move %%1 c:\files\!dirname!\>nul
)
timeout /t 10
goto:loop
Any suggestions how I can get the files renamed?
I.e. if Bob.pdf exists in the Bob folder and another Bob.pdf is added to the C:\files folder, I want it to be copied to the C:\files\bob folder as Bob001.pdf.
Something like this should do what you want. May need some tweaking. Also I have not tested it, just wrote it from memory. Need any explanations, just ask.
Script
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd C:\files
:Main
for /f "tokens=*" %%A in ('dir /a-d /b *.pdf') do (
if not exist "%%~dpnA\*" md "%%~nA"
set "File=%%~dpnA\%%~nxA"
if exist "!File!" call :Name "!File!" File
echo !File!
move "%%~fA" "!File!"
)
goto Wait
:Name <Target> <Variable>
set "Count=0"
:Count
set /a "Count+=1"
set "Number=00%Count%"
if exist "%~dpn1%Number:~-3%%~x1" goto Count
set "%~2=%~dpn1%Number:~-3%%~x1"
goto :eof
:Wait
timeout /t 10
goto Main
:End
popd
endlocal
Limitations
Each target subdirectory can only handle a maximum of 1000 files as this is written. If the subdirectory hits this maximum then the script will get stuck in an infinite loop.
Update
Fixed an error in my initial script

Batch for loop through variables, can't cd to each?

I have a for loop in a batch file:
#echo off
set logpath1=C:\path\to\first\log
set logpath2=C:\path\to\second\log
FOR %%G IN (%logpath1% %logpath2%) DO (
pushd %%G
pushd ..\
for %%D IN (%CD%) DO SET "dirname=%%~nxD"
popd
echo Will prefix files in %%G with %supername%
:: do operations.
popd
)
However, this doesn't seem to change my working directory. If I run the script from
C:\tmp\path\to
then I get
Will prefix files in C:\path\to\first\log with tmp
Will prefix files in C:\path\to\second\log with tmp
My purpose is to zip up log files in multiple directories. I've tried chdir, cd /d, and &~pd0 instead of %CD%, and searched high and low. Can anyone tell my why this doesn't work??
Put
setlocal enabledelayedexpansion
at the top of your batch file and use !CD! instead of %CD% in the loop. Depending on where %supername% is set you may need to use ! there too.

Batch: for /f +xcopy output - Save to .log

I have the next script, and I need it to save all the xcopy files copy outputs to one log file,
:tmdeploy
title Deploying Edithor - %deployer%
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\CompilacionQA
set dst_folder=S:\ApliTelinver\Ambientes-Edithor\Sincronizacion\Test\Test-Mantenimiento
set filelist=filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
)
echo.
pause
goto end
The problem is that I only get the last file copy in the output. And how to properly do a timestamp for .log file?
Thank you
You should use the appended redirection operator, >> instead of >.
So, your for loop will look like this:
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
)

How to make for loop in windows batch file run in name order

I have a windows batch file that does this:
for %%s in (*.sql) do call
It loops through all the sql script in a folder.
In the folder the file names are like:
s4.06.01.sql
s4.07.01.sql
s4.08.01.sql
s4.10.01.sql
s5.01.sql
But the for loop goes through the files randomly (not in the name order), it first run s5.01, then s4.06, then s4.08, then s4.10, then s4.07. How can I make them run in the name order?
It used to work, but now it doesn't. What may cause this problem?
Jerry's answer might very well be what is causing the problem.
You might solve it by changing
for %%s in (*.sql) do call
to
for %%s in (dir"*.sql ^| sort) do call
Edit cudo's to LonelyPixel
The posted solution does not work as is on a Windows 8 machine (perhaps it did back then with Windows XP but I have no idea).
Following is a working solution using Windows 8 command prompt.
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo %%s
If memory serves, it will operate on the files in the order they're returned by the file system. As such, if you run it on a disk partition that's formatted with NTFS, the names will be returned in sorted order so they'll be processed in sorted order. If the disk partition is formatted with something like FAT or FAT32, the names will be retrieved (and processed) in a more or less random order.
I too had to deal with spaces in the filenames, so I added quotes around the input variable like so:
for /f "tokens=*" %%s in ('dir /b *.sql ^| sort') do echo "%%s"
In my case it was SQLCMD, with the scripts in a subdirectory, like so:
SET "machineName=localhost"
for /f "tokens=*" %%f in ('dir /b PostDeployScripts\*.sql ^| sort') do sqlcmd -E -S%machineName% -i"PostDeployScripts\%%f"