Find files that have an extension larger than a specific number - windows

I am attempting to create a script that will search through a directory for files whose extension are larger than a specific numerical value. The process I am monitoring creates files with numeric extensions when it runs. My end goal is to delete all files of a certain file name one the extension exceeds 800.
The process runs and creates a file as a result, mainly a log files. When it creates its 800th file the file name would be like "filename.800". The file name can vary but the extension will be anything from 000 - 999. I need to delete "filename.*" for the collection of files that reaches 800 (or other). There are other files of different names but have numeric extensions as well in the same directory. I'm trying to make this part of an automated cleanup process.
I could use some suggestions on how to get started detecting the files exist. My initial attempt below does not seem to heed the 'GTR', 'LSS' options.
forfiles /M *.* /C "%comspec% /c IF #EXT GTR 800 (ECHO #PATH)"
I don't mind entertaining other methods to address my task. Thanks in advance.

The #ext value includes enclosing quotes, so you need to include them in your IF comparison. But adding quotes will throw off the command line parser since the entire command is already enclosed in quotes. Use 0x22 to represent each internal quote.
forfiles /M * /C "cmd /c if #ext gtr 0x228000x22 echo #path"
However, FORFILES is very slow. It is much faster to use a simple FOR loop. The ~x modifier expands to the file extension (including the dot).
The following works on the command line.
for %f in (*) do #if %~xf gtr .800 echo %f
Double the percents if you want to use the command in a batch script.

If you want to delete the files precisely in the 800-999 range, then the easiest and fastest way is this:
for %%a in (8 9) do del filename.%%a??
Previous method could even be adjusted for other different ranges.

#ECHO OFF
SETLOCAL
::method1
FOR %%f IN (*.8* *.9*) DO IF %%~xf gtr .800 IF %%~xf leq .999 ECHO %%f
:: method2
ECHO ======= method 2 ============
FOR %%f IN (*.8* *.9*) DO (
ECHO %%~xf|FINDSTR /r ".[89][0-9][0-9]$" >NUL
IF NOT ERRORLEVEL 1 ECHO %%f
)
The first method may suit, but it also detects filenames such as xxx.8004 xxx.8a9.
The second method is slower, but I believe will reliably detect only filenames that end ".[8 or 9][2 digits]"
The filenames are only ECHOed to the console. Replace the ECHO with DEL to delete the files - after rigorous testing...

Related

Using FOR /R for recursive search only in a subset of folder hierarchy

I want to create a batch file able to apply some processing on each JPG file in a folder hierarchy. The following script file works very well for that case (here I only echo the name of each file, but this should be replaced by some more complex statements in the real application):
:VERSION 1
#echo off
set "basefolder=C:\Base"
for /r %basefolder% %%f in (*.jpg) do echo %%f
Actually, I don't want to explore all the folder hierarchy under %basefolder%, but only a given list of subfolders. This modified script is able to deal with that case :
:VERSION 2
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
pushd %basefolder%\%%~s"
for /r %%f in (*.jpg) do echo %%f
popd
)
Is there a solution to remove the pushd/popd pair of statements, to get something closer to the initial script. I thought that one of the following scripts would do the job:
:VERSION 3
#echo off
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
for /r %basefolder%\%%~s" %%f in (*.jpg) do echo %%f
)
or, using delayed expansion:
:VERSION 4
#echo off
setlocal enabledelayedexpansion
set "basefolder=C:\Base"
set "subfolders=A B C"
for %%s in (%subfolders%) do (
set "folder=%basefolder%\%%~s"
echo !folder!
for /r !folder! %%f in (*.jpg) do echo %%f
)
but none of them is working. When running the second one, the echo !folder! command in the external loop shows C:\Base\A, C:\Base\B and C:\Base\C as expected, but the inner loop doesn't echo any JPG file, so I guess that the recursive for /r command does not run correctly.
What am I doing wrong ?
Final edit after answers :
Thanks to #aschipfl who provided a link to the answer posted by #jeb on another question, quoted below:
The options of FOR, IF and REM are only parsed up to the special character phase. Or better the commands are detected in the special character phase and a different parser is activated then. Therefore it's neither possible to use delayed expansion nor FOR meta-variables in these options.
In other words, my versions 3 and 4 do not work because when defining the root folder of the FOR /R command, neither the %%~s nor the !folder! are correctly expanded by the expression parser. There is no way to change that, as this is a parser limitation. As I said in a comment below: the root folder option in the FOR /R command is basically only syntactic sugar to avoid the use of pushd/popd before and after the command. As this syntactic sugar is incomplete, we have to stick to the original syntax for some specific use cases, as the one presented here. The alternatives proposed by #Gerhard (using a subroutine CALL) or by #Mofi (parsing the result of a DIR command) are working, but they are neither more readable nor more efficient than the simple pushd/popd version I proposed initially.
My Approach for this would be really straight forward:
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do for /R "%basedir%" %%a in ("%%~i\*.jpg") do echo %%~fa
The double quotes inside of the subfolders variable is important here, it will ensure that folder names with whitespace are not seen as separators for the folder names. For instance:
set "subfolders="Folder A","Folder B","Folder C""
Edit
#echo off
set "basedir=C:\Base"
set "subfolders="A","B","C""
for %%i in (%subfolders%) do call :work "%%~i"
goto :eof
:work
for /R "%basedir%\%~1" %%a in (*.jpg) do echo %%~fa
It is in general not advisable to assign the value of a loop variable to an environment variable and next use the environment variable unmodified without or with concatenation with other strings being coded in batch file or defined already above the FOR loop within body of a FOR loop. That causes just problems as it requires the usage of delayed expansion which results in files and folders with one or more ! are not correct processed anymore inside body of the FOR loop caused by double parsing of the command line before execution, or command call is used on some command lines, or a subroutine is used called with call which makes the processing of the batch file much slower.
I recommend to use this batch file for the task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "basefolder=C:\Base"
set "subfolders=A B C "Subfolder D" SubfolderE"
for %%I in (%subfolders%) do for /F "delims=" %%J in ('dir "%basefolder%\%%~I\*.jpg" /A-D /B /S 2^>nul') do echo %%J
endlocal
The inner FOR loop starts for each subfolder defined in subfolders in background one more command process with %ComSpec% /c and the DIR command line appended as additional arguments. So executed is with Windows installed to C:\Windows for example for the first subfolder:
C:\Windows\System32\cmd.exe /c dir "C:\Base\A\*.jpg" /A-D /B /S 2>nul
The command DIR searches
in specified directory C:\Base\A and all it subdirectories because of option /S
for files because of option /A-D (attribute not directory) including those with hidden attribute set
matching the pattern *.jpg in long or short file name
and outputs to handle STDOUT of background command process just the matching file names because of option /B (bare format)
with full path because of option /S.
The error message output by DIR on nothing found matching these criteria is redirecting from handle STDERR to device NUL to suppress it.
Read the Microsoft documentation 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 output to handle STDOUT of background command process is captured by FOR respectively the command process which is processing the batch file. FOR processes the captured output line by line after started cmd.exe terminated itself. This is very often very important. The list of files to process is already in memory of command process before processing the first file name. This is not the case on using for /R as this results in accessing file system, getting first file name of a non-hidden file matching the wildcard pattern, run all commands in body of FOR and accessing the file system once again to get next file name. The for /R approach is problematic if the commands in body of FOR change a file to process like deleting, moving, modifying, copying it in same folder, or renaming a found file because of the entries in file system changes while for /R is iterating over these entries. That can easily result in some files are skipped or some files are processed more than once and it could result also an endless running loop, especially on FAT file system like FAT32 or exFAT. It is never good to iterate over a list of files on which the list changes on each iteration.
Command FOR on usage of /F ignores empty lines which do not occur here. A non-empty line is split up into substrings using a normal space and a horizontal tab as string delimiters by default. This line splitting behavior is not wanted here as there could be full qualified file names containing anywhere inside full name one or more spaces. For that reason delims= is used to define an empty list of delimiters which disables the line splitting behavior.
FOR with option /F would also ignore lines on which first substring starts with ; which is the default end of line character. This is no problem here because of command DIR was used with option /S and so each file name is output with full path which makes it impossible that any file name starts with ;. So the default eol=; can be kept.
FOR with option /F assigns by default just first substring to specified loop variable as tokens=1 is the default. This default can be kept here as splitting the lines (full file names) into substrings is disabled already with delims= and so there is always the full file name assigned to the loop variable.
This example uses just echo %%I to output the file names with full path. But it is now safe to replace this single command by a command block which does more with the JPEG files because of the list of JPEG files for each specified subfolder tree in base folder is always already completely in memory of command process processing the batch file.

Batch File to Loop Over Files In a Folder and Run Exe for Info

I am writing a *.BAT file to count the frames in every GIF file in a folder. Writing all that to TXT files would be nice, but I haven't gotten that far yet.
I can do this from the command line for single files.
This does what I want, but it does not output the number of frames to the window.
The next line is a sample of what it should do for a single file.
"c:\program files\ImageMagick-6.8.9-Q8\identify.exe" -format %%n 14215349.gif
These two lines should do the same for every file in the folder.
But, they just echo the command, rather than show its output.
set "frames=^0x22c:\program^ files\ImageMagick-6.8.9-Q8\identify.exe^0x22^ -format^ %%n"
forfiles /m *.gif /c "cmd /c echo %frames% #file"
The only thing I can see wrong is the double quotes, but this shows that's not it:
"c:\program files\ImageMagick-6.8.9-Q8\identify.exe" -format %%n "14215349.gif"
pause
It should output an integer for each file. Line 1 does this. Lines 2 and 3 do not; they just echo the command.
This code works:
for %%I in (*.gif) do "c:\program files\ImageMagick-6.8.9-Q8\identify.exe" -format %%n %%I >>FramesList.txt
pause
Thanks.
I hope I can ask for one more thing. The output I get is integers only, on one line, no spaces of filenames. How do I write this so the FramesList.txt file shows the frame count integer (never more than 3 digits) followed by the filename on each line?
I would suggest you use something like this:
#Echo Off
For %%K In (*.gif)Do If /I "%%~xK"==".gif" (
"%ProgramFiles%\ImageMagick-6.8.9-Q8\identify.exe" -format "%%[n] %%[f]" "%%K"
)>>"FramesList.txt"
In the example above, I have used %%K for safety, because there are no short form attributes which use %K and it is a distinct character, for recognition purposes. In addition, I have decided to use the long form attributes percent escapes for the number of images, and filename, i.e. %[n] and %[f], for the same reason, because, from a batch file, %%n and %%f could be confused with For metavariables.
The If statement was included because *.gif does not loop through files with .gif extensions, it loops all extensions which begin with .gif. Whilst you may not have any which match this scenario, there's no real reason not to add in the additional safety.

Windows batch looping through subfolders and running a command

