Delete files by Specifying directory - windows

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

Related

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 /?

Check if there are any folders in a directory

CMD. How do I check if a directory contains a folder/s (name is not specified)? Other files are ignored.
If this was in the case of any .txt file, it would kind of look like this :
if exist * .txt
How do I do it with "any" folder?
There are multiple solutions to check if a directory contains subdirectories.
In all solutions below the folder for temporary files referenced with %TEMP% is used as an example.
Solution 1 using FOR /D:
#echo off
set "FolderCount=0"
for /D %%I in ("%TEMP%\*") do set /A FolderCount+=1
if %FolderCount% == 0 (
echo Temporary files folder has no non-hidden subfolder.
) else if %FolderCount% == 1 (
echo Temporary files folder has one non-hidden subfolder.
) else (
echo Temporary files folder has %FolderCount% non-hidden subfolders.
)
pause
The problem with this solution is that FOR with option /D to search for directories matching the wildcard pattern * in specified directory for temporary files ignores the directories with hidden attribute set. For that reason the command SET with the arithmetic expression to increment the value of environment variable FolderCount by one on each each directory is not executed for a directory with hidden attribute set.
The short version of this solution without counting the folders:
#echo off
for /D %%I in ("%TEMP%\*") do goto HasFolders
echo Temporary files folder has no non-hidden subfolder.
goto EndBatch
:HasFolders
echo Temporary files folder has non-hidden subfolders.
:EndBatch
pause
The loop is exited with command GOTO on FOR has assigned first name of a non-hidden directory to the loop variable.
Solution 2 using FOR /F and DIR:
#echo off
set "FolderCount=0"
for /F "eol=| delims=" %%I in ('dir "%TEMP%" /AD /B 2^>nul') do set /A FolderCount+=1
if %FolderCount% == 0 (
echo Temporary files folder has no subfolder.
) else if %FolderCount% == 1 (
echo Temporary files folder has one subfolder.
) else (
echo Temporary files folder has %FolderCount% subfolders.
)
pause
FOR with option /F and a set enclosed in ' results in starting in background one more command process with %ComSpec% /c and the command line within ' appended as additional arguments. So executed is with Windows installed to C:\Windows:
C:\Windows\System32\cmd.exe /c dir "C:\Users\UserName\AppData\Local\Temp" /AD /B 2>nul
DIR executed by background command process searches
in specified directory for temporary files
just for directories because of option /AD (attribute directory)
with including also directories with hidden attribute set because of option /AD overrides the default /A-H (all attributes except attribute hidden)
and outputs them in bare format because of option /B which results in ignoring the standard directories . (current directory) and .. (parent directory) and printing just the directory names without path.
The output of DIR is written to handle STDOUT (standard output) of the started background command process. There is nothing output if the there is no subdirectory in the specified directory.
There is an error message output to handle STDERR (standard error) of background command process if the specified directory does not exist at all. This error message would be redirected by the command process executing the batch file to own STDERR handle and would be output in console window. For that reason 2>nul is appended to the DIR command line to suppress the error message in background command process 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.
FOR with option /F captures the output written to handle STDOUT of started background command process and processes the output line by line after started cmd.exe terminated itself after finishing execution of internal command DIR.
Empty lines are ignored by default by FOR which do not occur here.
FOR would split up the line by default into substrings using normal space and horizontal tab character as string delimiters and would assign just first space/tab separated string to specified loop variable I. This line splitting behavior is unnecessary here and is disabled for that reason by using option delims= which defines an empty list of string delimiters.
FOR would ignore also lines on which first substring after splitting a line up into substrings starts with default end of line character ;. The line splitting behavior is already disabled, but the name of directory can start unusually with a semicolon. Such a directory name would be ignored by FOR. Therefore the option eol=| defines the vertical bar as end of line character which no directory name can have and so no directory is ignored by FOR. See also the Microsoft documentation page Naming Files, Paths, and Namespaces.
The directory name assigned to loop variable I is not really used because of FOR executes for each directory name just command SET with an arithmetic expression to increment the value of the environment variable FolderCount by one.
The environment variable FolderCount contains the number of subfolders in specified directory independent on hidden attribute.
The short version of this solution without counting the folders:
#echo off
for /F "eol=| delims=" %%I in ('dir "%TEMP%" /AD /B 2^>nul') do goto HasFolders
echo Temporary files folder has no subfolder.
goto EndBatch
:HasFolders
echo Temporary files folder has subfolders.
:EndBatch
pause
The loop is exited with command GOTO on FOR has assigned first name of a directory to the loop variable.
Solution 3 using DIR and FINDSTR:
#echo off
dir "%TEMP%" /AD /B 2>nul | %SystemRoot%\System32\findstr.exe /R "^." >nul
if errorlevel 1 (
echo Temporary files folder has no subfolder.
) else (
echo Temporary files folder has subfolders.
)
pause
The output of DIR as explained above executed by cmd.exe processing the batch file is redirected from STDOUT of command process to STDIN (standard input) of FINDSTR which searches for lines having at least one character. The found lines are all lines with a directory name output by DIR. This search result is of no real interest and therefore redirected to device NUL to suppress it.
FINDSTR exits with 1 if no string could be found and with 0 on having at least one string found. The FINDSTR exit code is assigned by Windows command processor to ERRORLEVEL which is evaluated with the IF condition.
The IF condition is true if exit value of FINDSTR assigned to ERRORLEVEL is greater or equal 1 which is the case on no directory found by DIR and so FINDSTR failed to find any line with at least one character.
This solution could be also written as one command line:
dir "%TEMP%" /AD /B 2>nul | %SystemRoot%\System32\findstr.exe /R "^." >nul && echo Temporary files folder has subfolders.|| echo Temporary files folder has no subfolder.
See single line with multiple commands using Windows batch file for an explanation of the operators && and || used here to evaluate the exit code of FINDSTR.
Additional hints:
It would be good to first check if the directory exists at all before checking if it contains any subdirectories. This can be done in all three solutions above by using first after #echo off
if not exist "%TEMP%\" (
echo Folder "%TEMP%" does not exist.
pause
exit /B
)
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.
cmd /?
dir /?
echo /?
exit /?
findstr /?
for /?
goto /?
if /?
pause /?
set /?
DIR "your directory" /ad, for example DIR C:\Users /ad brings out all folders that are inside C:\Users
Displays a list of files and subdirectories in a directory.
DIR [ drive:][path][filename] [/A[[:]attributes]] [/B] [/C] [/D] [/L] [/N]
[/O[[:]sortorder]] [/P] [/Q] [/R] [/S] [/T[[:]timefield]] [/W] [/X] [/4]
[drive:][path][filename]
Specifies drive, directory, and/or files to list.
/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points
If you just want to use the cmd.exe shell console to see if there are any directories:
DIR /A:D
If you want to check for it in a .bat file script:
SET "HASDIR=false"
FOR /F "eol=| delims=" %%A IN ('DIR /B /A:D') DO (SET "HASDIR=true")
IF /I "%HASDIR%" == "true" (
REM Do things about the directories.
)
ECHO HASDIR is %HASDIR%

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.

