Copy and rename with BAT File

jarett

Board Regular
Joined
Apr 12, 2021
Messages
179
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have read a lot of post on this and solved one of my problems for copying and rename a file with the following code
Code:
xcopy "M:\abc.csv" "O:\File"
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
rename "O:\File\abc.csv" "x_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%.csv"

My output on this is file name = x_YYYY-MM-DD_TTTT.csv, I don't understand how all the variables work, but I need to get this bat file to rename abc.csv file to something like "MM-DD-YYYY-#XXX.csv". The "#" would be an incremental value depending on how many files are renamed in a day(needs to start over the next day), the "XXX" would just be a set text value for every file. The abc.csv will have the same name every day. I started playing with the script but I'm getting stuck. I know I am missing the incremental part.

Code:
xcopy "M:\abc.csv" "P:\File"
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
rename "P:\File\abc.csv" "%date:~4,2%-%date:~7,2%-%date:~10,4%XXX.csv"

And the last question which will come up down the road, in the folder that I am copying and renaming, they will have 3 files (abc.csv, def.csv, hij.csv) all 3 will be copied and renamed. Same date and incremental format but with different "XXX" text. Do you have all this in one bat file (I am assuming you cant accommodate all of this in the same script, you would have to do separate lines) or do you break it up into 3 different bat files?
 
(sorry, I can't edit the posts ... so I add the ideas as they come to me)

The part that does the math I have done simple but it can be complex. For example, you can count based on the string XXX (hence the file name) and the date. How do you want the count to be? Is the rename part okay?
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
There is a 1 before and after abc. Isn't that exactly what you expected - count variable twice in the file name?
Yes that is but when I run the bat again, it saves the same name it doesn’t increment the two numbers. Which on the original files it started doing this as well just saving the same name and not incrementing.
 
Upvote 0
(sorry, I can't edit the posts ... so I add the ideas as they come to me)

The part that does the math I have done simple but it can be complex. For example, you can count based on the string XXX (hence the file name) and the date. How do you want the count to be? Is the rename part okay?
I want the count to increment each time I move and save the abc.csv file through the bat. I’m struggling to find the rename part in your code.
 
Upvote 0
I think I am seeing part of my problem for the recount issue, the %today% files or on the network and not on the C: drive. So I need to change directories, what do you use as a wildcard character to specific which files? I can't leave this line because it pulls all the files, I need it to pull just the "abc" and not the "def" ending filenames. move P:\Jarett\SM_TEST\IN\%today%* temp I tried move P:\Jarett\SM_TEST\IN\%today%*abc temp but I don't think it worked.
 
Upvote 0
Alright figured it all out, with the help of everyone, appreciate the time, learned a lot from this project and constant testing and playing around. Just had to adjust to this code P:\Jarett\SM_Test\IN\%today%??abc.csv temp
 
Upvote 0
Alright figured it all out, with the help of everyone, appreciate the time, learned a lot from this project and constant testing and playing around. Just had to adjust to this code P:\Jarett\SM_Test\IN\%today%??abc.csv temp
You said the name of your source file is abc.csv. Now, it is preceded with date?
 
Upvote 0
You said the name of your source file is abc.csv. Now, it is preceded with date?
With the date and the incremental number assigned depending on how many files already exist in the folder.
 
Upvote 0

Forum statistics

Threads
1,224,012
Messages
6,175,931
Members
452,686
Latest member
midhunjoseph

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