[SOLVED] Creating multiple copies of a whole folder in folders with consecutive date and time

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

[SOLVED] Creating multiple copies of a whole folder in folders with consecutive date and time

#1 Post by DOSadnie » 21 Jul 2022 15:41

Hello

This is my first post this forum and I am just just a small time code tweaker- thus I need help



I want to be able to simply execute a BAT file residing in any folder in order to copy whole content of the folder containing [e.g.] my Firefox profiles to another drive. But I also need following copies created in the same day to be distinguishable with something like "V02","V03", "V04" etc. added to the end of their names if I execute such BAT file for the 2nd, 3rd, 4th etc. time. So if I will execute that BAT all in all 7 times over the course of 3 days, I need the folders with backups to present themselves like this:

2022 08 01
2022 08 01 V02
2022 08 01 V03
2022 08 01 V04
2022 08 01 V05
2022 08 02
2022 08 02 V02
2022 08 03


Alternatively they can look like this:

2022 08 01
2022 08 01 09.00.00
2022 08 01 12.30.05
2022 08 01 19.22.17
2022 08 01 23.11.56
2022 08 02
2022 08 02 00.25.43
2022 08 03


i.e. utilize time

But in both cases, as it is shown above, I would need the first copy to not have such an addition / marker. And so it [i.e. "V01" or time] would either have to not be added / created in the first place - or - in case if there is no other way, it would have to be somehow automatically removed by renaming such lonely folder [i.e. a folder that has a unique date because there are no other folders present with such date in the main folder holding those backups]


All I have now is something like this

Code: Select all

@Echo Off
Set "sd="C:\Users\YOUR-USER-NAME\AppData\Roaming\Mozilla\Firefox\Profiles"
Set "dd="Z:\Backup Copies\Firefox\Profiles""
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
and this

Code: Select all

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=%"
Unfortunately when I tried merging these two codes together I ended up creating some folders with dates as their names- and they were holding sub-folder named in a same way. And that sub-sub-folder I was then unable to delete in a normal way because of the "system call level is not correct" error popping-up when I was attempting a deletion. [In the end, after using every other trick I could think of, aside from removing a volume from a dive, I managed to delete such ill folder by using the navigation feature of 7zip]
Last edited by DOSadnie on 02 Jul 2023 07:08, edited 1 time in total.

DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#2 Post by DOSadnie » 11 Aug 2022 14:03

For the time being I am using the free NirCmd by NirSoft with in form of a LNK file Start in set to

Code: Select all

"C:\Program Files\NirCmd"
and its Target set to

Code: Select all

"C:\Program Files\NirCmd\nircmd.exe" shellcopy "C:\Users\YOUR-USER-NAME\AppData\Roaming\Mozilla\Firefox\Profiles\*.*" "Z:\Backup Copies\Firefox\Profiles\~$currdate.yyyy MM dd$ - ~$currtime.HH.mm.ss$" yestoall noerrorui
which produces folders like

2022 08 01 07.45.12
2022 08 01 09.08.59
2022 08 01 15.03.22
2022 08 01 20.14.21
2022 08 01 23.59.58
2022 08 02 01.05.03
2022 08 02 23.45.02
2022 08 03 00:00:50



But there are three drawbacks with this method:

#1] The objective one: the path in Target cannot exceed the 260 characters limit

#2] The subjective one: I have later on to remove the time info in from folder name of the one that choose to keep for archive reason. [And that is why my backups were to start adding consecutive numbers from V2 and not from V1, because in most case I would have kept the first one from a given day- i.e. the one with just a date thus without e.g V4 added to its name; and so I would not have to rename anything but only delete the excess backups]

#3] The curious one: after closing Firefox I need to wait out some time before executing this LNK, because when clicked hasty it will not copy all of the content of profiles of Firefox. And this is curious because if I perform the copy process by using that build in Windows Explorer tool or TeraCopy, then all content is copied without a sweat. I tried contacting the developer about this but he says he does not have time to look into this issue



And so:

1] I would still like to be able to use BATs for coping. Does anyone know how to do what NirCmd does?

2] How can I overcome the 260 character limit for paths? I already added to my Regirsty

