Batch script to rename the files in a folder

October 20, 2010 at 04:23:44
Specs: Windows XP, 512
Hi could anybody help on this, i need to write a batch script that will read the first line of a each file in a folder and with which i need to rename each files using the header of the file. To be more specific, say i have 3 files like x.01, x.02,x.03 in a folder, now i need to open each file and read the header, say if i open x.01 it will have an header or first line like this
XXX XXX XXX XXX REPORT PAGE 1
now i need to get only the strings XXX XXX XXX REPORT from this first line of the file which is actually the report name and rename the file x.01 with this name (say XXX XXX XXX REPORT.DOC) and i need to repeat this procedure to all of the files in the folder. each file will have a different header. It would be great if somebody could help me on this.
Thanks in advance.

See More: Batch script to rename the files in a folder

Report •


#1
October 20, 2010 at 04:50:44
XXX XXX XXX XXX REPORT PAGE 1

So you want to drop everything after PAGE, right?


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
October 20, 2010 at 05:45:30
Yes! And even the characters before "XXX XXX XXX REPORT" (XXX). i.e if the header is like

No#123 The Total Report Page 1

i need only "The Total report" and need to rename the file with this.


Report •

#3
October 20, 2010 at 05:55:50
To do it, this bit needs to be defined:

No#123


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
October 20, 2010 at 10:31:21
means? this will have the id no of the server to which the report belongs. Say if all these reports are for server 123, then it will have the first line with the server id no, the report name and the page no. this report may have many pages with the same header. Could you help please. This is an emergency.

Report •

#5
October 20, 2010 at 10:50:14
'defined' means we need to know if the front bit is always n chars, always contains #, or what.

It's fine & dandy for a human to look and say leave out THAT part.

But a script can't think, so it needs to be told what to do.


=====================================
Life is too important to be taken seriously.

M2


Report •

#6
October 21, 2010 at 00:49:27
Server: 1234 BALANCE REPORT Page 1

This is how the heading would be. it will have spaces inbetween. Actually am pulling these reports from the server and placing it a folder and from ther i need to rename it. So if pull the reports from different server then server header will change, so defining may be not possible if am pulling reports from different servers.

Please let me know if you need some more details. I tried the below code but it dint work,

for %%a in (X.*) do (
call :sub1 %%a
ren %file% %str%.DOC
)
goto :eof

:sub1
set file= ?(i should get the firs file name am accessing)
for /f "delims=" %%b in (??X1 or the file am acessing) do (
set str=%%b
)

I dont know what to substitute in these places. Please help


Report •

#7
October 21, 2010 at 02:20:25
Post a few of your 'first' [header] lines.


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
October 22, 2010 at 07:02:53
Server: 1234 BALANCE REPORT Page 1

H Server: 2456 ACCOUNTING REPORT Page 1

$EF Server: 2456 PRODUCTIVITY REPORT Page 1

Server: 2456 REPORT Page 1


Report •

#9
October 25, 2010 at 20:47:54
Any suggestions??

Report •

#10
October 26, 2010 at 04:15:43
See if this does the right thing for one file.

===========================================
@echo off & setLocal enableDELAYedeXpansion

for /f "tokens=2 delims=:" %%a in (myfile) do (
call :sub1 %%a
goto :done
)
:done
echo ren myfile "!NEW!"

goto :eof

:sub1

for /f "tokens=2-5" %%i in ("%*") do (
if "%%l" equ "" (
set NEW=%%i %%j
) else (
set NEW=%%i %%j %%k
)
)

goto :eof


=====================================
Life is too important to be taken seriously.

M2


Report •

#11
October 28, 2010 at 02:55:16
Great!!!! it works... So i need to build another loop to modify all the reports??
And one more thing i noted now is i have one report which has the header in the second line something like this

(s9H
(s10B Report sheet - 10/08/2010 02:23 Signature

Now i need to modify the code to change this file name.

Thank you so much for your help. Hope you can help me on this too...


Report •

#12
October 28, 2010 at 03:33:32
For files with the needed text in line 2:

======================================

@echo off & setLocal enableDELAYedeXpansion

for /f "skip=1 tokens=2 delims=:" %%a in (myfile) do (
call :sub1 %%a
goto :done
)
:done
echo ren myfile "!NEW!"

goto :eof

:sub1

for /f "tokens=2-5" %%i in ("%*") do (
  if "%%l" equ "" (
    set NEW=%%i %%j
    ) else (
    set NEW=%%i %%j %%k
  )
)

goto :eof


=====================================
Life is too important to be taken seriously.

M2


Report •

#13
October 28, 2010 at 05:43:23
Thanks! but i have aproblem with this the folder has both the files i.e the file which has header in first line as well as teh file which has teh header in second line. So need an condition.. Also i tried the code by having three files in the folder but it renames only the first file.
dir/b ios.* >report.txt
for /f %%a in (report.txt) do (
call :Sub1 %%a
goto :done
)
:done
echo rename %report% %NEW%
goto :EOF
:Sub1
for /f "tokens=2-5" %%i in ("%*") do (
if "%%l" equ "" (
set NEW=%%i %%j
) else (
set NEW=%%i %%j %%k
)
)

This is what i tried please help!!!!!!! bare with me


Report •

#14
October 28, 2010 at 06:07:31
Next, decide whether you want to move the files to 2 directories.


=====================================
Life is too important to be taken seriously.

M2


Report •

#15
October 28, 2010 at 23:45:17
Actually am ftping the files from the server, so after that i can check and move it to other folder also.

Report •

#16
November 1, 2010 at 03:11:46
Hi, any idea on the above???

Report •


Ask Question