Computing.Net > Forums > Programming > Append date within file to name of file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Append date within file to name of file

Reply to Message Icon

Name: hartlar
Date: June 25, 2009 at 08:43:57 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: dtech10
Date: June 25, 2009 at 16:08:15 Pacific
Reply:

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



0

Response Number 2
Name: ghostdog
Date: June 25, 2009 at 16:57:40 Pacific
Reply:

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


0

Response Number 3
Name: ghostdog
Date: June 25, 2009 at 16:59:49 Pacific
Reply:

@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


0

Response Number 4
Name: dtech10
Date: June 26, 2009 at 05:40:43 Pacific
Reply:

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%]


0

Response Number 5
Name: hartlar
Date: June 26, 2009 at 07:33:37 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: hartlar
Date: June 26, 2009 at 12:28:55 Pacific
Reply:

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"


0

Response Number 7
Name: dtech10
Date: June 26, 2009 at 13:46:13 Pacific
Reply:

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.



0

Response Number 8
Name: hartlar
Date: June 26, 2009 at 13:50:47 Pacific
Reply:

No, there is space and more text after the date. I should have been clearer in my initial post. Thanks!


0

Response Number 9
Name: hartlar
Date: June 26, 2009 at 13:52:15 Pacific
Reply:

And yes, HDS9R1C06/17/09 is always on the second line to the far left.


0

Response Number 10
Name: dtech10
Date: June 26, 2009 at 14:10:58 Pacific
Reply:

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!)


0

Response Number 11
Name: hartlar
Date: June 26, 2009 at 14:15:14 Pacific
Reply:

U da man! That's perfect. I really apprciate your time and help with this little project.

Have a great weekend!


0

Response Number 12
Name: dtech10
Date: June 26, 2009 at 14:22:37 Pacific
Reply:

HI Hartlar

Glad it worked and Have a nice weekend yourself.


0

Response Number 13
Name: Mechanix2Go
Date: June 28, 2009 at 02:32:05 Pacific
Reply:

dtech10 strikes again

;)


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






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: Append date within file to name of file

Appending date to file name www.computing.net/answers/programming/appending-date-to-file-name/616.html

batch file:Insert date in file www.computing.net/answers/programming/batch-fileinsert-date-in-file/11123.html

Batch file to copy and edit file www.computing.net/answers/programming/batch-file-to-copy-and-edit-file/16997.html