Solved I need help with a batch file to change a file name

August 27, 2014 at 13:42:47
Specs: Windows 7
Hi I have worked with batch files before but I'm stuck on how I can write a batch file that will give me a file name like this: {2-Letter State}_{Provider Acronym}_{8-Digit Date}_{2-Digit Hour}_{File Number}.{File-Type Suffix}

when everything is said and done the file name will look like this:
• CA_DH_20131028_21_001.hl7
• CA_DH_20131028_21_002.hl7
the idea is the the file number will increase by one if it is within the same hour using a 24 hours clock. Any ideas on how to code this.


See More: I need help with a batch file to change a file name

Report •

✔ Best Answer
August 28, 2014 at 20:09:07
@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /od *.xo'') do (
for /f "tokens=1-6 delims=/: " %%b in ("%%~ta") do (
rem echo file date-time components: %%b %%c %%d %%e %%f %%g
set hr= %%e
set hr=!hr: 0=!
set /a hr=hr"%%"12
if %%g equ PM set /a hr+=12
set new=%%d%%b%%c_!hr!
if "!new!%" neq "!prev!" set serial=0&set prev=!new!
set /a serial+=1
set out=00!serial!
set out=!out:~-3!
echo ren "%%a" "CA_DH_!new!_!out!.hl7"
)
)

::========= end batch - prototype only, not set for working

message edited by nbrane



#1
August 28, 2014 at 04:35:51
Where does 2-Letter State come from?

Same goes for the other chunks.

=====================
M2 Golden-Triangle


Report •

#2
August 28, 2014 at 05:38:25
The 2 letter state is the state in which you are located in, for example CA= California, the next 2 letters are for the hospital you work for DH=Downing Hospital, the date the file was created, in YYYYMMDD, the hour it was created, using the 24 hour clock, then the file number. The file number is created when the file is created, but if there are more than one created in that hour then the file number goes up by one, ex. 001, 002 like the example I gave up above. Finally the HL7 is the type of transaction it is which is Health Level 7 coding.

I hope this helps, thanks for replying M2. I'm hoping you could help me


Report •

#3
August 28, 2014 at 08:47:53
Let me follow up on Mechanix2Go's question. Do we get to pick the above details? If not, where do we get the data.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 28, 2014 at 09:24:24
these files come from our other system into a folder they are named example: 34353.xo I have to rename the file and then upload it to another site. The format for the name came from the site that I have to upload to. This was their idea not mine. Something simplier would have been better but they want it this way. Hope this answers your questions.

Thanks


Report •

#5
August 28, 2014 at 18:23:00
The {2-Letter State}_{Provider Acronym} is standard, it will be hard coded into the script, I will need to add the date and hour (24 hour clock) to the naming convection and then do a loop as it checks for the current hour and determines what the file number should be for that hour, when the hour changes then the count will start back at 001. I can figure out almost all of it but the loop. That is the part that I'm having a hard time with. I'm searching for examples that I can use for this but I have not found anything as of yet.

Thanks for your help.

CLaboy


Report •

#6
August 28, 2014 at 20:09:07
✔ Best Answer
@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /od *.xo'') do (
for /f "tokens=1-6 delims=/: " %%b in ("%%~ta") do (
rem echo file date-time components: %%b %%c %%d %%e %%f %%g
set hr= %%e
set hr=!hr: 0=!
set /a hr=hr"%%"12
if %%g equ PM set /a hr+=12
set new=%%d%%b%%c_!hr!
if "!new!%" neq "!prev!" set serial=0&set prev=!new!
set /a serial+=1
set out=00!serial!
set out=!out:~-3!
echo ren "%%a" "CA_DH_!new!_!out!.hl7"
)
)

::========= end batch - prototype only, not set for working

message edited by nbrane


Report •

Ask Question