Append date within file to name of file

June 25, 2009 at 08:43:57
Specs: Windows XP
I have a text file that will be created each morning by a 3rd party vendor. Well, their naming convention is as follows: Report_000.txt, Report_001.txt, etc. I would like to locate the date within the report and append it to the name of the file so it reads as: Report_06-17-09. The date is located on the second line (left justified) of the txt file in the following format: HDS9R1C06/17/09.

See More: Append date within file to name of file

Report •


#1
June 25, 2009 at 16:08:15
Hi Hartlar

This Help

Hi
The "/" would be an illegal character in a filename
so I have changed them to "-"
The code below assumes you don't have the charactor
in other filenames

@echo off
SetLocal EnableDelayedExpansion

rem Find Report file that doe'nt contain "-"
rem and set var File to it.
dir /b | find "Report_" > ~Dir.txt
type ~Dir.txt | find /v "-" >~File.txt
set /p File=<~File.txt
echo [%File%]

set x=0
for /f "tokens=* delims=" %%a in ('type "%File%"') do (
set /a x+=1
set Line2=%%a
if !x!==2 (set FileDate=!Line2:~-8!)

)
rem Change "/" char to "-"
set FileDate=%FileDate:/=-%
ren %File% Report_%FileDate%.txt

del ~Dir.txt
del ~File.txt



Report •

#2
June 25, 2009 at 16:57:40
if you can use gawk for windows (see my sig)
BEGIN{
	temp=0
	# this part finds the greatest number in the filename, ie latest file.
	for(i=1;i<=ARGC;i++){
		file=ARGV[i]
		gsub(/Report_|\.txt/,"",file)
	 	if(file>temp){
		 	temp=file
		 	f[temp]=ARGV[i]
	 	}
	}
	getline < f[temp]
	getline d < f[temp] #get second line
	gsub(/^.*[a-zA-Z]/,"",d)
	gsub(/\//,"-",d)
	filename = "Report_"d
	cmd = "rename "f[temp]" "filename".txt"
	print cmd
	#system(cmd) #uncomment to use	
}

save the above as myscript.awk and on command line

C:\test>gawk -f test.awk  Report_*.txt

NB: assumes that you don't clear away your previous reports.

GNU win32 packages | Gawk


Report •

#3
June 25, 2009 at 16:59:49
@dtech:

>>dir /b | find "Report_"

why not

dir /b Report_*

and any way not to create temp files like ~File.txt and ~Dir.txt ??

GNU win32 packages | Gawk


Report •

Related Solutions

#4
June 26, 2009 at 05:40:43
Hi Ghostdog

You're right about dir /b Report_* and I don't like using Temp
files either, they're messy. But I needed a File for the Set /p command for the For loop. Unless anybody knows a better way.
I guess I wrote it quickly without thinking as really I only needed one Temp file.

rem Find Report file that doe'nt contain "-"
rem and set var File to it.
dir /b Report_* | find /v "-" >~File.txt
set /p File=<~File.txt
echo [%File%]


Report •

#5
June 26, 2009 at 07:33:37
First, I want to thank each of you for your response. Here is the code that I am working with. Obviously, your code...

@echo off
SetLocal EnableDelayedExpansion

rem Find Report file that doesn't contain "-" and set var File to it.
dir /b Report_* | find /v "-" >~File.txt
set /p File=<~File.txt
echo [%File%]

set x=0
for /f "tokens=* delims=" %%a in ('type "%File%"') do (
set /a x+=1
set Line2=%%a
if !x!==2 (set FileDate=!Line2:~-8!)

)
rem Change "/" char to "-"
set FileDate=%FileDate:/=-%
ren %File% Report_%FileDate%.txt

del ~File.txt


When I rem del ~File.txt:

~File.txt is created with the report name that I want renamed in the body of the file. For instance, Report_000.txt is on the first line of ~File.txt. Nothing actually happens to the report that I want renamed: Report_000.txt. Does something else need to be changed in the code above? Thanks again!


Report •

#6
June 26, 2009 at 12:28:55
Please disregard my post from earlier today. I made a small change and got the script to work but it places text from the middle of the file instead of the date... Any ideas? Thanks again for the help.

FROM
ren %File% Report_%FileDate%.txt

TO
ren "%File%" "Report_%FileDate%.txt"


Report •

#7
June 26, 2009 at 13:46:13
Hi Hartlar

ren "%File%" "Report_%FileDate%.txt"

The quotes are only needed if the filename contains spaces.
but don't do any harm in this case.

In your first post is the second line of your file always HDS9R1C06/17/09 as stated as the code if !x!==2 (set FileDate=!Line2:~-8!) alway takes the last 8 charactors of
that line, which i assumed would be the date.
Is the date always at the end.



Report •

#8
June 26, 2009 at 13:50:47
No, there is space and more text after the date. I should have been clearer in my initial post. Thanks!

Report •

#9
June 26, 2009 at 13:52:15
And yes, HDS9R1C06/17/09 is always on the second line to the far left.

Report •

#10
June 26, 2009 at 14:10:58
Hi Hartlar

If the Date is always in the same position.
Change this line
if !x!==2 (set FileDate=!Line2:~-8!)
To this
if !x!==2 (set FileDate=!Line2:~7,8!)


Report •

#11
June 26, 2009 at 14:15:14
U da man! That's perfect. I really apprciate your time and help with this little project.

Have a great weekend!


Report •

#12
June 26, 2009 at 14:22:37
HI Hartlar

Glad it worked and Have a nice weekend yourself.


Report •

#13
June 28, 2009 at 02:32:05
dtech10 strikes again

;)


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

M2


Report •


Ask Question