Solved Batch File to move and Rename files

March 12, 2014 at 05:51:02
Specs: Windows 7
Hi,

I have no batch file experience, but I need a batch file that will rename & copy/move files.. When the files have been copied, delete the original.
The new name would be the original name + a date/time stamp


See More: Batch File to move and Rename files

Report •


#1
March 12, 2014 at 15:53:52
✔ Best Answer
It's mostly just building your date-time element:
@echo off & setlocal
set dtst=%date:~4%_%time:~0,8%
set dtst=%dtst:/=-%
set dtst=%dtst::=_
echo date-timestamp looks like: %dtst%
pause
:: edit: foll. line had unmatched, and misplaced quotes. this is fixed:
for %%a in (*.*) do move "%%a" "e:\wherever\%%~na_%dtst%%%~xa%"
::==== end batch
If you look examine lines 2,3 and 4, you will see what they do. First line 2 assembles substrings of date and time (date from the 4th character on, time from 1st thru 8th).
Then 3 replaces the / in the date with -, then 4 replaces the colons in the time with underscores. Understanding this, you can experiment around with whatever format you actually want. You should run the batch from the directory that has the files, preferably from the command prompt (running batch from icons almost always results in confusion and obfuscation.)

message edited by nbrane


Report •

#2
March 14, 2014 at 12:35:44
Thanks, this looks great.
1 silly question, where do I put the "from" file path?

Report •

#3
March 14, 2014 at 19:43:16
Ok, this:
"You should run the batch from the directory that has the files, preferably from the command prompt..."
Obviates the need for any path, but two other ways to run from anywhere:
1) pushd "c:\where my files are"
blah blah (script content as given)
popd

OR
2)
:: make this line two:
set fromfiles="c:\where my files are"
:: then change this line:
for %%a in (%fromfiles%\*.*) do move "%%a" "e:\wherever\%%~na_%dtst%%%~xa%"

Note that I also fixed my dumb mistake with the quotes (see edited resp. #2)


Report •
Related Solutions


Ask Question