I'm trying to have a nested for loop in windows command prompt where I go to each subfolder in a specified folder to concatenate all the text files in there into one text file. I'm trying to use
FOR /F IN (.) DO (for %f in (*.dat) do type “%f” >> aggregate.txt)
but its not working. Someone help me!
I'm not sure if you want to concatenate .txt or .dat files, but this should work for .dat
(for /r %f in (*.dat) do #type "%f")>aggregate.txt
Type HELP FOR from the command line for more info on the many forms of the FOR statement.
It is more efficient to enclose the whole command in parentheses and do redirection just once instead of doing append redirection for each file.
EDIT - solution for refined requirement in comment: one aggregate per folder
If needed, first delete any existing aggregate.txt files
del /s aggregate.txt
Then the following should give you your desired result
for /r %F in (*.dat) do #type "%F" >>"%~dpF\aggregate.txt"
Note that if you put the commands in a batch file you must double the percents before the FOR variables. For example, %F on the command line would need to be %%F in a batch file.
Simply use for /r ... to walk all subdirectories of a given directory. Example:
for /r %f in (*.dat) do #type %d >> aggregate.txt
no nesting needed. Alternately:
(for /r %f in (*.dat) do #type %d) >> aggregate.txt
You can use /d instead of /r to go only one level deep in the hierarchy and list only folders (not files matching a certain wildcard). Which could be used as a piece of the puzzle when doing the directory traversal "manually". However, given the issues with variable expansion (inside loops, with and without subs) in NT scripts I would recommend you stick to the straightforward solution. I've used this for cleanup scripts in the past, but moved to for /r for some time now.
This should get you started
#echo off
for /r %%i in (*.dat) do #call :ConcatFolder "%%i"
:ConcatFolder
if "" == "%1" goto :EOF
setlocal ENABLEEXTENSIONS
set FLDR=%~dp1
type "%1" >> "%FLDR%aggregate.txt"
endlocal & goto :EOF

For loop in batch file reading a file of File Paths

I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)

How to rename and add incrementing number suffix on multiple files in Batch Script?

I have 500 files coming in and I need to first check if any file(s) exist then rename all of them regardless of what their filename is (the files are named in a different language).
No need to process them in any order.
Rename:
1. “¦X¼d¬f-20110703-¦+¦dñHÑ-ª-¦=¬¦.xls”
2. “¦X¼d¬f-20110707-¦+¡¦-+¡8.xls”
3. “¦X¼d¬f-20110707-¦+¡¦ñj¦«.xls”
4. “¦X¼d¬f-20110708-¦+¡¦¬M¼n.xls”
5. “¦X¼d¬f-20110713-¦d¼O¼n¦hÑP.xls”
.
.
.
500
To:
“TWN_CH_INV_VISIT_FORM_01.xls”
“TWN_CH_INV_VISIT_FORM_02.xls”
“TWN_CH_INV_VISIT_FORM_03.xls”
“TWN_CH_INV_VISIT_FORM_04.xls”
“TWN_CH_INV_VISIT_FORM_05.xls”
.
.
.
“TWN_CH_INV_VISIT_FORM_500.xls”
Hope you could help me on this one. I’ve been trying to do this for weeks.
a simple FOR with a count (SET /A) should do what you need.
setlocal enabledelayedexpansion
SET /A COUNT=0
FOR %%A IN (*.xls) DO (
SET /A COUNT+=1
REN "%%A" "TWN_CH_INV_VIST_FORM_!COUNT!.xls"
)
See HELP FOR and HELP SET
This is a deceptively difficult question to solve.
The 5 year old PA answer has a few problems.
1) The FOR loop begins iterating without buffering the entire directory tree, so it has the potential to rename a file that has already been renamed. I believe that is why the 7 file is missing within r0mmel's comment.
2) Delayed expansion occurs after for variables are expanded, so the file name will be corrupted and the rename will fail if the name contains a ! character.
3) A rename can fail if there already exists a TWN_CH_INV_VIST_FORM_n.xls file with the same number.
At first I thought I could solve the problem using the following:
#echo off
for /f "delims=: tokens=1*" %%A in (
'dir /b *.xls ^| findstr /n "^"'
) do ren "%%B" "TWN_CH_INV_VIST_FORM_%%A.xls.new"
ren *.txt.new *.
I use DIR /B to list the files, and pipe that result to FINDSTR to prefix each file name with a line number, followed by a colon.
I then use FOR /F to iterate and parse the results into the number and the file name. FOR /F buffers the entire result before iterating, so I don't need to worry about renaming the same file twice.
I first give the renamed files a .xls.new "extension", just in case your directory already has files that meet the TWN_CH_INV_VIST_FORM_n.xls pattern. You don't want any name collisions. The final REN command then simply removes the .new extension to leave the desired .xls.
BUT, I just noticed that the original file names have lots of weird characters that could involve unicode that is not in the current code page. FOR /F does not play well with unicode.
There is one other minor issue in that the above does not pad the number to a fixed width. (this could have been solved easily enough)
So at this point it is time to break out my JREN.BAT regular expression renaming utility. It is pure script (hybrid batch / JScript) that runs natively on any Windows machine from XP onward. It has a built in facility to incorporate a fixed width incrementing number in the new name, and it works fine with unicode. I still temporarily give the new name the ".xls.new" extension to avoid any name collisions.
#echo off
call jren "^.*" "'TWN_CH_INV_VIST_FORM_'+$n+'.xls.new'" /j /npad 3 /fm *.xls
ren *.xls.new *.
I chose to pad the incrementing number to 3 digits instead of 2 because the OP said there could be 500 files.
Full documentation for JREN.BAT is available from the command line via jren /?, or jren /?? if you want paged output.

Resources