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.
Sorting a folder full of files
Moderator: DosItHelp
Re: Sorting a folder full of files
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
I sort first by extension, then by name descending so you can handle multiple extensions in one pass.
Dave Benham
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
Re: Sorting a folder full of files
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?
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!!!
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!!!
-
- Posts: 2
- Joined: 23 Jan 2012 18:14
Re: Sorting a folder full of files
Thanks guys,
With a little modification I was able to get that working...
Thanks..
With a little modification I was able to get that working...
Thanks..
Re: Sorting a folder full of files
Hi,
Could you post modifications.
All examples help.
The code was put into a .bat file... right?
Thanks
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