1.

Solve : Overwriting Folders?

Answer»

Hi there

I have a code in my batch file which renames a folder if it exists

ren "\\%1\g$\Utilities\Mgt\Archives\Mgt%Date:~-10,2%%Date:~-7,2%%Date:~-4,4%" "Mgt-1%Date:~-10,2%%Date:~-7,2%%Date:~-4,4%"

I have set -1 in front of timestamp condition which will rename my above folder. If these files are renamed more then once a day then i'm wondering is there a code or suggestion that i can get from here which would change the renaming condition probabably pass the renaming condition through an array

I'm a newbie here so please question me again if this question doesn't make sense. Quote from: captedgar on December 17, 2009, 08:17:31 AM

"I have a code in my batch file which renames a folder if it exists."

ren "\\%1\g$\Utilities\Mgt\Archives\Mgt%Date:~-10,2%%Date:~-7,2%%Date:~-4,4%" "Mgt-1%Date:~-10,2%%Date:~-7,2%%Date:~-4,4%"

It would be difficult to rename a folder when it does not exist. ( the worse problem is an error message. )  ( fix the rename folder and then worry about the no folder error message. )

The one line of code above to rename a folder does not provide enough information  for us to work with.

Where is the folder?  What is the CURRENT name of the folder?  Are the folders overwritten automatically?  Must you save the information in the current folder?  Would a backup utility work?  Or plain copy or xcopy? It might be easier to work with the files inside the folder?

Please provide all the code for the "rename the folder process"If you WANT to rename the folder on the second run (MGT exists) do you want to retain the existing MGT-1 folder?

One standard way that is often used to avoid name clashes is to USE the epoch number - the number of seconds since 00:00:00 Jan 1st 1970 ZULU, ignoring leap seconds, example: 1261089513 (2009-12-17 22:38:33Z) This is also called "Unix Time" The STREAMING videos on the RTVE (Spanish TV) video on demand website use this as a filename but modify this by adding milliseconds to the end. Example: 1258808030808.flv However YYYYMMDDhhmmss or some variant should work OK.
alternative: if you can download GNU win32 tools (coreutils) (see my sig), you can create timestamp with epoch using date command
Code: [Select]C:\test>gnu_date +%Y%m%d%H%M%s%N
2009121816011261123265179033100
then you can rename your files uniquely. see gnu_date --help for more info.You can do datestamp & epoch stuff in Visual Basic Script too. And get at it in a batch

Code: [Select]
echo off
echo wscript.echo DateDiff("s", "01/01/1970 00:00:00", Now())>Now2Epoch.vbs
echo wscript.echo DateAdd("s", wscript.arguments(0), "01/01/1970 00:00:00")>Epoch2Date.vbs

for /f "delims=" %%E in ('cscript //nologo Now2Epoch.vbs') do set NowEpoch=%%E
echo Epoch number now : %NowEpoch%

REM and back the other way...

for /f "delims=" %%D in ('cscript //nologo Epoch2date.vbs %NowEpoch%') do set EpochDateTime=%%D
echo Date and time    : %EpochDateTime%


Date & time format returned by Epoch2Date will be in your local format settings.

Code: [Select]Epoch number now : 1261126966
Date and time    : 18/12/2009 09:02:46Now that Dias de verano has posted, the solution for Overwriting Folders
is perfectly clear.

We can tell how pleased the OP is by the feedback from the original poster.

All new readers can quickly overwrite their old folders.

p.s.

Why would anyone not understand:

"for /f "delims=" %%D in ('cscript //nologo Epoch2date.vbs %NowEpoch%') do set EpochDateTime=%%D
echo Date and time    : %EpochDateTime%" Quote from: billrich on December 18, 2009, 07:17:26 AM
Why would anyone not understand:

"for /f "delims=" %%D in ('cscript //nologo Epoch2date.vbs %NowEpoch%') do set EpochDateTime=%%D
echo Date and time    : %EpochDateTime%"

Indeed. Why not? It is actually fairly obvious.


Discussion

No Comment Found