Copy and rename with BAT File

jarett

Board Regular
Joined
Apr 12, 2021
Messages
165
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?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I posted this question on the following site as well this morning, Copy and Rename with Bat File. They have a good bit of post about this topic, I just can't seem to get it to click on how to do it yet.
 
  • Like
Reactions: yky
Upvote 0
I don't see any useful suggestion there. Maybe you want to post another question asking only how to get the maximum number from file names and increment it by one.
 
Upvote 0
I don't see any useful suggestion there. Maybe you want to post another question asking only how to get the maximum number from file names and increment it by one.

This should work to count the .csv files:
Code:
set count=0 & for %x in (*.csv) do @(set /a count+=1 >nul)
set count=00%count%
set count=%count:~-3%

And in action. (Of course, if you set count=1 initially it will be one more than the number of .csv files, which is the next integer for your new file for renaming)
1619679595470.png
 
  • Like
Reactions: yky
Upvote 0
That's great. However, the OP seems to have files of all different dates in the same directory. So, what's needed is not to count the total number of .csv files but to count .csv files for each date.
 
Upvote 0
Well, why not move all .csv files of the current day to a temporary subfolder, count them and increment, then move them back. Like this:
Code:
rem Presumes %date% returns Ddd DD/MM/YYYY so may need adjusting if American format
set today=%date:~7,2%-%date:~4,2%-%date:~10,4%
echo %today%
echo "Moving today's files to temp"
mkdir temp
move %today%* temp
move abc.csv temp
cd temp
set count=0
for %%x in (*.csv) do (set /a count+=1)
set count=00%count%
set count=%count:~-3%
rename abc.csv %today%-#%count%.csv
move *.csv ..
Worked on my test files:
1619691174005.png
> output >
1619691199232.png

Of course, you could check for whether temp dir exists, and I think I missed the HHMMSS part, but this should solve your incremental renaming requirement and work with a little adjustment.
 
  • Like
Reactions: yky
Upvote 0
Well, why not move all .csv files of the current day to a temporary subfolder, count them and increment, then move them back. Like this:
Code:
rem Presumes %date% returns Ddd DD/MM/YYYY so may need adjusting if American format
set today=%date:~7,2%-%date:~4,2%-%date:~10,4%
echo %today%
echo "Moving today's files to temp"
mkdir temp
move %today%* temp
move abc.csv temp
cd temp
set count=0
for %%x in (*.csv) do (set /a count+=1)
set count=00%count%
set count=%count:~-3%
rename abc.csv %today%-#%count%.csv
move *.csv ..
Worked on my test files:
View attachment 37751 > output > View attachment 37752
Of course, you could check for whether temp dir exists, and I think I missed the HHMMSS part, but this should solve your incremental renaming requirement and work with a little adjustment.
To test this code do I just put echo in front of the whole code or the 2 echo's in the code create the test? I need to change a few things to see if it works.
 
Upvote 0
I tried simplifying the code and when I ran it, the destination folder that had the 04-29-2021#001.csv file produced the next file as abc.csv, is this because I removed the part of moving the file to a temp folder? I might not have explained my self well enough abc.csv will be moved to new folder and renamed 04-29-2021#001.csv, on the same day I might need to move abc.csv(new version saved over last version) again and the new file in the destination needs to be 04-29-2021#002.csv.
Code:
xcopy "M:\TEST\abc.csv" "P:\Jarett\"
rem Presumes %date% returns Ddd DD/MM/YYYY so may need adjusting if American format
set today=%date:~4,2%-%date:~7,2%-%date:~10,4%
set count=0
for %%x in (abc.csv) do (set /a count+=1)
set count=00%count%
set count=%count:~-3%
rename "P:\Jarett\abc.csv" "%today%-#%count%.csv"
 
Upvote 0
Added back the move to temp folder and when I run the bat file it just saves the file as "abc.csv" in the final destination folder still. So it is copying the file correctly just not renaming. Is this because the file it is renaming is the same name everytime (abc.csv)? Am I going to have to use that subfolder for more than just a temp? I could possibly make the original file different, it is an export from access and it just writes over the existing abc.csv file.
 
Upvote 0
Added back the move to temp folder and when I run the bat file it just saves the file as "abc.csv" in the final destination folder still. So it is copying the file correctly just not renaming. Is this because the file it is renaming is the same name everytime (abc.csv)? Am I going to have to use that subfolder for more than just a temp? I could possibly make the original file different, it is an export from access and it just writes over the existing abc.csv file.
Would you post the code you tested? Seems you have modified the code somewhat.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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