Sorting a folder full of files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
acerimmer10
Posts: 2
Joined: 23 Jan 2012 18:14

Sorting a folder full of files

#1 Post by acerimmer10 » 23 Jan 2012 18:20

Hi,

I was wondering if someone could help me getting started on a batch file for sorting a folder full of files.

The files are named in the format of <filename><Rev Letter>.pdf example 00500-1747B.pdf

What we are trying to do is to remove all the older revisions from the folder, i.e if a D exists remove A-C from the folder into an archive folder.

I was thinking of using the Dos short file name as the identifier ~3 etc but if there is an easier way to identify the files that would be great.

Any suggestions appreciated.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Sorting a folder full of files

#2 Post by dbenham » 23 Jan 2012 21:00

I'm not seeing how the short 8.3 name can help.

This is totally untested. Please make a copy of your directory before you try this :!:

Code: Select all

@echo off
setlocal enableDelayedExpansion
set prevName=:none
for %%F in ('dir /b /oe-n *.pdf *.docx') do (
  set "name=%%~nF"
  set "name=!name:~0,-1!%%~xF"
  if !name!==!prevName! del "%%F"
  set "prevName=!name!"
)

I sort first by extension, then by name descending so you can handle multiple extensions in one pass.


Dave Benham

Aacini
Expert
Posts: 1930
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Sorting a folder full of files

#3 Post by Aacini » 23 Jan 2012 21:09

Well, you want not to sort file names, but to archive several files excepting the last one from a group of similar-named files, right?

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set lastName=.
for %%f in (*.pdf) do (
   set newName=%%~Nf
   set newName=!newName:~0,-1!
   if !lastName! == !newName! echo Archive: !lastFullName!
   set lastName=!newName!
   set lastFullName=%%f
)

Previous code assume there is not spaces embedded in file names, but it is easy to fix this point if needed.

Opps! Dave: your answer was posted when I am writting the mine!!!

acerimmer10
Posts: 2
Joined: 23 Jan 2012 18:14

Re: Sorting a folder full of files

#4 Post by acerimmer10 » 24 Jan 2012 16:57

Thanks guys,

With a little modification I was able to get that working...

Thanks..

aj50
Posts: 1
Joined: 01 Feb 2012 11:06

Re: Sorting a folder full of files

#5 Post by aj50 » 01 Feb 2012 11:24

Hi,

With a little modification I was able to get that working...


Could you post modifications.
All examples help.
The code was put into a .bat file... right?

Thanks

Post Reply