Code: Select all

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
"LongPathsEnabled"=dword:00000000
which was suppose to remove it. But overall it only helped me a little bit - [long story short] by allowing me to create longer filenames with a help of a certain BAT script, which prior to implementing this REG hack that BAT was ignoring commands when an overall path was somewhat too long for it [but nevertheless not exceeding 260 character path limit]

betapolling
Posts: 2
Joined: 11 Aug 2022 14:22

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#3 Post by betapolling » 11 Aug 2022 21:06

#3] The curious one: after closing Firefox I need to wait out some time before executing this LNK, because when clicked hasty it will not copy all of the content of profiles of Firefox. And this is curious because if I perform the copy process by using that build in Windows Explorer tool or TeraCopy, then all content is copied without a sweat. I tried contacting the developer about this but he says he does not have time to look into this issue
Firefox profiles may over 2000 of files in those directories. My own experience tell me that copying around such a number of files all the time will degrade much sooner your hard disk. Also check for Firefox update service, Firefox task scheduler job and look at task manager if Firefox really closes. Check Unlocker, iobit unlocker, Process Monitor (aka. filemon) if some files are open. If neither helps run a chkdsk disk check and see your event log. See the whole structure thought Total Commander will help to find hardlinks etc.

DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#4 Post by DOSadnie » 30 Aug 2022 11:34

betapolling wrote:
11 Aug 2022 21:06
[...]
check
[...]
if Firefox really closes. Check Unlocker, iobit unlocker, Process Monitor (aka. filemon) if some files are open
[...]
I used Process Explorer by Sysinternals which allows for sorting by Company Name - and apparently it shows exactly the same entries as Task Manager of Windows 10. But this is not a case of some hidden processes - as the culprit is in plain sight: and it is this NirCmd that I am forced to use as a workaround solution

Yes- the processes named firefox.exe are many and they do close in groups exercising a time delay, sometimes taking up to 10 seconds all together. But
DOSadnie wrote:
11 Aug 2022 14:03
[...]
this is curious because if I perform the copy process by using that build in Windows Explorer tool or TeraCopy, then all content is copied without a sweat
[...]
without having to wait for some cool down of Firefox - i.e. whatever items are present in

C:\Users\YOUR-USER-NAME\AppData\Roaming\Mozilla\Firefox\Profiles\

then they are copied [while NirCmd picks up just a minority of them on the account of an unknown reason]

DOSadnie wrote:
11 Aug 2022 14:03
And so:

1] I would still like to be able to use BATs for coping. Does anyone know how to do what NirCmd does?
[...]
[So that I could stop using NirCmd which is unable to execute coping process 100% correctly for 100% of the time when it is used]

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#5 Post by aGerman » 02 Sep 2022 11:36

From my understanding you just have some difficulties to create the folder name?

Code: Select all

@echo off &setlocal
set "sd=%appdata%\Mozilla\Firefox\Profiles"
set "dd=Z:\Backup Copies\Firefox\Profiles"

if not exist "%sd%\" exit /b

for /f %%i in ('wmic os get LocalDateTime /value') do for /f "tokens=2 delims==" %%j in ("%%i") do set "ds=%%j"
set "ds=%ds:~,4% %ds:~4,2% %ds:~6,2%"
set "version="
if exist "%dd%\%ds%*" for /f "tokens=*" %%i in ('dir /b /ad /on "%dd%\%ds%*"') do for /f "tokens=1* delims=V" %%j in ("%%~nxi") do set /a "version=1%%k+1"
if defined version if "%version%"=="2" (set "ds=%ds% V02") else set "ds=%ds% V%version:~-2%"

robocopy "%sd%" "%dd%\%ds%" /e
Steffen

DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#6 Post by DOSadnie » 03 Jan 2023 04:10

aGerman wrote:
02 Sep 2022 11:36
[...]
difficulties to create the folder name?
[...]
Yes. And I finally got the time to test this code of yours thoroughly and it seems to work A-OK, so thank you very much

But if I wanted to replace V02, V03, V04 etc. with current time stamps in format hh.mm.ss [also preceded with a pause], then how would this script have to be adjusted?

DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#7 Post by DOSadnie » 28 May 2023 09:03

