Batch file command to rename file to randomly chosen filename

BlackieHamel

Board Regular
Joined
May 9, 2014
Messages
93
This is only tangentially related to Excel. I have a batch file that runs Excel and opens the file temp.csv:

Code:
[FONT=arial]start[/FONT][FONT=arial] C:\Program" "Files\Microsoft" "Office" "15\root\office15\EXCEL.EXE "d:\downloads\temp.csv[/FONT]

This works fine, but sometimes I want to do this multiple times, and I get a filename collision if I try to run this batch command again. My workaround is to rename temp.csv while I'm in Excel. Sometimes I forget, though!

Can I add a batch file command to rename temp.csv to something random, such as a filename containing the current time, and then start Excel and open that file?

Thanks!

Blackie
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here is one way to get date/time stamps and rename your file:
Code:
C:
CD\
FOR /F "TOKENS=2,3,4 DELIMS=/ " %%I IN ('DATE/T') DO SET D=%%K%%I%%J
FOR /F "TOKENS=1,2,3 DELIMS=: " %%I IN ('TIME/T') DO SET T=%%I%%J%%K

copy d:\downloads\temp.csv d:\downloads\temp_%d%%t%.csv
del d:\downloads\temp.csv
 
Upvote 0
Solution
Here is one way to get date/time stamps and rename your file:
Code:
C:
CD\
FOR /F "TOKENS=2,3,4 DELIMS=/ " %%I IN ('DATE/T') DO SET D=%%K%%I%%J
FOR /F "TOKENS=1,2,3 DELIMS=: " %%I IN ('TIME/T') DO SET T=%%I%%J%%K

copy d:\downloads\temp.csv d:\downloads\temp_%d%%t%.csv
del d:\downloads\temp.csv

Thanks, Joe4! Exactly what I needed. Works like a charm.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top