Batch: Copy files which last modification were 15 minutes before

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 /?

Batch file for loop executes on one machine only

I have written the following .bat file, and it runs perfectly on my Windows 2000 machine, but will not run on my Windows 7 or Windows XP machines. Basically it just loops through the current directory and runs a checksum program which returns the checksum. The output of the program is saved to a text file and then formatted to remove the checksum of the output file.
#Echo Off
for /r %%f in (*.txt) do crc32sum.exe %%f >> all_checksums.txt
ren all_checksums.txt old.txt
findstr /v /e /c:"all_checksums.txt" old.txt > all_checksums.txt
del old.txt
When I run this file on my Win2k PC with a bunch of text files and the crc32sum.exe in a folder, it outputs the file. On other machines it outputs a blank file. I turned Echo on and kept only the for loop line and found that the output from executing the crc32sum.exe is nothing. If you manually run the crc32sum.exe file it outputs the checksum no problem.
Any ideas as to how to fix this?
EDIT: Here is a link to the software: http://www.di-mgt.com.au/src/digsum-1.0.1.zip
EDIT2: New development, it seems that the file works if the path of the folder has no spaces in it i.e. C:\temp or C:\inetpub\ftproot or C:\users\admin\Desktop\temp. Does anyone know how I can make this work with paths that have spaces? %%~f doesnt work it says unexpected.
Try this modified batch code which worked on Windows XP SP3 x86:
#echo off
goto CheckOutput
rem Command DEL does not terminate with an exit code greater 0
rem if the deletion of a file failed. Therefore the output to
rem stderr must be evaluated to find out if deletion was
rem successful or (for a single file) the file existence is
rem checked once again. For details read on Stack Overflow
rem the answer http://stackoverflow.com/a/33403497/3074564
rem The deletion of the file was successful if file created
rem from output message has size 0 and therefore the temp
rem file can be deleted and calculation of the CRC32 sums
rem can be started.
:DeleteOutput
del /F "all_checksums.txt" >nul 2>"%TEMP%\DelErrorMessage.tmp"
for %%E in ("%TEMP%\DelErrorMessage.tmp") do set "FileSize=%%~zE"
if "%FileSize%" == "0" (
set "FileSize="
del "%TEMP%\DelErrorMessage.tmp"
goto CalcCRC32
)
set "FileSize="
echo %~nx0: Failed to delete file %CD%\all_checksums.txt
echo.
type "%TEMP%\DelErrorMessage.tmp"
del "%TEMP%\DelErrorMessage.tmp"
echo.
echo Is this file opened in an application?
echo.
set "Retry=N"
set /P "Retry=Retry (N/Y)? "
if /I "%Retry%" == "Y" (
set "Retry="
cls
goto CheckOutput
)
set "Retry="
goto :EOF
:CheckOutput
if exist "all_checksums.txt" goto DeleteOutput
:CalcCRC32
for /R %%F in (*.txt) do (
if /I not "%%F" == "%CD%\all_checksums.txt" (
crc32sum.exe "%%F" >>"all_checksums.txt"
)
)
The output file in current directory is deleted if already existing from a previous run. Extra code is added to verify if deletion was successful and informing the user about a failed deletion with giving the user the possibility to retry after closing the file in an application if that is the reason why deletion failed.
The FOR command searches because of option /R recursive in current directory and all its subdirectories for files with extension txt. The name of each found file with full path always without double quotes is hold in loop variable F for any text file found in current directory or any subdirectory.
The CRC32 sum is calculated by 32-bit console application crc32sum in current directory for all text files found with the exception of the output file all_checksums.txt in current directory. The output of this small application is redirected into file all_checksums.txt with appending the single output line to this file.
It is necessary to enclose the file name with path in double quotes because even with no *.txt file containing a space character or one of the special characters &()[]{}^=;!'+,`~ in its name, the path of the file could contain a space or one of those characters.
For the files
C:\Temp\test 1.txt
C:\Temp\test 2.txt
C:\Temp\test_3.txt
C:\Temp\TEST\123-9.txt
C:\Temp\TEST\abc.txt
C:\Temp\TEST\hello.txt
C:\Temp\TEST\hellon.txt
C:\Temp\Test x\test4.txt
C:\Temp\Test x\test5.txt
the file C:\Temp\all_checksums.txt contains after batch execution:
f44271ac *test 1.txt
624cbdf9 *test 2.txt
7ce469eb *test_3.txt
cbf43926 *123-9.txt
352441c2 *abc.txt
0d4a1185 *hello.txt
38e6c41a *hellon.txt
1b4289fa *test4.txt
f44271ac *test5.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.
cls /?
del /?
echo /?
for /?
goto /?
if /?
rem /?
set /?
type /?
One of the help pages output on running for /? informs about %~I, %~fI, %~dI, %~pI, %~nI, %~xI, %~sI, %~aI, %~tI, %~zI.
Using in a batch file f (in lower case) as loop variable and referencing it with %%~f is a syntax error as command processor expects next the loop variable. %%~ff would be right, but could be different to %%~fI (name of a file/folder with full path and extension without quotes) in comparison to %%~I (string without surrounding quotes).
It is not advisable to use (those) small letters as loop variable. It is better to use upper case letters or character # as loop variable. The loop variable and also those modifiers are case sensitive while nearly everything else in a batch file is case insensitive.

Resources