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.
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
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.
To do it, this bit needs to be defined: No#123
=====================================
Life is too important to be taken seriously.M2
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.
'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
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
Post a few of your 'first' [header] lines.
=====================================
Life is too important to be taken seriously.M2
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
Any suggestions??
See if this does the right thing for one file. ===========================================
@echo off & setLocal enableDELAYedeXpansionfor /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
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 SignatureNow 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...
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
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
Next, decide whether you want to move the files to 2 directories.
=====================================
Life is too important to be taken seriously.M2
Actually am ftping the files from the server, so after that i can check and move it to other folder also.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |