Batch: Copy files which last modification were 15 minutes before - windows

I have a folder where files are automatically created and I need every 5 minutes to copy the new files (that is, the files whose last modification was in the last 5 minutes).
:loop
for %a in (C:\test\*) do (
set FileDate=%~ta
)
timeout /t 300
goto loop
That's the way I have found to get date of file but I don't know how to compare and get the current date less 5 minutes.
(The copy command is not necessary, because is via SSH and this problem is resolved).

In batch date time calculations are a very tedious task.
I suggest to use PowerShell (at least as a tool)
To get the files created in the current directory in the last 5 minutes.
This powershell command will output a dir-like listing:
Get-ChildItem -Path 'X:\path'|Where-Object {((Get-Date) - $_.LastWriteTime).TotalMinutes -le 5}
To restrict this to only the FullName you can append the pipe
| Select-Object -ExpandProperty FullName
or simply enclose the command in parentheses and append (...).FullName
(Get-ChildItem -Path 'X:\path'|Where-Object {((Get-Date) - $_.LastWriteTime).TotalMinutes -le 5}).FullName
Wrapped in a batch
:: Q:\Test\2018\11\08\SO_53206386.cmd
#Echo off
for /f "usebackq delims=" %%A in (`
powershell -Nop -C "(Get-ChildItem -Path 'X:\path' -File |Where-Object {((Get-Date) - $_.LastWriteTime).TotalMinutes -le 15}).FullName"
`) Do Echo %%A
Sample output of this batch (listing itself)
> SO_53206386.cmd
Q:\Test\2018\11\08\SO_53206386.cmd
The -File parameter requires PowerShell v3+ but can be replaced with another piped command
| Where-Object {!($PSISContainer)}
filtering out folders. (The opposite is -Directory or no ! for not)
#Echo off
for /f "usebackq delims=" %%A in (`
powershell -Nop -C "(Get-ChildItem -Path 'X:\path' | Where-Object {!($PSISContainer)}| Where-Object {((Get-Date) - $_.LastWriteTime).TotalMinutes -le 15}).FullName"
`) Do Echo %%A

Here is a completely different solution resulting in most likely the same behavior with the advantage that the last modification date of a file does not really matter. So if a file is copied into the observed folder, it is also processed even if its last modification time is not within the last X minutes. It uses the archive file attribute set by Windows automatically each time a file is created in a folder or the file is modified by a process.
#echo off
set "Folder=C:\test"
:loop
for /F "eol=| delims=" %%I in ('dir "%Folder%\*" /AA-D-H /B /ON 2^>nul') do (
%SystemRoot%\System32\attrib.exe -a "%Folder%\%%I"
echo Copy the file "%Folder%\%%I"
)
%SystemRoot%\System32\timeout.exe /T 300
goto loop
The command FOR executes the following command line in a separate command process started with cmd.exe /C in background.
dir "C:\test\*" /AA-D-H /B /ON 2>nul
The command DIR outputs
in bare format only file name and file extension because of /B
only non-hidden files with archive attribute set because of /AA-D-H
ordered by file name because of /ON (not really needed)
found in directory C:\test matching wildcard pattern *.
The error message output by DIR on not finding any directory entry matching these requirements is suppressed by redirecting it from handle STDERR to device NUL.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
This output of DIR in separate command process is captured by FOR and processed line by line.
Empty lines are always skipped by FOR which do not occur here.
Lines (file names) starting with a semicolon are also skipped by default by FOR. This behavior is disabled by changing with eol=| the end of line character from default ; to vertical bar which no file name can have anywhere.
FOR splits up by default the line in substrings (tokens) using space/tab as delimiters and assigns just first space/tab delimited string to specified loop variable I. This behavior is not wanted here as file names can contain one or more spaces. For that reason delims= is used to specify an empty list of delimiters which disables the line splitting behavior.
So assigned to loop variable I is the file name with file extension as output by DIR without path.
The command ATTRIB is used to remove the archive attribute from current file for next iteration of the FOR loop. Then the file can be copied to a different location or processed otherwise not modifying its content.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
attrib /?
dir /?
echo /?
for /?
goto /?
timeout /?

