Filename to DateTime
Moderator: DosItHelp
Filename to DateTime
Only if the file name in the active directory contains "MyBackup" in it, I want to change it's name to prepend the datetime, e.g., "200907052345 MyBackup..."
This new DOS is so overwhelming to me, as a former DOS pro, it is intimidating. If someone could furnish me with the correct syntax for a bat file that I could run daily as a scheduled task, I would be very thankful.
The directory this would run from is "H:\Acronis\AA1 III White" and the affected file names would be "MyBackup1.tib", "MyBackup2.tib", etc.
TIA!
This new DOS is so overwhelming to me, as a former DOS pro, it is intimidating. If someone could furnish me with the correct syntax for a bat file that I could run daily as a scheduled task, I would be very thankful.
The directory this would run from is "H:\Acronis\AA1 III White" and the affected file names would be "MyBackup1.tib", "MyBackup2.tib", etc.
TIA!
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
You can start with this... might need some tweaking, but you should get the jist.
Code: Select all
@Echo Off
Set "Unique=x"
Set "Fldr=H:\Acronis\AA1 III White"
Call :GetUnique Unique
Dir /b %Fldr%\*MyBackup* > %Temp%\Dir.Lst
For /F %%F in (%Temp%\Dir.Lst) do (
Echo Old Filename was: %%F
Ren %%F %Unique%-%%F
Echo New Filename is : %Unique%-%%F
)
Erase %Temp%\Dir.Lst >Nul
Goto:Eof
:GetUnique
SETLOCAL
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%date:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a mm=mm+100
set /a dd=dd+100
set "mm=%mm:~1,2%"
set "dd=%dd:~1,2%"
for /f "tokens=1-4 delims=:. " %%A in ("%time: =0%") do @set UNIQUE=%yy%%mm%%dd%%%A%%B
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
Goto:EOF
Thanks for the very speedy reply. I was able to understand some, but no where near all of it. I did some tracing to try to get a better understanding and it appears that it's picking up the file stamp date, but the current time, not the filestamp time. There also seems to be a problem finding the list file: "
H:\Acronis\AA1 III White Mon 07/06/2009 12:09:07.81>Dir /b H:\Acronis\AA1 III White\*MyBackup* 1>C:\DOCUME~1\UserXP\LOCALS~1\Temp\Dir.Lst
The system cannot find the file specified."
I just wish I understood more of it than I do, but my aging mind precludes following such complex logic.
Needless to say, it did not succeed, so if you have the time and patience to assist further, I would really appreciate it!
Thanks.
Dave Campbell
H:\Acronis\AA1 III White Mon 07/06/2009 12:09:07.81>Dir /b H:\Acronis\AA1 III White\*MyBackup* 1>C:\DOCUME~1\UserXP\LOCALS~1\Temp\Dir.Lst
The system cannot find the file specified."
I just wish I understood more of it than I do, but my aging mind precludes following such complex logic.
Needless to say, it did not succeed, so if you have the time and patience to assist further, I would really appreciate it!
Thanks.
Dave Campbell
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
Same song, 2nd verse!
This encompasses the Folder name in quotes since it has spaces in it
Before you just said put the Date/Time into the name, you didn't specify that you wanted the file Date/Time. This takes care of that issue as well.
This assumes your file date/time are echoed in the "02/06/2009 01:01 PM" format.
This encompasses the Folder name in quotes since it has spaces in it
Before you just said put the Date/Time into the name, you didn't specify that you wanted the file Date/Time. This takes care of that issue as well.
This assumes your file date/time are echoed in the "02/06/2009 01:01 PM" format.
Code: Select all
@Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
Set "Unique=x"
Set Fldr="H:\Acronis\AA1 III White"
Dir /b %Fldr%\*MyBackup* > %Temp%\Dir.Lst
For /F %%F in (%Temp%\Dir.Lst) do (
Echo Old Filename was: %%F
Echo File Date/Time is: %%~tF
Call :GetUnique Unique "%%~tF"
Echo New Filename is : !Unique!-%%F
Ren %%F !Unique!-%%F
)
Erase %Temp%\Dir.Lst >Nul
Goto:Eof
:GetUnique
SETLOCAL
Set DT=%~2
Set TM=%DT:~11,5%
Set Dmm=%DT:~0,2%
Set Ddd=%DT:~3,2%
Set Dyy=%DT:~6,4%
for /f "tokens=1-4 delims=:. " %%A in ("%TM: =0%") do @set UNIQUE=%Dyy%%Dmm%%Ddd%%%A%%B
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
Goto:EOF
-
- Posts: 319
- Joined: 12 May 2006 01:13
you can use vbscript for easier date manipulation and everything else
save the above as myscript.vbs and on command line
Code: Select all
today = Now
yr = Year(today)
mth = Month(today)
dy = Day(today)
hr = Hour(today)
min =Minute(today)
sec = Second(today)
If Len(mth) <2 Then
mth="0"&mth
End If
If Len(dy) <2 Then
dy="0"&dy
End If
Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "H:\" 'your H folder here
Set objFolder = objFS.GetFolder(strFolder)
For Each strFiles In objFolder.Files
strFileName = strFiles.Name
strFilePath = strFiles.Path
If InStr(strFiles.Name,"MyBackup")>0 Then
WScript.Echo strFileName
strBaseName = objFS.GetBaseName(strFileName)
strExtension = objFS.GetExtensionName(strFileName)
strNewFileName = yr & mth & dy & hr & min & sec&" "& strBaseName & "." & strExtension
WScript.Echo strNewFileName
strFiles.Name = strNewFileName
End If
Next
save the above as myscript.vbs and on command line
Code: Select all
h:\folder> cscript /nologo myscript.vbs
-
- Posts: 319
- Joined: 12 May 2006 01:13
RElliott63 wrote:Same song, 2nd verse!
This encompasses the Folder name in quotes since it has spaces in it
Before you just said put the Date/Time into the name, you didn't specify that you wanted the file Date/Time. This takes care of that issue as well.
This assumes your file date/time are echoed in the "02/06/2009 01:01 PM" format.:lol: :lol:Code: Select all
@Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
Set "Unique=x"
Set Fldr="H:\Acronis\AA1 III White"
Dir /b %Fldr%\*MyBackup* > %Temp%\Dir.Lst
For /F %%F in (%Temp%\Dir.Lst) do (
Echo Old Filename was: %%F
Echo File Date/Time is: %%~tF
Call :GetUnique Unique "%%~tF"
Echo New Filename is : !Unique!-%%F
Ren %%F !Unique!-%%F
)
Erase %Temp%\Dir.Lst >Nul
Goto:Eof
:GetUnique
SETLOCAL
Set DT=%~2
Set TM=%DT:~11,5%
Set Dmm=%DT:~0,2%
Set Ddd=%DT:~3,2%
Set Dyy=%DT:~6,4%
for /f "tokens=1-4 delims=:. " %%A in ("%TM: =0%") do @set UNIQUE=%Dyy%%Dmm%%Ddd%%%A%%B
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
Goto:EOF
some minor issues
1) there's no need to use temp files, right?
2) this solution doesn't take care of different date/time settings. yes, i am not about a to change my regional settings to run your script:) the script should take care of that
3) you forgot to put 20 infront of 09 to make it 2009
-
- Expert
- Posts: 80
- Joined: 04 Feb 2009 10:03
ghostmachine4 wrote:RElliott63 wrote:Same song, 2nd verse!
This encompasses the Folder name in quotes since it has spaces in it
Before you just said put the Date/Time into the name, you didn't specify that you wanted the file Date/Time. This takes care of that issue as well.
This assumes your file date/time are echoed in the "02/06/2009 01:01 PM" format.Code: Select all
@Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
Set "Unique=x"
Set Fldr="H:\Acronis\AA1 III White"
Dir /b %Fldr%\*MyBackup* > %Temp%\Dir.Lst
For /F %%F in (%Temp%\Dir.Lst) do (
Echo Old Filename was: %%F
Echo File Date/Time is: %%~tF
Call :GetUnique Unique "%%~tF"
Echo New Filename is : !Unique!-%%F
Ren %%F !Unique!-%%F
)
Erase %Temp%\Dir.Lst >Nul
Goto:Eof
:GetUnique
SETLOCAL
Set DT=%~2
Set TM=%DT:~11,5%
Set Dmm=%DT:~0,2%
Set Ddd=%DT:~3,2%
Set Dyy=%DT:~6,4%
for /f "tokens=1-4 delims=:. " %%A in ("%TM: =0%") do @set UNIQUE=%Dyy%%Dmm%%Ddd%%%A%%B
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%UNIQUE%) ELSE echo.%UNIQUE%
Goto:EOF
some minor issues
1) there's no need to use temp files, right?
*Some do, Some Don't ... in the grand scheme of things... does this REALLY MATTER?
2) this solution doesn't take care of different date/time settings. yes, i am not about a to change my regional settings to run your script:) the script should take care of that
*What part of (This assumes your file date/time are echoed in the "02/06/2009 01:01 PM" format.) are you arguing about? Reading must not be a priority for you...
3) you forgot to put 20 infront of 09 to make it 2009
*Based on the example given this "Set Dyy=%DT:~6,4%" takes care of the *20* in front of the *09* to make it *2009* -- Reading must STILL be an issue for you. Thus, the ORIGINAL statement of "(this)...might need some tweaking,..."
@ghostmachine4
Just to ask a question if you don't mind... Based on your posts and what you've responded in other threads... Do you have to be such a *jerk* about helping people? There are NUMEROUS ways to do scripts (that's the beauty of this website)... and you providing another view or example is welcome (even the VB Scripts). But, jeeze, show some common courtesy to those of us trying to help others.
-
- Posts: 319
- Joined: 12 May 2006 01:13
RElliott63 wrote:Just to ask a question if you don't mind... Based on your posts and what you've responded in other threads... Do you have to be such a *jerk* about helping people?
why do you think so? you can call me a jerk as long as my suggestion sound like bullshit and does not even answer the OP, otherwise they are valid facts to be looked at. Isn't it true that the batch code is not independent of one's regional settings? I know you had assumed your own date format, however, i am also only stating a fact. you don't have to get so upset over a fact. As for the "20" in front of the "09" case, have you tested your code when you run it on a PC with different regional settings?
There are NUMEROUS ways to do scripts (that's the beauty of this website)... and you providing another view or example is welcome (even the VB Scripts). But, jeeze, show some common courtesy to those of us trying to help others.
yes, there are numerous ways, so we should choose to code our script the best possible way without losing efficiency and the ability to comprehend/maintain the code.
Lastly I am not even saying your batch is bad that it can't be used. Just that its not the best way to do things.
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
ghostmachine4 wrote:why do you think so?
We don't have non-verbal communication on a forum. Your posts have tended to be short -- sometimes with not even a passing mention of the workable solutions that others have provided. Most people would read your posts and think "that guy's tone is anywhere from pompous to arrogant to jerk to clueless to mean to whatever". We don't know you, and many people don't "proofread" their posts to see if it might sound harsh or whatever. Case in point:
Especially the bold portion. Yes, you did throw a smiley face in there, but that doesn't absolve you if it still sounds like an attack.some minor issues
1) there's no need to use temp files, right?
2) this solution doesn't take care of different date/time settings. yes, i am not about a to change my regional settings to run your script:) the script should take care of that
3) you forgot to put 20 infront of 09 to make it 2009
It's not the fact -- it's how you presented it.you can call me a jerk as long as my suggestion sound like bullshit and does not even answer the OP, otherwise they are valid facts to be looked at. Isn't it true that the batch code is not independent of one's regional settings? I know you had assumed your own date format, however, i am also only stating a fact. you don't have to get so upset over a fact.
Are you serious? It's a perfectly reasonable assumption from the original post that this will be run on 1 machine and, as a "former DOS pro" it is further reasonable to assume that the original poster can tweak the code to match his regional requirements. Should that not be the case, a more complicated script can be developed if the original poster asks for more help. Not to mention that this website has date/regional tools that are pretty close to "cut and paste". Sometimes we expect the original poster to do some of their own work.As for the "20" in front of the "09" case, have you tested your code when you run it on a PC with different regional settings?
Relliott is being nice. My personal opinion is that this is a dos forum, and I should use standard dos commands available to pretty much any standard WINXP type machine and only use other options if it can't be done in dos. Sometimes an alternative solution is hinted at (as in "this is awkward in pure dos--IF you're interested I could show you a visual basic script (or whatever) that would likely be easier and better"). But that's must my opinion.There are NUMEROUS ways to do scripts (that's the beauty of this website)... and you providing another view or example is welcome (even the VB Scripts). But, jeeze, show some common courtesy to those of us trying to help others.
Do you really not comprehend how pompous, snobbish, and arrogant that sounds? It pretty much is the same as "My standards are right, your standards are wrong" or, really, just "I'm right, you're wrong".yes, there are numerous ways, so we should choose to code our script the best possible way without losing efficiency and the ability to comprehend/maintain the code.
Here in the midwest (Iowa, USA), despite the definition of the individual words, when anyone uses the phrase "That's not the best way to do something", they mean exactly that it's bad. No joke. I guess I'd call it "pretend politeness" or something. It's not "politically correct" to just tell someone they're doing something wrong, so you sorta say "it's not the best way", but you really just mean it's bad. And what's so funny, is that everyone knows that's what you mean, but somehow we all still accept it as a more polite way to talk to each other.Lastly I am not even saying your batch is bad that it can't be used. Just that its not the best way to do things.
So anyway -- let me try to be clear. I don't know you. I don't believe anything you've posted has been mean or overtly negative. I do believe that some of your posts have been slightly insulting (though probably unintentional). In my opinion, the overall "tone" of your posts has been condescending, or perhaps arrogant. Like you know better than anyone else and somehow have earned the right to correct the rest of us and show us a better way -- your way. There are a lot of people on forums like this who just want "quick and dirty" to get something done. I feel like you are imposing your personal sense of coding style and good coding practice upon the original posters. Really, isn't it up to them to decide if their problems have been satisfactorily solved? It's not like you are engaging in an intellectual discussion of the presented solution for the edification of all involved (example -- we could have a nice discussion about the evils of temp files and that they should generally be avoided where possible). Instead you are completely bypassing the already proposed solution and presenting (by all appearances because you believe it to be superior) a completely different solution.
-
- Posts: 319
- Joined: 12 May 2006 01:13
avery_larry wrote:ghostmachine4 wrote:Are you serious? It's a perfectly reasonable assumption from the original post that this will be run on 1 machine and, as a "former DOS pro" it is further reasonable to assume that the original poster can tweak the code to match his regional requirements.
well, if its an assumption, then its also perfect alright for me to assume that he's going to use it on different PC someday. isn't it so?My personal opinion is that this is a dos forum, and I should use standard dos commands available to pretty much any standard WINXP type machine and only use other options if it can't be done in dos.
that's your opinion. Unless specific rules are set for this definition, anyone can post viable solutions.Do you really not comprehend how pompous, snobbish, and arrogant that sounds? It pretty much is the same as "My standards are right, your standards are wrong" or, really, just "I'm right, you're wrong".
don't put words in my mouth. I did not say "you're wrong" , i just said its not the best way. there are differences. Therefore, please don't assume on your own.There are a lot of people on forums like this who just want "quick and dirty" to get something done. I feel like you are imposing your personal sense of coding style and good coding practice upon the original posters. Really, isn't it up to them to decide if their problems have been satisfactorily solved?
yes, in conclusion, its really up to OP. therefore, why are you nitpicking on me saying that i am imposing my personal sense of coding style? If OP wants to use my solution, fine. if not, so be it.