What I'm trying to do:
I have X amount of folders in a directory, all of which need to be appended with '.r' at the end - but this must be done with ~30 seconds in between each file name edit. I.E. When I start the script I need folder1, folder2, and folder3 to go through this process: "folder1.r" 30 seconds pass "folder2.r" another 30 seconds "folder3.r" etc.
I've tried cobbling together my own batch script for this, and I've gotten it mostly working, but it seems to not always start at the first folder (alphabetically sorted by default) - primarily, it'll start from where the script last left off, despite me having closed the command window.
What I've got so far:
for /d %%d in (*) do (
ren "%%d" "%%d.r"
timeout 3
)
Currently, this renames any folder in the directory to add the .r suffix. But since I've run the batch file a few times, it doesn't start off at folder1, it starts off at folder'X' where X would have been the next folder the last run of the batch would've renamed, thus the beginning folder on each run is a revolving number. If I stopped the last test batch at, say, renaming to folder20.r, then the next run of the batch starts with folder21.
Long story short, my current folder structure is as follows:
MainFolder
Today Print001
imageA.jpg
imageB.jpg
imageC.jpg
Today Print002
imageD.jpg
imageE.jpg
imageF.jpg
Today Print003
imageG.jpg
imageH.jpg
imageI.jpg
[...]
Today Print098
imageR.jpg
imageS.jpg
imageT.jpg
Today Print099
imageU.jpg
imageV.jpg
imageW.jpg
Today Print100
imageX.jpg
imageY.jpg
imageZ.jpg
And my intention is to use this batch script to rename each print folder, with a 30-second pause between each name change, to become:
MainFolder
Today Print001.r
imageA.jpg
imageB.jpg
imageC.jpg
Today Print002.r
imageD.jpg
imageE.jpg
imageF.jpg
Today Print003.r
imageG.jpg
imageH.jpg
imageI.jpg
[...]
Today Print098.r
imageR.jpg
imageS.jpg
imageT.jpg
Today Print099.r
imageU.jpg
imageV.jpg
imageW.jpg
Today Print100.r
imageX.jpg
imageY.jpg
imageZ.jpg
Of note, folder names will definitely be alphanumeric, may have spaces in them, but shouldn't have any symbols (that I can foresee.)
Bonus points if there's a way to have it only affect folder names that begin with, say, "bat" but this would just be icing on the cake. I haven't gotten that far into the process to research how to make that happen, and I want to be clear, I'm not asking anyone to build this entire thing for me!
CodePudding user response:
My test from a command prompt
C:
cd C:\StackOverFlow\Testing
for /f "delims=" %d in ('dir /b /ad bat*') do (ren "%d" "%d.r"&timeout 3)
dir /b /ad bat* Will get all of the folders that start with bat in the current working directory
& will allow you multiple commands on a single line
%% should be used in a batch file instead of a single %.
CodePudding user response:
This can be used in a batch-file
run under cmd
. Note that there is a check to ensure that previously existing directory names ending in ".r" are not renamed again. If you are on a supported windows
system, powershell
is available. When you are confident that the directories will be renamed correctly, remove the -WhatIf
switch from the Rename-Item
command.
powershell.exe -NoLogo -NoProfile -Command ^
Get-ChildItem -Directory -Path 'C:\src\t' ^| ^
ForEach-Object { ^
if ($_.Name -notmatch '.*\.r$') { ^
Rename-Item -Path $_.FullName -NewName ($_.Name '.r') -WhatIf; ^
Start-Sleep -Seconds 3 ^
} ^
}