-1

For example,

We have a folder

c:\files

The folder contains files like these:

 abc_m2611somthing.pdf
 abc_m2611somthing.dat
 abc_m2611somthing.log
 abc_m3849somthing.pdf
 abc_m3849somthing.dat
 abc_m3849somthing.log
 xyz_s1654somthing.pdf
 xyz_s1654somthing.dat
 xyz_s1654somthing.log

New files are generated and moved to this folder every day. File names and extensions of these files are constant, except numbers in the middle, every day the 4 digits in new files will be different.

My task:

To write a batch which will take files with the same numbers, and archive them. The name of the archive must be the same as the file name.

The end result should looks like this:

abc_m2611somthing.zip
abc_m3849somthing.zip
xyz_s1654somthing.zip

Each of these archives consist all files with the same number.

The difficulty lies in the fact that the numbers are different every day.

rene
  • 41,474
  • 78
  • 114
  • 152
Oleg
  • 43
  • 1
  • 6
  • 5
    There are plenty of answers where this type of task has been requested, _the exact filenames may be different but the method and task are the same, even if the compression part may not be incorporated_. Please use the search facility and write some code yourself, this is not a free coding service. After writing and testing your code, if it fails to work as written and intended, you can [edit your question](https://stackoverflow.com/posts/56509811/edit), to include it and any other information as recommended in [ask]. – Compo Jun 08 '19 at 20:25
  • I have not found information about it. Can you please point me to the links where they talk about it? I know that it is possible with the programming language. I would like to know if it can be implemented using the windows batch script. – Oleg Jun 08 '19 at 20:43
  • @Oleg As per my knowledge, It can be implemented using the batch file. You can take a look at this link to understand how you can zip the files using batch file https://stackoverflow.com/questions/18180060/how-to-zip-a-file-using-cmd-line Also, take a look at this link to learn batch file programming http://www.trytoprogram.com/batch-file/ – Mova Jun 08 '19 at 20:47
  • @Oleg I would like to help you with this. But first, try something from your side. It will help you in a future update for the same code. You can come back If you get stuck anywhere while implementing this. – Mova Jun 08 '19 at 20:52
  • Take a look at the answers to [this question](https://stackoverflow.com/q/52326060), they should give you enough information to begin some coding. – Compo Jun 08 '19 at 20:55
  • @MOI Thanks for your answer. I know how to zip files and I know basic of batch scripting. I would like to know how to sort files with dynamic names. – Oleg Jun 08 '19 at 21:18
  • @Compo Thanks, I'll take a look. – Oleg Jun 08 '19 at 21:18

1 Answers1

0

So, I managed to write the batch

@echo off

set "srcfolder=c:\files"
set "dstfolder=c:\zipfiles"
set "winrar=c:\Program files\WinRar"

for /f %%A in ('dir /b %srcfolder%') do (

    cd %winrar% 
    rar a -ep "%dstfolder%\%%~nA.rar" "%srcfolder%\%%~nA*"

    )
)

It takes all the files with the same numbers and puts them into the archive with the same name as the files.

Oleg
  • 43
  • 1
  • 6