0

I'm trying to rename the latter part of a file, before the extension, in a batch script.
For instance:

block1.txt --> block1-%mydate%-%mytime%.txt

block2.zip --> block2-%mydate%-%mytime%.txt

The file name is passed to the program as %1. Only one file name will be changed per run. The program is intended to append a time-stamp to the end of a file, in the form of MMDDYYYY-HHMM.

The first part of the program produces the expression %mydate%-%mytime%.

I can't for the life of me figure out how to do it in a generic and consistently functional way.

Any help?

Artemix
  • 2,113
  • 2
  • 23
  • 34
  • What exactly is not working? Computation of date and time strings? or parsing out the name components? – dbenham Sep 04 '12 at 08:54

2 Answers2

1

For Windows here is what @hobodave answered to a similar question:

For command-line

for /F %i in ("c:\foo\bar.txt") do @echo %~ni

output: bar

For .bat Files

set FILE=bar
for /F %%i in ("%FILE%") do @echo %%~ni

output: bar

Further Reading:

http://www.computerhope.com/forhlp.htm

For Unix

You can use the basename command. It will clear path and extension from a given path.

basename /foo/bar/arch.zip .zip

Will output

arch

Basename manual: http://unixhelp.ed.ac.uk/CGI/man-cgi?basename

Community
  • 1
  • 1
Desislav Kamenov
  • 1,193
  • 6
  • 13
1

Did you mean: FileName-MMDDYYYY-HHSS.*

for /f "tokens=2-7 delims=/:. " %%a in ("%date% %time: =0%") do set newFileName=%~n1-%%a%%b%%c-%%d%%f%~x1
ren "%~1" "%newFileName%"

Or did you mean: FileName-MMDDYYYY-HHMM.*

for /f "tokens=2-6 delims=/:. " %%a in ("%date% %time: =0%") do set newFileName=%~n1-%%a%%b%%c-%%d%%e%~x1
ren "%~1" "%newFileName%"
James K
  • 4,005
  • 4
  • 20
  • 28