问题
I'm a novice at batch sripting. I've been using Robocopy in a batch file (that calls a PowerShell script) to do various tasks that have made work life much more efficient. Now I need to rename a folder with a different name and increment that different name by 1 for each time the batch script is executed. Spacing is important because I have software that will not read the path if it is underscored between the naming.
C:\Users\Desktop\Source\
What exists in this path is a folder sequence like this:
Folders Now
Folders Now Previous 1
Folders Now Previous 2
I've been trying to find a batch script that will rename Folders Now to Folders Now Previous 'next increment', but I've had no luck. I've tried various scripts that I've found and they work to a certain degree, but none of them do exactly this. Spacing between the naming is important (there can't be Folders_Now_Previous_3), and that's where the other scripts fail.
I've been using a batch script that will look at the highest last number and increment that name by 1. That works, but the naming parameter (source name and new name) aka set "baseName= are the same and they need to be different, and it also involves underscores and that's a no-go for the legacy software.
EDIT: I've been using this script from an older post in this forum:
@echo off
setlocal enableDelayedExpansion
set "baseName=Folders_Now_Previous_"
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%baseName%=!"
if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
When this script is executed, it returns this:
Folders_Now_Previous_1
Folders_Now_Previous_2
Folders_Now_Previous_3
What I've been trying to do is modify it so that the output doesn't contain underscores. It should output this:
Folders Now Previous 1
Folders Now Previous 2
Folders Now Previous 3
My attempts at modifying it will work the first time but it will stop working after the first folder is renamed.
来源:https://stackoverflow.com/questions/55151232/batch-script-to-rename-folder-to-a-new-name-and-increment-new-name