1.

Solve : Renaming files in a folder depending on size and order?

Answer»

Hello, I am new to this list and hope to find some help with this.
I am using Windows server 2003 and XP and need a bat file that can do the following if it is possible.
The files are in a folder lets say "D:\testfiles" and has to be copied to a new folder "D:\updates" and also renamed
I have in the "testfiles folder" files that looks like this:
test_20.id
test_20.ind
test_20.map
test_20.dat
test_21.id
test_21.ind
test_21.map
test_21.dat
etc etc...

I need them to be copied to "updates" and renamed to line1, line2, line3, line4, LINE5, line6, line7... etc in numerical order that always start with "1"
End result must be like this
line1.id
line1.ind
line1.map
line1.dat
line2.id
line2.ind
line2.map
line2.dat
etc etc...

The problem is that the filenames in the "testfiles" folder change so next time could be like this or even higher numbers, (they are always in numerical order):

test_72.id
test_72.ind
test_72.map
test_72.dat
test_73.id
test_73.ind
test_73.map
test_73.dat
etc etc...

As I have a bat file with "rename" function I need to check the names in the "testfiles" folder first and then edit the batfile so it has the correct filenames. This is a bit boring and time consuming as it can be up to 40 - 50 files. Is there a WAY to solve this in an easier manner?

Also it is a "group" ( for example test_72.*) of the files that is of no interest as they have a max total of 25 Kb, can they be sorted out and be removed from the process? Is this possible with a bat file to calculate total size of a group of files with the same name (except for the file type) and remove it, if it is below a specified size?

Regards
Soren


This comes dangerously close to programming and batch coding is really not suitable for this if for no other reason than for maintenance.

Code: [Select]@echo off
setlocal
set /a lines=1

for /f "tokens=1-3 delims=e." %%i In ('dir /o:-n /a:-d /b d:\updates') do (
set lines=%%j+1
goto out
)

:out
set /a count=0

for /f "tokens=* delims=." %%v in ('dir /a:-d /b d:\testfiles') do (
call :group "%%v"
)

:group
call set /a count=%%count%%+1
if %count% GTR 4 (
set /a count=1
call set /a lines=%%lines%%+1
)
for /f "tokens=1-2 delims=." %%w in ("%1") do (
copy d:\testfiles\"%1" d:\updates\line%lines%.%%x > nul
)


Quote

Also it is a "group" ( for example test_72.*) of the files that is of no interest as they have a max total of 25 Kb, can they be sorted out and be removed from the process? Is this possible with a bat file to calculate total size of a group of files with the same name (except for the file type) and remove it, if it is below a specified size?

It could be done, but I would suggest a separate script. Do one thing at a time, do it very well and then move on.

PS. The files get named properly, but a copy operation leaves the original files in place for the next run. Not knowing your operation, this might be a problem with duplicating data.Yes many thanks to you it works, but the problem is that this folder has other files as well and they will also be processed. So my fix was to copy the files I needed to a temporary folder and from there use your script. It seems that I can't add new lines to your script, like:

D:
CD Testfiles
COPY test*.* C:\temp\99\test*.*
C:
CD temp\99
Rename *.TMP *.DAT

Do I need another Bat file for doing this or is it possible to put new lines of codes before and after your script? If that is the case how do I activate another bat file within another?

Thanks


I used the "Call" function and it works. Now I just need to test and change a few settings. Thanks, it was of great help. The file size thing I have to live with for a while, I can remove it manually. Sidewinder, is it possible to change the numerical order so it starts with "0" instead of "1" then I can fix the problem more easily and don't need to remove the files. It now works perfect when I copied the files to a new folder (so I have only those files I need to edit) and run the script from there.

ThanksSoli004,

A couple of thoughts:

Change set /a lines=1 to set /a lines=0 near the top of the script.

You don't need to copy the files to another directory. Try filtering the files returned from the dir command with the same mask you used for your copy instruction:

Code: [Select]for /f "tokens=* delims=." %%v in ('dir /a:-d /b d:\testfiles\test*.*') do (

About sizing the files. You'd need to hold each group in memory until it's determined whether to keep or discard the group. You can use the %%~zv variable within the second for loop for your calculations.

It's not necessary to do all the work from a single script. Simple really is BETTER.

PS. I could swear that set lines=%%j+1 should be replaced by set /a lines=%%j+1, but neither you nor I report an error. Strange.
Sidewinder,
Works as a charm, this is great. removing the files is easier now when they are set to number 0, I can just put a few lines of "Del" and they are gone. All this has made me think of the final goal that I thought was not possible by using batch commands, perhaps I underestimate the potential of using batch

Ok last call, I need to add (create) to each group a txt file that has a few lines of text (same text for every file) ending with ".TAB"
For the moment I have 50 prepared .TAB files in a special folder that I copy to D:/updates in the script so each time I have 50 .TAB files when I perhaps only need let say 10 or less.

This is the text that need to be into each .TAB file for each group. So if there is 10 groups of files it has to be 10 files with this text included (example line1.TAB, line2.TAB etc...).

!table
!version 300
!charset WindowsLatin1

Definition Table
Type NATIVE Charset "WindowsLatin1"
Fields 1
Vagnamn Char (80) ;There were a few ways to approach this. An in-memory array would work, but I chose to create a temporary tab file which could be simply copied and renamed accordingly:

Code: [Select]@echo off
setlocal

> c:\temp\tab.dat echo !table
>> c:\temp\tab.dat echo !version 300
>> c:\temp\tab.dat echo !charset WindowsLatin1
>> c:\temp\tab.dat echo.
>> c:\temp\tab.dat echo Definition Table
>> c:\temp\tab.dat echo Type NATIVE Charset "WindowsLatin1"
>> c:\temp\tab.dat echo Fields 1
>> c:\temp\tab.dat echo Vagnamn Char (80) ;

set /a lines=1

for /f "tokens=1-3 delims=e." %%i In ('dir /o:-n /a:-d /b d:\updates') do (
set /a lines=%%j+1
goto out
)

:out
set /a count=0

for /f "tokens=* delims=." %%v in ('dir /a:-d /b d:\testfiles\test*.*') do (
call :group "%%v"
)
del c:\temp\tab.dat

:group
call set /a count=%%count%%+1
if %count% GTR 4 (
set /a count=1
copy c:\temp\tab.dat d:\updates\line%lines%.tab > nul
call set /a lines=%%lines%%+1
)
for /f "tokens=1-2 delims=." %%w in ("%1") do (
copy d:\testfiles\"%1" d:\updates\line%lines%.%%x > nul
)

Quote
perhaps I underestimate the potential of using batch

Actually many users over estimate the potential of using batch and end up complicating tasks that are better suited to other scripting languages.

Good luck.
Sidewinder,
Thanks but unfortunately its not working completely. The result I get is following for the tab file.

**************************
!version 300
!charset WindowsLatin1

Definition Table
**************************

I can not see why it jumps (not writing) over !table and the last 3 text rows. Is the spaces before the txt (last 3) a problem? I did try to see if that had something to do with it and removed the spaces but that did not work either.

Any clue?I'm not getting your result. In fact I was just congratulating myself on how well this works.

The tab file creation logic at the top of the script requires a single > character in the first line and double >> in all the other lines. Echo itself is transparent enough to accept most characters. Guessing the lines with stars were posted for effect and are not part of the tab file.

To debug, eliminate all the script except the top code where the tab file is created. You should be able to narrow down any error. If you copied/pasted the code, a stray character may have been picked up which is affecting the interpreter. If you retyped the code and you type like me, all bets are off.

Coding is a bore. The real fun comes when you start debugging. Coding is a bore. The real fun comes when you start debugging.

For me good coding is a work of art, I have SEEN just a few lines be making magic.

You are correct again (as usual) something happened when copy and paste, I could not see it but it helped re-typing it THANKS.

The only final "problem" I just find out is that the last "group" does not get a "tab" file.
test_0 to test_19 has a ".tab" but test_20 does not? Can you see this... or have I messed up again?Quote
Can you see this... or have I messed up again?

You didn't mess up, but it does make me wonder how I let this get by. First and last record PROCESSING is usually the most problematic.

Code: [Select]@echo off
setlocal

> c:\temp\tab.dat echo !table
>> c:\temp\tab.dat echo !version 300
>> c:\temp\tab.dat echo !charset WindowsLatin1
>> c:\temp\tab.dat echo.
>> c:\temp\tab.dat echo Definition Table
>> c:\temp\tab.dat echo Type NATIVE Charset "WindowsLatin1"
>> c:\temp\tab.dat echo Fields 1
>> c:\temp\tab.dat echo Vagnamn Char (80) ;

set /a lines=1

for /f "tokens=1-3 delims=e." %%i In ('dir /o:-n /a:-d /b d:\updates') do (
set /a lines=%%j+1
goto out
)

:out
set /a count=0

for /f "tokens=* delims=." %%v in ('dir /a:-d /b d:\testfiles\test*.*') do (
call :group "%%v"
)
del c:\temp\tab.dat

:group
call set /a count=%%count%%+1
if %count% GTR 4 (
set /a count=1
call set /a lines=%%lines%%+1
)
if %count% EQU 4 (
copy c:\temp\tab.dat d:\updates\line%lines%.tab > nul
)
for /f "tokens=1-2 delims=." %%w in ("%1") do (
copy d:\testfiles\"%1" d:\updates\line%lines%.%%x > nul
)

Quote
For me good coding is a work of art

It will be interesting when you revisit this code in the future (and you will) if you see art or just scratch your head in wonderment. Sidewinder,

I just want to thank you for an exellente solution.
Everything is working smoothly with no errors, saving me from lots of copying and renaming files manually.

Salute


Discussion

No Comment Found