# need help from guru to make a DOS batch file



## peter733 (Feb 1, 2012)

Dear Sir/Madam,

I'm looking for help and willing to negotiate for reward. I would like a single DOS batch file that can do the following actions on all the files in the same folder where the bat file is located:

there four possible formats :

2012-02-01 09.07.52.jpg
video-2012-01-31-15-12-02.mp4
recording-20111231-190945.mp3
2012-02-01 time 08_20_10 Outgoing Telephone Call +19091234567.mp3

I want to remove the "video-" and "recording-" out of the filename
I want to insert a text " time " between the date and the timestamp
I want to use dots instead of "-" and underscore

the results should be :

2012.02-01 time 09.07.52.jpg
2012.01.31 time 15-12-02.mp4
2011.12.31 time 19.09.45.mp3
2012.02.01 time 08.20.10 Outgoing Telephone Call +19091234567.mp3

in the end I want to move files that contain the phone number 19091234567.mp3 to a specified file. I tried move *19091234567.mp3 to c:\0 but it didn't work. I thought it should but I'm not good at this.

I really hope someone can help me make a single batch file that can rename the files that I accumulate from all four sources. I am willing to reward with a big thank you, a recommendation on linked.com, or something else entirely.

thank you for reading. I look foward to hear from you

peter


----------



## Squashman (Apr 4, 2003)

Your output file names are not consistent with your description. You still have hyphens in your output file names. I thought you wanted periods. Which in my opinion is not a good practice to do but I guess that is for you to decide. You should either stick with the hyphens or underscores or don't use any at all. I don't personally ever use time in my file names but I do use date and I always just do a YYYYMMMDD format. I do get files from our customers that do have the time in it and I usually see the same thing with that as well. HHMMSS. So they will send the file as 20120201_080610_magazine_data.txt. Why have the word TIME in the file name if you already know it is the second part of the file name. You might as well put the word DATE before the date then.

I am not understanding what you are trying to do with the MP3 file with the Phone number in it. Are you trying to move or copy that file to a folder named *C:\0*?


----------



## peter733 (Feb 1, 2012)

Squashman, thanks for reading my post. You did notice my mistake in providing the example. I personally preferred using the dots because all this time when I rename the dates and the times manually I used dots. I've always use numbers/digits and dots more often than with hypens because they are used more often in currency, in math, in Excel, etc. I use hypens only when typing out calculation which is not as often as typing 3.14 (Pi), or $1.99. I hope you get the idea.

Nevertheless I figured out the renaming part by looking at this post

I will post my codes separately so others can benefit from it, or not

thanks again, Squashman



Squashman said:


> Your output file names are not consistent with your description. You still have hyphens in your output file names. I thought you wanted periods. Which in my opinion is not a good practice to do but I guess that is for you to decide. You should either stick with the hyphens or underscores or don't use any at all. I don't personally ever use time in my file names but I do use date and I always just do a YYYYMMMDD format. I do get files from our customers that do have the time in it and I usually see the same thing with that as well. HHMMSS. So they will send the file as 20120201_080610_magazine_data.txt. Why have the word TIME in the file name if you already know it is the second part of the file name. You might as well put the word DATE before the date then.
> 
> I am not understanding what you are trying to do with the MP3 file with the Phone number in it. Are you trying to move or copy that file to a folder named *C:\0*?


----------



## peter733 (Feb 1, 2012)

@echo off
cls
setlocal enabledelayedexpansion
rem pushd path_to_files

for /f "tokens=1,2,3,4,5,6,7,8 delims=._-+" %%A in ('Dir /A-D /B 2*.mp3') do (
set _T1=%%F
set _T1=!_T1:~0,2!.!_T1:~2,2!.!_T1:~4,2! rexx!_T1:~6,4!
echo ren "%%A-%%B-%%C_%%D_%%E+%%F.%%G" "%%A.%%B.%%C.%%D.%%E%%F rexx.%%G"
rem "%%A-%%B-%%C" "!_T1! time !_T2!"
)
PopD

for /f "tokens=1,2,3,4 delims= " %%A in ('Dir /A-D /B *.jpg') do (
set _T1=%%A
set _T1=!_T1:~0,4!.!_T1:~5,2!.!_T1:~8,2!
set _T2=%%B
set _T2=!_T2:~0,2!.!_T2:~3,2!.!_T2:~6,2! pix!_T2:~8,4!
echo ren "%%A %%B" "!_T1! time !_T2!"
)
PopD

for /f "tokens=1,2,3 delims=-" %%A in ('Dir /A-D /B recording-*.mp3') do (
set _T1=%%B
set _T2=%%C
set _T1=!_T1:~0,4!.!_T1:~4,2!.!_T1:~6,2!
set _T2=!_T2:~0,2!.!_T2:~2,2!.!_T2:~4,2! rexx!_T2:~6,4!
echo ren %%A-%%B-%%C "!_T1! time !_T2!"
rem "%%A-%%B-%%C" "!_T1! time !_T2!"
)
PopD

for /f "tokens=1,2,3,4,5,6,7 delims=-" %%A in ('Dir /A-D /B *.mp4') do (
set _T1=%%G
set _T1=!_T1:~0,2! vidz!_T1:~2,4!
echo ren %%A-%%B-%%C-%%D-%%E-%%F-%%G "%%B.%%C.%%D time %%E.%%F.!_T1!"
)
PopD

pause


----------



## Squashman (Apr 4, 2003)

We do have Bulletin Board CODE TAGS.

Not how I would have done it but whatever works for you.


----------



## Squashman (Apr 4, 2003)

14 lines of code to your 28.

```
@echo off
setlocal enabledelayedexpansion

FOR %%G in (*.jpg *.mp3 *.mp4) do (
	set filename=%%~nG
	set filename=!filename:.=!
	set filename=!filename:-=!
	set filename=!filename:_=!
	set filename=!filename: =!
	set filename=!filename:time=!
	set filename=!filename:video=!
	set filename=!filename:recording=!
	set filename=!filename:OutgoingTelephoneCall= Outgoing Telephone Call !
	rename "%%G" "!filename:~0,4!.!filename:~4,2!.!filename:~6,2! time !filename:~8,2!.!filename:~10,2!.!filename:~12!%%~xG"
)
```
Output

```
C:>dir /a-d /b
2012-02-01 09.07.52.jpg
2012-02-01 time 08_20_10 Outgoing Telephone Call +19091234567.mp3
recording-20111231-190945.mp3
rename.bat
video-2012-01-31-15-12-02.mp4

C:>rename.bat

C:>dir /a-d /b
2011.12.31 time 19.09.45.mp3
2012.01.31 time 15.12.02.mp4
2012.02.01 time 08.20.10 Outgoing Telephone Call +19091234567.mp3
2012.02.01 time 09.07.52.jpg
rename.bat

C:>
```


----------



## Squashman (Apr 4, 2003)

Just realized I could shorten this code up by another six lines.


----------

