Computing.Net > Forums > Programming > batch file help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

batch file help

Reply to Message Icon

Name: Primetime
Date: November 4, 2008 at 12:01:51 Pacific
OS: Vista
CPU/Ram: 3.0/2gb
Product: Hp Compaq
Comment:

Greetings,

I would like to be able to read the header record from a file that is numerically named (i.e. 844205) and rename that file accordingly with the date appending.

For example, I have report number 837854 and the first record contains ID=tree. If id=tree then rename it 'tree-110108.txt'. The file name is always different. Thanks.

Rob



Sponsored Link
Ads by Google

Response Number 1
Name: Holla
Date: November 5, 2008 at 02:04:46 Pacific
Reply:

Getting the date in the above format is quite a task.
If it is ok to rename in whatever format your %date% is, then the below script may work:
May be slow though.
The batch file has to reside somewhere else, not in the current directory, because then it would try to rename itself :-(


@echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set iLine=0
for /f "tokens=2 delims==" %%i in (%%a) do (
set /a iLine=!iLine! + 1
if !iLine!==1 (
set id=%%i
ren %%a !id!-%date%.txt
)
)
)
endlocal

--
Holla.


0

Response Number 2
Name: Mechanix2Go
Date: November 5, 2008 at 04:57:42 Pacific
Reply:

ID=tree

Is that all that's in the first record?


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

M2


0

Response Number 3
Name: Primetime
Date: November 5, 2008 at 05:31:05 Pacific
Reply:

No, there is more, but the ID helps me to identify how I name that file.

Rob


0

Response Number 4
Name: Primetime
Date: November 5, 2008 at 06:20:40 Pacific
Reply:

Holla,

Thank you for your reply. The code that you provided is returning: "The syntax of the command is incorrect" x2

Rob


0

Response Number 5
Name: Mechanix2Go
Date: November 5, 2008 at 06:43:28 Pacific
Reply:

You need to post the first line.

We'll deal with the renaming later.


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

M2


0

Related Posts

See More



Response Number 6
Name: Primetime
Date: November 5, 2008 at 06:45:43 Pacific
Reply:

Sorry for not specifying earlier, but if ID=tree then tree will not be part of the naming convention, it is just a way to determine the naming convention. So if ID=tree then rename the file
"issue-112008.txt".

Rob


0

Response Number 7
Name: Primetime
Date: November 5, 2008 at 06:48:50 Pacific
Reply:

Here is the first line:

$$ADD ID=TREE BID='754647-TEST'

Rob


0

Response Number 8
Name: Mechanix2Go
Date: November 5, 2008 at 07:08:14 Pacific
Reply:

I see the line in #7 but you lost me with the naming convention in #6.


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

M2


0

Response Number 9
Name: Primetime
Date: November 5, 2008 at 07:27:57 Pacific
Reply:

Sorry about that.

Let's just say if the first row is:
$$ADD ID=TREE BID='754647-TEST'

I then want to rename the file
"issue-112008.txt" (issue is derived from ID=tree)

Rob


0

Response Number 10
Name: Mechanix2Go
Date: November 5, 2008 at 08:51:14 Pacific
Reply:

"(issue is derived from ID=tree)"

how?


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

M2


0

Response Number 11
Name: Primetime
Date: November 5, 2008 at 09:06:21 Pacific
Reply:

Sorry, I am having trouble explaining this.

If ID=tree then I want the beginning of the file to start with issue. Technically it is not derived, it's just how I want the the file to be named.

Each day, I will have 4 files, each with different ID's. All 4 will need to be renamed (it does not matter for now how we choose to rename them as long as they are different). The next day the process repeats with 4 new files (the only difference is the new date appended to the end). Does that make sense?

Rob


0

Response Number 12
Name: Mechanix2Go
Date: November 5, 2008 at 09:26:31 Pacific
Reply:

So the name prefix has nothing to do with the file content?


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

M2


0

Response Number 13
Name: Primetime
Date: November 5, 2008 at 09:50:12 Pacific
Reply:

correct.

Rob


0

Response Number 14
Name: Mechanix2Go
Date: November 5, 2008 at 11:28:49 Pacific
Reply:

I'm glad it only took 12 replies to clear that up.

This uses RANDOM to rename. Should not be a problem unless you have tons of files.

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


@echo off
setLocal EnableDelayedExpansion

call :ymd8

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
echo ren %%a !random!-!year!!month!!dat!%%~Xa
)
goto :eof

:ymd8

::== YMD8.bat

@echo off > d.d

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9 ) do if errorlevel %%a set dat=0%%a
for %%a in ( 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set dat=%%a
::echo Date %dat%

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday
::echo Day %day%

month
for %%a in (1 2 3 4 5 6 7 8 9 ) do if errorlevel %%a set month=0%%a
for %%a in ( 10 11 12) do if errorlevel %%a set month=%%a
::echo Month %month%

year
if errorlevel 215 set Year=2007
if errorlevel 216 set Year=2008
if errorlevel 217 set Year=2009
if errorlevel 218 set Year=2010
::echo Year %Year%

del day.com
del month.com
del year.com
del dat.com
::== DONE


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

M2


0

Response Number 15
Name: Primetime
Date: November 5, 2008 at 11:46:05 Pacific
Reply:

Thanks.

Rob


0
Reply to Message Icon

Creating folders automati... Automaticly organize fold...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: batch file help

batch file help www.computing.net/answers/programming/batch-file-help/13830.html

Batch File Help www.computing.net/answers/programming/batch-file-help-/11464.html

batch file help www.computing.net/answers/programming/batch-file-help/190.html