batch to rename based on fileconten

Grey / 001
December 24, 2008 at 07:26:27
Specs: win2003s, 486/8
Here is my request for a batch file:

I have a directory that is regularly filled with multiple files with weird but semi-predictable names like p1_sss.xx etc

Each of the files contains a unique identifying string at positions 143:147 (basically a count that is currently in the 1020 range. They will remain 4-digit for the rest of eternity as far as I am concerned.)

I'd like to automatically RENAME the files to be, eg

As I said, I know that the ID is sitting in each file at the specific POSITION 143:147...

Any help would be most appreciated.


See More: batch to rename based on fileconten

Report •

December 25, 2008 at 03:52:04
try this one. this is one is a bit slow because it read all the lines into temp file. i dont know the syntax to read a file char by char, nor the syntax to skip file by n char(s). maybe someone with more experience in batch prog can help you.

@echo off
SetLocal EnableDelayedExpansion

echo.%* | find "?" >nul && GOTO :SYNTAX

for /f "tokens=*" %%L in (%1) do ver err|set /P text=%%L>>~tmp_OneLiner.txt
for /f "tokens=*" %%L in (~tmp_OneLiner.txt) do set s=%%L & echo !s:~%2,%3!

del ~tmp_OneLiner.txt
goto :eof

goto :eof

Report •

December 25, 2008 at 09:53:05
If you're open to seeing other options, here's a simple and efficient Perl script.

The script would need to be in the same dir as the target files. If you want it to work on any dir, then we'd need to adjust how it retrieves the filenames.


use warnings;
use strict;
use Fcntl qw(:seek);

foreach my $file ( grep(/^p\d_/, <*>) ) {
open my $FH, $file or die "can't open '$file' $!";
seek $FH, 143, SEEK_SET;
my ($num) = <$FH> =~ /^(\d{4})/;
close $FH;
unless ( $num ) {
warn "unable to retieve ID number in '$file'\n";
rename $file, "${num}_$file" or warn "unable to rename '$file' $!";

Report •

December 26, 2008 at 01:43:28
"string at positions 143:147 "

Can you phrase that another way?

If at first you don't succeed, you're about average.


Report •

Related Solutions

December 26, 2008 at 12:17:24
Thanks for all the help - nothing works (yet), but I am getting some good ideas from all the input!

a)"input files" only contain a SINGLE LINE - albeit VERY LONG - we are talking 1000s of chars.
b)"string position" 143:147 means that the ID string I am interestd in START as the 143rd char in the line and ENDS as the 147th
(or, phrased differently, starts as the 143rd character in the line and is 4 digits long).
c) I MIGHT be able to use the "token" idea and simply search for the 25th token or so" preceeding / following is a ~, I think. I am experimenting with this right now.

I'd love to post an example file, but they contain prop. information :(

thanks to all up to now! Sorry for the slow response - am actually on vacation. Back 1/2/9.


Report •

December 26, 2008 at 12:34:14
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b') do (
set str=%%a
set str=!str:~142,4!
ren %%a !str!_%%a

If at first you don't succeed, you're about average.


Report •

January 1, 2009 at 21:45:51
I changed the last suggested script by adding another DO loop as show below: for all files in the directory, read and act. While it looks redundant, it works IN MY CASE since every file is only one line long. The key command I needed was set str:~142:4.

Thanks everyone for all the help. -a

---cut here---
@echo off & setLocal EnableDelayedExpansion
REM rename p*.x01 files by PREPENDING UID from char[142:145]

FOR /F %%f in ('dir /b p*.x01') DO (
FOR /F "tokens=* delims=" %%l in (%%f) DO (
set str=%%l
set str=!str:~142,4!
ren %%f !str!_%%f
---cut here

Report •

Ask Question