k4lls
k4lls

Reputation: 141

Unix function FIND with MS-DOS

How could I generate the same result but using windows (MS-DOS command) :

'find ./ -type f \( -name "*.Z3D" -or -name "*.z3d" -or -name "*.Z3d" -or -name "*.z3D" \)     -size +1000 > list_Z3D.txt'

list_Z3D.txt :

.//data/BOX07_CH4/ZEN111.Z3D
.//data/BOX07_CH4/ZEN111_copy.Z3D
.//data/BOX07_CH5/ZEN065.Z3D
.//data/BOX07_CH5/ZEN065_copy.Z3D
.//data/ZEN111_bis1.Z3D
.//data/ZEN111_bis2.Z3D
.//data/ZEN111_bis3.Z3D
.//sch_1/NBL01_CH1/ZEUS3058.Z3D
.//sch_1/NBL01_CH2/ZEUS3064.Z3D
.//sch_1/NBL01_CH4/ZEUS3075.Z3D
.//sch_1/NBL01_CH5/ZEUS3083.Z3D

Upvotes: 0

Views: 442

Answers (1)

dbenham
dbenham

Reputation: 130849

Windows file names are case insensitive, so that simplifies things a bit. But cmd.exe (batch) does not have very sophisticated directory listing capabilities. You would probably be better off with something like PowerShell, but I don't know that scripting language very well.

Or better yet, use the free gnu find for Windows utility as per Endoro's suggestion in the comment.

But if you want to restrict yourself to native cmd.exe (batch) commmands, then below are some options:

The following command will work on the command line. It will give the absolute paths, but it will not work properly with files whose size exceeds ~2 gigabytes because cmd.exe numbers are limited to signed 32 bit integers. Numbers greater than the maximum will be compared using string semantics instead of numeric, thus giving the wrong result. File sizes are always in bytes, so I've adjusted the size accordingly:

(for /r %F in (*.z3d) do @if %~zF geq 512000 echo %F) >list_Z3D.txt

Percents must be doubled to use within a batch file

@echo off
(for /r %%F in (*.z3d) do if %%~zF geq 512000 echo %%F) >list_Z3D.txt

Relative paths can be gotten with FORFILES, but it is comparatively slow. This command will work both in command line and batch file. It also will not work properly with files that exceed ~2 gigabytes.

forfiles /s /m *.z3d /c "cmd /c if @fsize geq 512000 echo @relpath" >list_Z3D.txt

WMIC can accurately screen by file size, regardless of the size. But it is quite slow when working with directory trees. This provides the absolute paths.

@echo off
setlocal disableDelayedExpansion
set "ext=z3d"
set "sz=512000"
for %%F in ("%cd%") do (
  set "drv=%%~dF"
  set "pth=%%~pnxF\%%"
)
wmic datafile where "drive='%drv%' and path like '%pth:\=\\%' and filesize>%sz% and extension='%ext%'" get name |more +1 >list_Z3D.txt

Either of the first two solutions can be made to work with large file sizes with additional programming. The following support file sizes up to ~999 terabytes. These solutions will be faster than the WMIC solution.

To accurately get absolute paths of files that are >= 512000 using a batch file:

@echo off
setlocal disableDelayedExpansion
>list_Z3D.txt (
  for /r %%F in (*.z3d) do (
    set "sz=000000000000000%%~zF"
    set "file=%%F"
    setlocal enableDelayedExpansion
    if !sz:~-15! geq 000000000512000 echo !file!
    endlocal
  )
)

To accurately get relative paths of files that are >= 512000 on the command line or in batch file:

forfiles /s /m *.z3d /c "cmd /v:on /c set sz=000000000000000@fsize&if !sz:~-15! geq 000000000512000 echo @relpath" >list_Z3D.txt

Upvotes: 1

Related Questions