Related

How to list the names of all the files and directories in a folder using for loop in a batch file

I want to list all the files and directories inside a directory using a for loop in a batch script. How can I do it?
I used below but it didn't work :
for /r %%I in (".") do ( ls -ltr '%%I') ## Listing only filenames and not directories name
Any help is appreciable.
Thanks!
If you just want a list of dirs and files, recursively, what about:
dir /b/s "."
If you want to do something special with each of the stream item, using a for loop, you could do something like:
for /f "tokens=* delims=" %%i in ('dir /b/s "."') do ( echo "%%i" )
There I used echo for echoing, but you can put whatever you need.
"to list all the files and directories inside a directory using a for loop in a batch script." you should use the DIR command.
If you open a Command Prompt window, type dir /? and press the ENTER key you should see its usage information.
One important thing to note is the /A option. What is not mentioned specifically is that using it alone, (without additional parameters D, R, H, A, S, I, L or O), enables all attributes.
Therefore to list all items in the current directory recursively in bare format you'd use:
DIR /A /B /S
or
DIR . /A /B /S
If you wanted to list them in a specific location relative to the current directory, you'd use:
DIR "Location" /A /B /S
or:
DIR ".\Location" /A /B /S
And For a specific absolute path:
DIR "L:\ocation" /A /B /S
And if you wanted it to be in the same location as the batch file itself, you can use the special variable for the current script %0:
DIR "%~dp0." /A /B /S
To perform that command within a For loop, you should first open a Command Prompt window, type for /? and press the ENTER key, to read its usage information.
You should note that you are running a command, and should therefore use a FOR /F loop, i.e.
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
But should also note that:
To use the FOR command in a batch program, specify %%variable instead of %variable.
So:
FOR /F ["options"] %%variable IN ('command') DO command [command-parameters]
As you have your command already, the options now become important. The first you need to understand is eol which whilst it seems to mean End Of Line, is specific to only one end, the beginning! What this does it does not pass any result of 'command' to the DO if it begins with a single specific character. The defualt for eol is the semicolon ;, (probably because historically it was a common line comment marker in many files). Generally, a file or directory name could include, and begin with a semicolon, so in order to include all files, you would specify a character which cannot be included in a filename, for me the simplest is ?, although I've seen many examples using |. However, when you perform a recursive DIR command, every returned line is a fully qualified path, none of which can begin with a semicolon, so you can for this task ignore eol. You clearly want everything returned, so do not require skip any lines returned. tokens and delimiters, are adjusted according to what you want to do with the results, in this case, you want the entire content of each line returned by your 'command' with no splitting on specific characters. You should note that tokens by default is 1 and delims by default is both the space and a horizontal tab characters. You should stipulate therefore that you do not want any delimiters, so that the first token is everything returned on each line of 'command'. You rarely require the usebackq option, so for the purposes of this answer, and your task, just ignore it.
Now put it all together:
FOR /F "delims=" %%G IN ('DIR "Location" /A /B /S') DO command
Finally you can use your wanted DO command with each result from your parenthesized DIR command. That result will be held within your variable %%G.
For the purposes of just viewing each result, we'll use the ECHO command, (you would just replace that with your chosen command). Please note that as each result of the DIR command is a file or directory name string, you should generally doublequote it.
allObjects.cmd
FOR /F "delims=" %%G IN ('DIR "Location" /A /B /S') DO ECHO "%%G"
Please remember to replace "Location" as needed, before running the Windows Command Script
Create two loops, one for files
for /r %%i in (*.*) do <something>
and one for directories
for /r %%i in (.) do <something>
and use the same command after do
But, since you have Cygwin installed anyway, why not use that power and do
find . | xargs -L1 ls -ltr
where find . finds all files and directories, | xargs passes the output to xargs which -L1 splits the output after each line and passes each line to ls -ltr.

Move files to a subdirectory

