0

So I got my answer to the

How to create a folder with current date in its name and copy to it all files and folders from some path on Windows 10

question, but as it was a duplicated issue and I am a newbie on this forum- it was closed and I am unable to ask additional question in similar topics

By using this How to create a subfolder based on current date? solution I was able to achieve what I wanted [thank you very much user compo]

And so this code is working A-OK:

@Echo Off
Set "sd=C:\Users\YOUR USER NAME\AppData\Roaming\Mozilla\Firefox\Profiles\fsj89244"
Set "dd=D:\Backup Copies\Firefox\Profile"
Set "ds="
If Not Exist "%sd%\" Exit /B
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
RoboCopy "%sd%" "%dd%\%ds%" /E

It creates a sub-folder with a date [on drive D] in its name and then copies to it all content from a specified path [from the drive C]. This speeds up my manual creation of backup copies

But if the script is executed again I would need it to check first if such sub-folder exists. And if yes then leave it alone and create a folder with name in date format >>YYYY MM DD v2<<; and the next time >>YYYY MM DD v3<< etc. which would stop happening when the date in the operating system would change to a next day

  • Hi Pozytron. Welcome to SO. SO is not a code writing site and this question isn't applicable here. Try editing your question to add more detail like the code you have tried so far and what went wrong with those. See [What shouldn't I ask?](https://stackoverflow.com/help/dont-ask), [What can I ask?](https://stackoverflow.com/help/on-topic), [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask), and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Nico Nekoru Apr 14 '21 at 14:46
  • You have asked two questions that each have completely different logic. Which question do you want answered? 1) How to check for the existence of folder and delete it? 2) How to check for the existence of versioned folders and create a new one with a new version number? – Squashman Apr 14 '21 at 15:34
  • 2
    `either delete it [...] or [...] just create another version [...]?` Are you asking us which you should choose? That's opinion based and the best person to answer that is you (as you have all the relevant information). – Stephan Apr 14 '21 at 17:40
  • Thank you for your thorough input I wrote >> OR << because I have no idea if either of two options are possible at all - and SO either of them I deemed as good enough. I just made an edit in accordance to what you wrote – Elerium-115 Apr 15 '21 at 16:33
  • both is possible. Delete it with `rd /s /q "%ds%"`. For versioning, see my answer. – Stephan Apr 15 '21 at 18:14

1 Answers1

0
Set "ds="
For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'
) Do If Not Defined ds Set "ds=%%A %%B %%C"
If Not Defined ds Exit /B
set ver=0
for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1
set "newFolder=%ds% v%ver%"
md "%newFolder: v0=%"

dir /ad /on will sort the folders by name and the for ends with the last version plus one (The obvious for /d %%a in ("%ds%*") do set /a ver+=1 just counts the found folders and so will be incorrect if a folder is missing for any reason and probably will use an already "occupied" folder)

The first folder of a day will be in the format YYYY MM DD, all further folders will be in the format YYYY MM DD vX. If you want the first folder to be the same format (... v0), use just md "%newFolder%.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you. This work, i.e. it creates a folder in format YYYY MM DD vX, where X is a consecutive number But how do I merge this with the previous code? I need the BAT to first take care of the creating of a properly named folder - and then copy to that newly created folder content of some other folder [the Firefox profile from C], no matter how that new folder was named – Elerium-115 Sep 24 '21 at 11:20
  • I don't understand. You created the folder with the help of a variable. Just use that variable. – Stephan Sep 25 '21 at 10:25
  • I do not know how. I tried merging the two scripts into for example this @echo off Set "sd=C:\Users\YOUR-USER-NAME\AppData\Roaming\Mozilla\Firefox\Profiles\MatrixOnFire" Set "dd=D:\Backup Copies\Firefox\Profile" Set "ds=" For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null' ) Do If Not Defined ds Set "ds=%%A %%B %%C" If Not Defined ds Exit /B set ver=0 for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1 set "newFolder=%ds% v%ver%" md "%newFolder: v0=%" RoboCopy "%sd%" "%dd%\%ds%" /E but it did not work. All I get are empty folders – Elerium-115 Sep 29 '21 at 10:46
  • You created a folder named `%newFolder:v0=%` Why don't you use it as your destination? – Stephan Sep 30 '21 at 09:57
  • How exactly do I do that? – Elerium-115 Oct 01 '21 at 11:28
  • Well, can anyone help me out with this merging of this two codes? – Elerium-115 Jun 02 '22 at 13:28
  • Your new folder is `"%newFolder: v0=%"`, so just use it as the destination with the `robocopy` command. If that doesn't answer your question, you have to be more specific. – Stephan Jun 02 '22 at 14:36
  • Once again: I do not know how The code that I will paste here as a comment in two parts [because it is too long] just creates folders \ 2022 06 13 \ 2022 06 13 \ 2022 06 13 \ \ 2022 06 13 v1 with the middle one holding copied content `Set "ds="` `For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'` `) Do If Not Defined ds Set "ds=%%A %%B %%C"` `If Not Defined ds Exit /B` `set ver=0` `for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1` `set "newFolder=%ds% v%ver%"` `md "%newFolder: v0=%"` – Elerium-115 Jun 13 '22 at 13:53
  • `Set "sd=C:\!test"` `Set "dd=%newFolder:v0=%"` `Set "ds="` `For /F "Tokens=1-3Delims=/ " %%A In ('RoboCopy/NJH /L "\|" Null'` `) Do If Not Defined ds Set "ds=%%A %%B %%C"` `If Not Defined ds Exit /B` `set ver=0` `for /f "tokens=3,4 delims=v " %%a in ('dir /ad /on /b "%ds%*"') do set /a ver=%%b+1` `set "newFolder=%ds% v%ver%"` `md "%newFolder: v0=%"` `RoboCopy "%sd%" "%dd%\%ds%" /E` – Elerium-115 Jun 13 '22 at 13:53
  • I tried with my very limited understanding of what those lines of codes mean. In result I ended up creating a folder with date in its name that hosted a sub-folder with the same date. And that sub-folder was unmovable and immune to any change because of a "system call level is not correct" error. That folder could only be copied - thus creating another unmovable one – Elerium-115 Jun 26 '22 at 22:17
  • And having found very little information about that "system call level is not correct" error on the Internets I tried multiple tricks and programs to remove it. After almost giving up and thinking that I will have to remove a volume from drive hosting that folder I remembered that in the past I had some similar unable-to-do-anything-with-a-file issue I run 7-zip 19.0 x64. And only then was I able to first rename it and then delete it. So I do not think that I will be able to use it as my destination; in a correct way that is. I' am just not a programmer, just a small time code tweaker – Elerium-115 Jun 26 '22 at 22:18