# CMD script to keep last index in directory, and move old index to archive

We have a fileshare where CAD data (pdf, dxf, stp) is written on release.
I would like to make a CMD script that keeps only the last (released partnumber) in the folder and moves the other indexes to a subfolder (\_achrive).
After that for every partnumber a zip should be created.

Here an example on how the filnames look like before the script runs:
800374_.dxf
800374_.pdf
800374_.stp
800374_A.dxf
800374_A.pdf
800374_A.stp
800374_B.dxf
800374_B.pdf
800374_B.stp
800263_A_Sheet__1.dxf
800263_A_Sheet__2.dxf
800263_A.pdf
800263_A.stp
800263_B_Sheet__1.dxf
800263_B_Sheet__2.dxf
800263_B.pdf
800263_B.stp
800263_C_Sheet__1.dxf
800263_C_Sheet__2.dxf
800263_C.pdf
800263_C.stp

The follwoing files would be moved the the \archive folder:
800374_.dxf
800374_.pdf
800374_.stp
800374_A.dxf
800374_A.pdf
800374_A.stp
800263_A_Sheet__1.dxf
800263_A_Sheet__2.dxf
800263_A.pdf
800263_A.stp
800263_B_Sheet__1.dxf
800263_B_Sheet__2.dxf
800263_B.pdf
800263_B.stp

After that the following zip Files will remain:
800374_B.zip
800263_C.zip

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Batchelor, Developer and EE Topic AdvisorCommented:
You cannot ZIP with native CMD commands. You can use 7za or a different commandline interface for ZIP software, though. Or use some VBS or PowerShell code instead or in addition.
0
Author Commented:
Yes i know, i've already done some zip stuff with cmd and 7za.
But i really have no idea how to do the first part with moving older index files and only keep the latest one.
I really want to keep it in cmd.
0
Batchelor, Developer and EE Topic AdvisorCommented:
I'll assume a DXF file is always present, and can be used as "head" file to determine the common part of a same-time set.
@echo off

set src=C:\Temp\EE\from
set dst=C:\Temp\EE\to

pushd %src%

for /F "delims=_ tokens=1*" %%A in ('dir *.dxf' /b /o:-d') do (
7za m -tzip %%A_%B.zip %%A_%%B* || exit /b 1
del %%A_%%B*
move %%A_* %dst%
)
popd

This code is untested, so be careful. You might want to comment or remove the delete for test to keep all files in the archive folder.
0
Technical Specialist/DeveloperCommented:
CircularLogArchiver can maintain the lifecycle of files. It can be configured to keep specific age files live, archive older than a specific data and optionally delete archives older than specific date
https://www.experts-exchange.com/articles/29108/Log-file-problem-CircularLogArchiver.html
0
Author Commented:
@Qlemo Thanks for the code

Does the code really zip only the last index of a number?
What does the %B and %%B part mean exactly?

Maybe i explain again my request:
I have a folder with thousands of files:
- every file starts with a part number (example:104165)
- every part number has several file types (pdf,dxf,stp)
- ever part number can have from no index to more then 10 (_,_A_,B,_C,_D,_E,_F,_G,_H....)
- for every part number i need pdf,dxf,stp zipped but only with the last index created
- if i have A,B,C it is C
- if i have A,B,C,D,E,F it is F

But have a few questions/remarks:
1. Apostroph after dxf seems to me to much
2. Don't find m switch for 7za, so used a
3. I get the following error on 7za part:
Error:
cannot open file
104165_B*.zip
the filename, directory name, or volume label syntax is incorrect


Thanks a lot
0
Batchelor, Developer and EE Topic AdvisorCommented:
%B is a typo, should have been %%B. But there some more issues with my code I detected just now.
@echo off
setlocal EnableDelayedExpansion

set src=C:\Temp\EE\from
set dst=C:\Temp\EE\to

pushd %src%

for /F "delims=_ tokens=1*" %%A in ('dir *.dxf' /b /o:-d') do ^
if exist %%A_%%B  (
del tmpzip.zip 2>nul >nul
set name=%%A_%%B
set name=!name:.dxf=!
7za a -tzip tmpzip.zip !name!* || exit /b 1
del !name!*
move %%A_* %dst%
ren tmpzip.zip !name!.zip
)
popd

%%A is the base name (up to the first underscore). The script selects the most recent DXF file having the same base name. We could sort for names instead, but for date seems to be much better.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Sorry for late answer. Qlemo's post helped a lot! Thanks again
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.