Using some great help (Create subdirectory under each directory containing a file) I added a \pre subdirectory in any directory that contained a .jpg photo.
I want to move any .jpg files from their current directory into the \pre subdirectory. The script I tried is:
FOR /R c:\temp %G IN (*.JPG) DO pushd %~dpG && if exist *.jpg move *.jpg pre\ && popd
The script moved the .jpg files. The problem is that the script moves the files then follows to the \pre directory and tries to do the move again.
The pre directories have been created using the script linked to in the first paragraph.
For example, directory A\B\C was processed to give A\B\C\pre. This script scans A/B/C and moves the .jpgs to \A\B\C\pre. It then follows the directory tree into A\B\C\pre and tries to move the .jpg files again
What about the following script:
#echo off
rem // Enumerate the directory tree:
for /D /R "C:\TEMP" %%G in ("*") do (
rem // Check whether current directory is not named `pre`:
if /I not "%%~nxG" == "pre" (
rem // Check whether there are files:
if exist "%%~G\*.jpg" (
rem // Create sub-directory called `pre`:
md "%%~G\pre" 2> nul
rem // Move files into the sub-directory:
move "%%~G\*.jpg" "%%~G\pre"
)
)
)
Or directly in command prompt:
#for /D /R "C:\TEMP" %G in ("*") do #if /I not "%~nxG" == "pre" if exist "%~G\*.jpg" md "%~G\pre" 2> nul & move "%~G\*.jpg" "%~G\pre"
The problem of doing the operation in the new "pre" directory is solved by getting the list of directories before enumerating over them.
It might be nice to have a one-liner command to do it, but this is getting a bit more complex than what is easy to do in a one-liner.
Here is a PowerShell script that will do it. If you are on a supported Windows platform, PowerShell will be available. This script requires PowerShell 5.1 or higher. If you cannot get to the current PowerShell, the code can be changed to make it work. When you are satisfied that the correct files will be moved, remove the -WhatIf from the mkdir and Move-Item commands.
=== Move-JpegToPre.ps1
$dirs = Get-ChildItem -Directory -Path "C:/src/t"
$extent = 'jpg'
foreach ($dir in $dirs) {
if (Test-Path -Path "$($dir.FullName)/*.$($extent)") {
if (-not (Test-Path -Path "$($dir.FullName)/pre")) {
mkdir "$($dir.FullName)/pre"
}
Move-Item -Path "$($dir.FullName)/*.$($extent)" -Destination "$($dir.FullName)/pre" -WhatIf
}
}
In a cmd.exe shell it can be invoked by:
powershell -NoLogo -NoProfile -File Move-JpegToPre.ps1
I suggest to use this batch file for the file moving task.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "delims=" %%I in ('dir "C:\Temp\*.jpg" /A-D /B /S 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /C:"\\pre\\" /C:"\\post\\"') do (
if not exist "%%~dpIpre\" md "%%~dpIpre"
move /Y "%%I" "%%~dpIpre\"
)
endlocal
FOR starts in background one more command process with %ComSpec% /c and the specified command line appended as additional arguments. So there is executed with Windows installed into C:\Windows in background:
C:\Windows\System32\cmd.exe /c dir "C:\Temp\*.jpg" /A-D /B /S 2>nul | %SystemRoot%\System32\findstr.exe /I /L /V /C:"\\pre\\" /C:"\\post\\"
DIR executed by the background command process searches
in directory C:\Temp and all its subdirectories because of option /S
just for files because of option /A-D (attribute not directory)
matching the wildcard pattern *.jpg
and outputs in bare format because of option /B
just the file names with full path because of option /S.
This list of file names is redirected from STDOUT (standard output) of background command process with redirection operator | to STDIN (standard input) of FINDSTR which searches
for lines containing case-insensitive because of option /I
either the literal string \pre\ or the literal string \post\
and outputs the inverted result because of option /V, i.e. the lines not containing \pre\ or \post\.
So FINDSTR is used here as filter to get from the list of *.jpg file names output by DIR with full path just those file names which do not have \pre\ or \post\ in their path to exclude the JPEG files which are already in one of the two subdirectories with name pre or post.
2>nul after the arguments of command DIR suppresses the error message output by DIR if it cannot find any *.jpg file name in C:\Temp and its subdirectories by redirecting the error message written to STDERR (standard error) to the device NUL.
Read the Microsoft article about Using command redirection operators for an explanation of 2>nul. The redirection operators > and | must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with findstr with using a separate command process started in background.
FOR with option /F captures everything written to handle STDOUT of started command process by FINDSTR and processes this output line by line after started cmd.exe terminated itself. It is very important here to process a captured list of file names and do not iterate over one file name after the other returned by the file system because of the files matched by wildcard pattern *.jpg are moved during each loop iteration within the directory structure. So the directory entries matching *.jpg changes on each loop iteration and therefore it is required that a list of file names is processed loaded into memory before moving the files.
FOR with option /F ignores empty lines which do not occur here.
FOR with option /F would split up each line into substrings using normal space and horizontal tab as string delimiters and would assign just first space/tab delimited string to specified loop variable I if not starting with default end of line character ; in which case the line would be completely ignored like an empty line.
A file name with full path cannot start with ;. So default eol=; must not be modified here. But the line splitting behavior is counterproductive because of a full qualified file name can contain one or more spaces. For that reason the option delims= is used to define an empty list of string delimiters which disables completely the line splitting behavior.
Therefore each full file name output by DIR not containing \pre\ or \post\ in path as filtered out by FINDSTR is assigned to loop variable I one after the other.
It is checked next if there is for the current JPEG file a subfolder pre and this folder is created if not already existing. Then the current JPEG file is moved into the subdirectory pre with overwriting the file in pre with exactly the same file name.
So this batch file can be executed multiple times on C:\Temp as it ignores all *.jpg files in all subdirectories pre and post
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
findstr /?
for /?
if /?
md /?
move /?
setlocal /?

