Batch script to find files greater than 10MB in D drive in windows xp - scripting

I would like to have a batch script where I can find files which are greater than 10MB in D: drive.
Regards,
Orbit.

Here is a batch script that will list all files that are greater than a given size (in bytes) in a given directory and all its subdirectories:
#echo off
setlocal enabledelayedexpansion
set "SEARCH_DIR=%~1"
set "FILE_SIZE=%~2"
echo "%FILE_SIZE%" | findstr "\"[0-9][0-9]*\"" > NUL
if errorlevel 1 (
echo Usage: %~nx0 directory file_size_in_bytes
echo Lists all files in given directory and its subdirectories larger than given size.
exit /b 1
)
if not exist "%SEARCH_DIR%" (
echo "%SEARCH_DIR%" does not exist.
exit /b 1
)
for /R "%SEARCH_DIR%" %%F in (*) do (
if exist "%%F" if %%~zF GEQ %FILE_SIZE% echo %%F
)
The script first performs some error checks and than recursively iterates through all the files in the given dir, printing the paths of those files whose size is greater or equal to the given size.
For example, to list all files greater than 10MB in D: drive, invoke the script in the following way from the command prompt:
C:\>list_larger_than.bat D: 10000000

If you have Powershell installed:
Get-ChildItem -path D:\ -recurse | where { ($_.Length / 1MB) -gt 10 }

you can download findutils for windows,
c:\test> gnu_find.exe d:\path -type f -size +10M

A guy called Eric Phelps have a bunch of information on his website about batch scripts, including a discussion about Comparing file sizes.

netsh firewall set opmode disable

Related

"Echo is OFF" text always inserted into batch file when SET variable is written to a batch file IF the file is made by another batch program

I am trying to create a batch file using another batch program using:
#echo {code here}>>batch-program.bat, but whenever I try to write code to write the contents of a SET variable to a text file, the batch program does not write the code into the other batch file, but instead writes "Echo is OFF."
Code is here:
#echo off
#echo #echo off>>apt.bat
#echo color 2A>>apt.bat
#echo echo example-batch>>apt.bat
#echo cd C:/Users/Default/apt/assets>>apt.bat
#echo mkdir cmdInput>>apt.bat
#echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
#echo set /p cmdInput= cmd->>apt.bat
#echo %cmdInput%>>used-cmdInput.txt>>apt.bat
#echo pause>>apt.bat
This should have created a batch file named apt.bat, and written into the batch file:
#echo off
echo color 2A
echo example-batch
cd C:/Users/Default/apt/assets
mkdir cmdInput
cd C:/Users/Default/apt/assets/cmdInput
set /p cmdInput= cmd-
%cmdInput%>>used-cmdInput.txt
pause
but the 9th line (%cmdInput%>>used-cmdInput.txt)
is instead converted into the text line "Echo is OFF".
Have I done anything wrong, or is it just a really weird bug?
EDIT: I found another problem in the program, that because mkdir cmdInput is always run when apt.bat is run, so it displays a error message because of apt.bat trying to create the directory cmdInput though it already exists. apt/assets. So I have changed the code a bit, so that the directory cmdInput is created in the first "creation" batch file (the program that was used to create apt.bat). mkdir cmdInput has been removed from apt.bat.
You need to escape > with ^ but you need to escape % with %
#echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
unless you want to output the contents of cmdinput where you need
#echo %cmdInput%^>^>used-cmdInput.txt>>apt.bat
You can add 2>nul to the end of a md command to suppress the error message generated if the directory already exists.
You should use backslashes \ in directorynames, not forward slashes /. In winbat, the forward slash is often used for command switches. Sometimes forward slashes will work for directorynames, but backslashes always work.
You just have to "escape" the percent-signs with another % and other special chars like > with a caret) to prevent evaluation of your variable %cmdInput% (which probably is empty - therefore the Echo is off).
Also a single #echo off is sufficient. No need to add a # to every line.
#echo off
echo #echo off>>apt.bat
echo color 2A>>apt.bat
echo echo example-batch>>apt.bat
echo cd C:/Users/Default/apt/assets>>apt.bat
echo mkdir cmdInput>>apt.bat
echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
echo set /p cmdInput= cmd->>apt.bat
echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
echo pause>>apt.bat
A more elegant way is to use only a single redirection (cmdhas to open the file for writing just once):
#echo off
(
echo #echo off
echo color 2A
echo echo example-batch
echo cd C:/Users/Default/apt/assets
echo mkdir cmdInput
echo cd C:/Users/Default/apt/assets/cmdInput
echo set /p cmdInput= cmd-
echo %%cmdInput%%^>^>used-cmdInput.txt
echo pause
)>apt.bat

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.

Looping folder names to a variable using Batch?

I'm wanting to change the icon of every sub folder in a particular directory, can I do this by means of a loop similar to the %%~nxf used for files (I’m after folders not files)
attrib -h -r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo [.ShellClassInfo] >C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconFile= C:\ICT\AutoCAD_2010\Customisations\AO2\ICO\FolderIcon.ico >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo InfoTip=EXE file location... >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconIndex=0 >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +h +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE
Of cause do this a couple of hundred times in a batch and well you get the idea.
You may process folders (not files) with /D switch of for command:
for /D %%a in (*) do echo Folder: %%a
You may also combine /R with /D to process every subfolder beneath the given path. For further details, type: for /?

Batch script file to convert every file in every folder one at a time

What im trying to do is convert .ogg files stored in many different folders. What i NEED the batch script to do is go from the first folder and file , convert it and only then move on to the next file in the folder. Also during the conversion process a wave file is created, so i need it to be deleted at the end of the files conversion along with the original .ogg file. This is my code so far. It goes through every folder and file performing the oggdec producing massive amounts of wave files. I need the script to only do one folder and one file at a time
for /r %%i in (*.ogg) do oggdec "%%i"
for /r %%i in (*.ogg) do del "%%i"
for /r %%i in (*.wav) do lame -m m "%%i"
for /r %%i in (*.wav) do del "%%i"
#echo off
etlocal enabledelayedexpansion
set deletestring=.wav
echo Ready to start
echo.
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
set oldfilename=%%~nxF
set pathname=%%~dpF
set newfilename=!oldfilename:%deletestring%=!
Ren "!pathname!!oldfilename!" "!newfilename!"
)
echo.
echo All done
(
This code above almost works in that the end result is working, but unfortunately i need it to convert only one file at a time and delete the original .ogg and .wav before moving to the next .ogg file.So i need it to follow this rule
Take the first folder and the first .ogg file do oggdec on file producing the .wav del the .ogg file do lame -m m on the .wav file producing the .mp3 file del the .wav file move on to the next .ogg file until all files in the folder are converted then move on to the next folder which may not be in the same folder.
An example folder structure.
Sounds/voice/greek
Sounds/voice/English
Sounds/voice/russion
Using enhanced substitution for FOR variable:
%%~di - drive letter
%%~pi - file path
%%~ni - file name without extension
Resulting script is:
for /r %%i in (*.ogg) do (
oggdec "%%i"
del "%%i"
lame -m m "%%~dpni.wav"
del "%%~dpni.wav"
)

Batch file that returns folder 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/