DOSadnie wrote:
03 Jan 2023 04:10
[...]
But if I wanted to replace V02, V03, V04 etc. with current time stamps in format hh.mm.ss [also preceded with a pause], then how would this script have to be adjusted?
Well, can anyone help me with this?

DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Creating multiple copies of a whole folder in folders with consecutive date and time

#8 Post by DOSadnie » 02 Jul 2023 07:07

It seems that such two lines will do the job

Code: Select all

for /f "tokens=2 delims==" %%i in ('wmic os get LocalDateTime /value') do set "ds=%%i"
set "ds=%ds:~,4% %ds:~4,2% %ds:~6,2% %ds:~8,2%.%ds:~10,2%.%ds:~12,2%"
hence the alternative version of this script can will look in its entirety like this

Code: Select all

setlocal
set "sd=C:\q"
set "dd=S:\Configs\q"

if not exist "%sd%\" exit /B

for /f "tokens=2 delims==" %%i in ('wmic os get LocalDateTime /value') do set "dt=%%i"
set "date=%dt:~0,4% %dt:~4,2% %dt:~6,2%"
set "time=%dt:~8,2%.%dt:~10,2%.%dt:~12,2%"


set "version="
if exist "%dd%\%date%*" (
    for /f "tokens=*" %%i in ('dir /b /ad /on "%dd%\%date%*"') do (
        for /f "tokens=1* delims=V" %%j in ("%%~nxi") do set /a "version=1%%k+1"
    )
)

if not defined version (
    set "folder_name=%date%"
) else (
    set "folder_name=%date% %time%"
)

robocopy "%sd%" "%dd%\%folder_name%" /E
So thank you for your help; case closed

DOSadnie
Posts: 144
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: [SOLVED] Creating multiple copies of a whole folder in folders with consecutive date and time

#9 Post by DOSadnie » 08 Dec 2024 12:46

I have made a significant update to this script

It now takes care of the issue: what if the copying process is interrupted [e.g. with a sudden system crash] - and thus the user is left with a folder that supposedly contains a backup [as indicated by its name] but in reality has incomplete content? This new version creates notes in form of extension-less files that inform accordingly about occurred events

It also makes the date and time reported by properties of the newly created sub-folder which holds the copied content to have correct / current value [instead of those of the last item created or modified in the source folder]

Code: Select all

@echo off
echo.
echo.   RE-OPENING MINIMIZED...
echo.

if not "%1" == "min" start /MIN cmd /c %0 min & exit/b >nul 2>&1



setlocal
set "sd=C:\test - the source"
set "dd=C:\test - the backups"

if not exist "%sd%\" exit /B



for /f "tokens=2 delims==" %%i in ('wmic os get LocalDateTime /value') do set "dt=%%i"
set "date=%dt:~0,4% %dt:~4,2% %dt:~6,2%"
set "time=%dt:~8,2%.%dt:~10,2%.%dt:~12,2%"



set "version="
if exist "%dd%\%date%*" (
    for /f "tokens=*" %%i in ('dir /b /ad /on "%dd%\%date%*"') do (
        for /f "tokens=1* delims=V" %%j in ("%%~nxi") do set /a "version=1%%k+1"
    )
)



if not defined version (
    set "folder_name=%date%"
    echo > "%dd%\%date% %time%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"
) else (
    set "folder_name=%date% %time%"
    echo > "%dd%\%folder_name%\%date% %time%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"
)

echo > "%sd%\%date%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"


robocopy "%sd%" "%dd%\%folder_name%" /E /XF "%sd%\desktop.ini"



if not defined version (
    echo > "%dd%\%date%   (--- COPYING PROCESS WAS COMPLETED"
) else (
    echo > "%dd%\%date% %time%   (--- COPYING PROCESS WAS COMPLETED"
)

del "%sd%\%date%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"
del "%dd%\%date%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"
del "%dd%\%date% %time%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"
del "%dd%\%folder_name%\%date%   (--- ATTEMPTED EXECUTION OF BACKUP PROCESS"

del "%dd%\%folder_name%\%date%   (--- COPYING PROCESS WAS COMPLETED"



endlocal

Post Reply