I need to display ordered list of files on my hard drive "c:" with the help of cmd. we should order files by size - cmd

I need to display ordered list of files on my hard drive "c:". we should order files by size. When i do it, I have problem. Every directory dispays grouped. For example on my hard drive are 2 directories : 1-st- "55" and it has some files. 2-nd-"66" and that directory also has some files and 3 files. When i tried to display ordered list of files i saw next: first dispayed all 3 files from hard drive "\c" then displayed files from directory "55" and next to them dispalyed files from directory "66". How can i display all files and order them by size withot ftouping them by directories. I used command dir /a-d /s /o-s /b

As Mofi already mentioned, dir isn't built to do that. You have to build a list ("array") of the files and their size, then sort by size. I'm not sure if you need just the bare sorted list of files or their size too, so adapt the echo statement to your needs. You can simply reverse the sort order with sort parameter /r
#echo off
setlocal enabledelayedexpansion
set cnt=10000
for /r %%a in (*) do (
set /a cnt+=1
set "siz= %%~za"
set "file[!cnt!]=!siz:~-10! %%~fa"
)
REM for debug: set file[
for /f "tokens=2,* delims= " %%a in ('set file[ ^| sort /+15') do (
echo %%a - %%b
)
set file[ lists all variables (I started with 10000 to get the variable names to the same length for sort /+15 to work properly - adapt if you might have more than 89998 files),
sort can't handle natural numbers, it sorts strictly based on ASCII values, so we have to prepend the length with spaces or zeroes and get all of them to the same length for sort to work "properly" (a.k.a to "human standards").
sort /+15 starts sorting at the 15th character of each line and so ignores the variable name - adapt the 15 accordingly if you change 10000 or the file[...] variable name.
Also increase the number of spaces (currently 10) and the 10 in :~-10 if you might have file sizes in the Gigabyte range.

Related

Batch script to read numerical part of file and rename with next higher integer

I have a file, such as -
foofile_1.ext
A script should read the numerical part of the file, and then rename the file with the next integer, i.e., after execution, the file name should be
foofile_2.ext
I can do it with a C++ / c application or even in bash but not sure how to write a batch script to perform this rename. The filename before the _ isn't going to change, and _ will aaways appear in the same position within the filename.
I can strip the filename to _, but recognizing the numerical is an implementation I am not familiar with. Once I recognize the numerical, I can increment it and rename the file.
Something to consider is that renaming foofile_1.ext to foofile_2.ext will fail should foofile_2.ext already exist. One way to get around it is to rename in descending numerical order, I posted an answer like that before on SO.
I am however not going to post the same answer here, nor link that answer. I will however show one other method:
#echo off
setlocal enabledelayedexpansion
for /f "tokens=1,*delims=_" %%i in ('dir /b /a-d "*_*.ext"') do (
echo %%~nj | findstr /R /V /C:"[A-Z]">nul && (
set /a numeric=%%~nj+1
ren "%%~i_%%~j" "%%~i_-hld-!numeric!%%~xj"
)
)
for /f "delims=" %%f in ('dir /b /a-d "*_-hld-*.ext"') do (
set "name=%%~f"
ren "%%~f" !name:-hld-=!
)
Considering that your input is as you said and does not contain earlier _'s anywhere. This will just take each file with the *_*.ext format. We split by the _ into two tokened metavaiables (%%i and %%j) We take the numeric value and increment by one, then rejoin %%i which is pre _. This however is where the problem comes when the file you are trying to rename to exists, so for that, first we test if %%~nj does not have Alphabetical characters only, using findstr (not doing special characters in this free code) Secondly we give a temporary addition to the name to prevent name clashing.
Once we are done, we simply do a rename on all the files containing the _-hld- temp inclusion.

Need to view merged photos directory in date sequence

I have two photo folders taken with different cameras, and want to merge them by date, and then view the combined directory in date sequence. I have merged them, and the directory dates look good, but the tools I use insist of showing the photos in name sequence, which is not what I want. I thought of doing a batch rename function to make the date part of the file names, using a bat file, but the DOS DIR command doesn't seem to use this date - if I do
for /f "skip=5 tokens=1,2,4,5* delims= " %%i in ('dir /a:-d /o:d /t:c') do (
etc., most of the file dates are correct, but some of the files seem to be using the "created date" of the directory.
Maybe there is a simple solution as this must be a common problem, but I haven't found a good solution - short of doing a laborious manual rename of some 700+ files!
Help would be appreciated!
Paul M.
for /f "tokens=1*delims=[]" %%a in ('dir /b /a-d /od /t:c^|find /v /n "" ') do echo ren "%%b" "%%a%%~xb"
Perform a directory scan of the source directory, in /b basic mode /a-d without directories in date order selecting created-date, number in brackets each line that doesn't match an empty string and assign each filename found to %%b and the number to %%a
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Note: filenames that begin [ or ] will be processed incorrectly. If an existing file has a name which belongs to the target nameset (ie 1.? 2.? etc.) then simply prefix the target name with a prefix that doesn't already exist, eg ren "%%b" "new_%%a%%~xb" (where there are no existing files named new_*)
I'm not sure about Win7's capability when displaying files using the utility you are using (Explorer?) Win10 has the capability of selecting display by creation-date (select sort by then creation-date)
If you are sure you are sorting by creation-date then probably you'd need to select some other sorting scheme (change t:c to t perhaps)
If there are filenames that aren't being converted, then you are probably adding files (called img*) to a directory that already contains files named 1,2,3... - in fact, it's difficult to see how that phenomenon could happen otherwise.
Change "%%a%%~xb" in the rename command to "xyz%%a%%~xb" and you should find that the files are then all renamed xyz1,xyz2,xyz3 etc. If you then reprocess the files with xyz removed, the names should become 1,2,3 with no omissions.

Windows batch file to find duplicates size files in harddisk (no matter the name and the extension)

I'd like a batch file ( Windows CMD is the interpreter, a .bat ) to do this type of task:
1) Search through a folder and its subfolders (entire harddisk)
2) Find files with the same size (no matter filename and extension)
3) Display these files (or not)
Thank you for any kind of help! :)
EDIT:
With the following commands, I can know all sizes that the files have...
#echo off
set "filename=*.*"
for %%A in (%filename%) do echo.Size of "%%A" is %%~zA bytes
but now the big problem is that you need to compare the first, with the remainder and so on!
The Batch file below should solve your problem; however, be aware that in despite of its apparent simplicity, it is based on advanced concepts, like array management in Batch files.
#echo off
setlocal EnableDelayedExpansion
rem Group all file names by size
for /R %%a in (*.*) do (
set "size[%%~Za]=!size[%%~Za]!,%%~Fa"
)
rem Show groups that have more than one element
for /F "tokens=2,3* delims=[]=," %%a in ('set size[') do (
if "%%c" neq "" echo [%%a]: %%b,%%c
)
This program may take too much time if the number of files is large or if the starting folder have a long path.

Naturally Sort Files in Batch

I have a folder with 20 some files and am wanting to sort them similar to the way Windows Explorer does. The files I have all have prefixes of block sizes like so:
1024KB.log
32KB.log
64KB.log
4KB.log
256KB.log
512KB.log
But when I sort them in batch, it only looks at the first digit then sorts them like so:
1024KB.log
256KB.log
32KB.log
4KB.log
512KB.log
64KB.log
I want to sort them by smallest to largest block size. Any ideas?
EDIT: I also have to keep the file name integrity because I then call another script which uses the file names and creates strings.
Take Command - the CMD.EXE replacement has natural sorting.
If you can pad the filenames with zeros then a normal sort will return them ok.
This code will return them sorted numerically:
#echo off
type nul>1024KB.log
type nul>32KB.log
type nul>64KB.log
type nul>4KB.log
type nul>256KB.log
type nul>512KB.log
setlocal enabledelayedexpansion
for %%a in (*.log) do (
set num=0000000000%%a
set num=!num:~-14!
set $!num!=%%a
)
for /f "tokens=1,* delims==" %%a in ('set $0') do echo %%b
pause

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