Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.

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 EnableDelayedExpansionrem 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%.txtdel ~Dir.txt
del ~File.txt

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_*.txtNB: assumes that you don't clear away your previous reports.

@dtech:
>>dir /b | find "Report_"
why not
dir /b Report_*
and any way not to create temp files like ~File.txt and ~Dir.txt ??

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

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 EnableDelayedExpansionrem 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%.txtdel ~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!

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%.txtTO
ren "%File%" "Report_%FileDate%.txt"

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.

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

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

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

dtech10 strikes again
;)
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

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