How to get just the last two lines of standard output of command DIR?

The command I am executing is the following:
dir >> dir.txt
I would be interested in redirecting only certain lines to the txt, for example the last two lines. How can I do that? It occurs to me with findstr, but I don't know how.
A simple findstr match will isolate those two lines based upon them being the only two lines beginning with two spaces:
Dir | FindStr /BC:" " >> "dir.txt"
Assuming that you are under Windows, you can use the Win32 port of the Unix tail command from https://sourceforge.net/projects/tailforwin32/ and then issue the piped command:
dir | tail --lines=2
This shows the last 2 lines
Hope this helps
This can easily be done with PowerShell that you already have on your machine.
powershell -NoLogo -NoProfile -Command "& cmd.exe /C dir | Select-Object -Last 2 | Out-File -FilePath '.\dir.txt' -Encoding ascii -Append"
Alternatively...
powershell -NoLogo -NoProfile -Command "& cmd.exe /C dir | Select-Object -Last 2" >>dir.txt
Here is a simple batch file to get output the last two lines of standard output of command dir with language dependent information about
number of files in directory,
total number of bytes of the files in directory,
number of subdirectories in directory,
free space on partition in bytes.
dir excludes by default directories and files with hidden attribute set because of using implicit /A-H if dir option /A is not used at all.
Here is the batch file to get displayed the last two lines of output of dir executed without any parameters on current directory which of course can be different to directory containing the batch file.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SummaryFiles="
set "SummaryFolders="
for /F delims^=^ eol^= %%I in ('dir') do (
set "SummaryFiles=!SummaryFolders!"
set "SummaryFolders=%%I"
)
if defined SummaryFiles echo !SummaryFiles!
if defined SummaryFolders echo !SummaryFolders!
pause
endlocal
The output done by the two echo can be also redirected into a text file using for example
( if defined SummaryFiles echo !SummaryFiles!
if defined SummaryFolders echo !SummaryFolders!
) >DirectorySummary.txt
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
if /?
pause /?
set /?
setlocal /?

Windows cmd cd into newest directory

I have a deployment directory that contains subdirectories, one for each deployment. I'm trying to write a batch script that, among other things, performs a cd into the newest one of these directories.
I know how to do this in bash (has already been ansered here as well), but I don't know how to accomplish the same thing in Windows cmd. Can anyone help me?
In a batch file following lines can be used to changed to the subdirectory with newest modification date:
#echo off
for /F "eol=| delims=" %%I in ('dir * /AD /B /O-D 2^>nul') do cd "%%I" & goto DoneCD
echo No subdirectory found in: "%CD%"
:DoneCD
The command FOR with option /F starts a new command process with %ComSpec% /c and the command line specified between ' as further arguments in background. So executed by FOR is with usual Windows installation path:
C:\Windows\System32\cmd.exe /c dir * /AD /B /O-D 2>nul
DIR executed by background command process searches with the specified arguments
in current directory
for directories because of option /AD (attribute directory)
matching the wildcard pattern * (all)
and outputs
in bare format because of option /B just the directory names without path never enclosed in "
ordered reverse by last modification date because of option /O-D and not using option /TC (creation date) or /TA (last access date) which means first the newest modified directory and last the oldest modified directory.
The output by DIR is written to handle STDOUT of the started background command process.
2>nul redirects the error message output by DIR on not finding any directory in current directory from handle STDERR to device NUL to suppress this error message.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
FOR captures everything written by DIR to handle STDOUT of started command process and processes this output line by line after started cmd.exe terminated itself.
FOR ignores empty lines which do not occur here because of DIR outputs the list of directory names without empty lines because of using /B.
FOR would split up by default a line into substrings (tokens) using normal space and horizontal tab character as delimiters. After this substring splitting is done FOR would by default check if the first substring starts with default end of line character ; in which case the line would be ignored like an empty line. Otherwise FOR would assign first space/tab delimited string to the specified loop variable I and would execute the command line with CD and GOTO.
A directory name could be for example  ;Test Folder, i.e. a directory name starting with a space and a semicolon and containing one more space. Such a directory name would be split up to ;Test (without space at beginning) and Folder and next ignored by FOR because of ;Test starts with a semicolon.
For that reason the end of line character is redefined from default semicolon to a vertical bar with eol=| which is a character no file or folder name can contain according to Microsoft documentation about Naming Files, Paths, and Namespaces. And line splitting behavior is disabled with delims= at end of options argument string after for /F which defines an empty list of delimiters. So the directory name as output by DIR is assigned to loop variable I without any modification even on being a very unusual name for a directory.
FOR executes command CD which changes current directory to the last modified subdirectory of the current directory and next command GOTO is executed to continue the processing of the batch file on the line below the label line :DoneCD. So the FOR loop execution is broken already after processing first directory name with command GOTO.
It is of course possible to use other commands after the FOR command line and the label line :DoneCD than just the ECHO line reporting that no subdirectory was found in current directory as shown by referencing dynamic environment variable CD like a command line to exit batch processing on this unusual use case or error condition case.
This FOR command line with the command GOTO to exit FOR loop after CD cannot be used in a Windows command prompt window. A solution for Windows command prompt window would be:
set "DoneCD=" & (#for /F "eol=| delims=" %I in ('dir * /AD /B /O-D 2^>nul') do #if not defined DoneCD cd "%I" & set "DoneCD=1") & set "DoneCD="
In a batch file this single line with multiple commands would be written as
#set "DoneCD=" & (#for /F "eol=| delims=" %%I in ('dir * /AD /B /O-D 2^>nul') do #if not defined DoneCD cd "%%I" & set "DoneCD=1") & set "DoneCD="
or better readable in its multi-line version with an additional echo as
#echo off
set "DoneCD="
for /F "eol=| delims=" %%I in ('dir * /AD /B /O-D 2^>nul') do (
if not defined DoneCD (
cd "%%I"
set "DoneCD=1"
)
)
if not defined DoneCD echo No subdirectory found in: "%CD%"
set "DoneCD="
First the environment variable DoneCD is deleted if it is defined by chance.
Next FOR runs cmd.exe with DIR as described above and processes the first output directory with newest modification date. The IF condition is true on newest directory as the environment variable was definitely undefined before execution of FOR. So command CD is executed to change the current directory to newest subdirectory. Then the environment variable DoneCD is defined with value 1. Any other value would be also possible like on using set "DoneCD=%%I". Important here is that for the other subdirectories output by DIR the environment variable DoneCD is now defined and so the IF condition is always false. So there is no attempt made to change in current subdirectory of initial current directory into a subdirectory not existing here or existing by chance also in the subdirectory.
Finally the environment variable DoneCD is deleted again if defined at all during execution of FOR.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
echo /?
for /?
goto /?
if /?
set /? ... explaining on last help page dynamic environment variable CD.

Delete files by Specifying directory

I am using the following batch script while deletes files below a certain size.
#echo off
setlocal
:: Size is in bytes
set "min.size=100000"
for /f "usebackq delims=;" %%A in (`dir /b /A:-D *.*`) do If %%~zA LSS %min.size% del "%%A"
This works if I put the batch file inside the folder but it deletes the batch file also.
However how do I keep the batch file at a different position and specify the directory path explicitly?
The easiest solution is making the directory on which to delete files like C:\Temp\Test temporarily the active directory.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Size is in bytes
set "min.size=100000"
set "FullBatchFileName=%~f0"
pushd "C:\Temp\Test"
rem Do nothing if the command line above fails unexpected.
if errorlevel 1 goto EndBatch
for /F "eol=| delims=" %%I in ('dir * /A-D-H /B /OS 2^>nul') do if not "%FullBatchFileName%" == "%%~fI" if %%~zI LSS %min.size% ( del "%%I" ) else goto DeletionDone
:DeletionDone
popd
:EndBatch
endlocal
The DIR command line is executed by FOR in a separate command process started with cmd.exe /C in background and FOR captures all lines output by DIR to handle STDOUT. An error message output by DIR to handle STDERR on finding not any non-hidden file in current directory is redirected with 2>nul to device NUL to suppress it.
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.
The DIR option /OS results in getting the list of non-hidden files output by DIR ordered by size with smallest first and largest last.
FOR would skip lines starting with a semicolon which means it would skip files which file name starts with ; which is in general possible. This is avoided by specifying with eol=| the vertical bar as end of line character which no file name can contain.
FOR would split up the lines into substring using normal space and horizontal tab as delimiter and would assign only first substring to loop variable I. File names can contain one or more spaces. Therefore delims= is used to define an empty list of delimiters which disable the line splitting behavior completely and get assigned to loop variable I the entire file name.
The IF condition if not "%FullBatchFileName%" == "%%~fI" compares case-sensitive the full qualified name of the batch file (drive + path + name + extension) with full qualified name of current file. This condition is only true if the current file is not the currently running batch file.
The next IF condition if %%~zI LSS %min.size% compares the file size of current file converted to a 32-bit signed integer with the specified file size also converted to a 32-bit signed integer. This file size comparison fails on files with 2 GiB or more as such a large file exceeds the maximum positive 32-bit signed integer value 2147483647.
The FOR loop is exited with goto DeletionDone on first line having a file size equal or greater the specified minimum size because of all further files output by DIR have definitely a file size equal or greater than the specified minimum size because of being output ordered by size from smallest to largest.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~f0 ... full qualified file name of argument 0 - the currently executed batch file.
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
popd /?
pushd /?
rem /?
set /?
setlocal /?
If you wanted to push ahead into PowerShell, the following script might work. When you are confident that the correct files will be deleted, remove the -WhatIf from the Remove-Item cmdlet.
$deldir = 'C:\Temp'
$minsize = 100000
$precious = 'Remove-UnderSize.ps1', 'Remove-UnderSize.bat'
Get-ChildItem -File -Path $deldir |
ForEach-Object {
if (-not ($precious -contains $_.Name)) {
if ($_.Length -lt $minsize) {
Remove-Item -Path $_.FullName -WhatIf
}
}
}
This script can be called from a cmd.exe bat file script.
powershell -NoProfile -File .\Remove-UnderSize.ps1

Resources