My Assistant
![]() ![]() |
|
|
Oct 12 2011, 12:05 PM
Post
#1
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
I have a file on my local drive I want to copy to my network drive, and save it with the current date as an extension and do this in DOS using a batch file - is this possible ?
My file name is ProjectTracker.mdb on my C: drive - I want to copy it to the P: drive, something like ProjectTracker_Copy_2011_10_12.mdb if I'm running my batch file on Oct 12th, and also I would want to write another line of code deleting previous files, like 10 days back, and any earlier than that |
|
|
|
Oct 12 2011, 12:24 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 8,166 From: Pacific NorthWet |
Here's a line of code I use to make a copy of an .mdb file in a 'backup' folder...
FileCopy strLocalVersionName, Left(strLocalVersionName, Len(strLocalVersionName) - 15) & "\backup\" & Format(Now(), "yyyymmdd@hhmm") & strFileName Take a look in Access HELP for the exact syntax of the FileCopy command. ... by the way, if you use the date in yyyymmdd format as a PREFIX, you can easily sort/find the date-of-backup... |
|
|
|
Oct 12 2011, 12:26 PM
Post
#3
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
I'm sorry, you will have to 'dumb it down' - my question was for creating a batch file using MS-DOS - where exactly did you do this ?
|
|
|
|
Oct 12 2011, 12:28 PM
Post
#4
|
|
|
UtterAccess VIP Posts: 8,166 From: Pacific NorthWet |
Sorry, I was focusing on MS Access. Hopefully one of the DOS wizards can help with a batch file...
|
|
|
|
Oct 12 2011, 12:30 PM
Post
#5
|
|
|
UtterAccess VIP Posts: 6,898 From: Earth... |
Just create the batch file in NotePad, and save it with a .bat extension |
|
|
|
Oct 12 2011, 12:40 PM
Post
#6
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
Just create the batch file in NotePad, and save it with a .bat extension Yes, I know how to create a batch file, I just need help with syntax - I don't want to just create a copy in another folder and call it "MyFilename_Copy.accdb" - I want to call give it an extension with the current date, like "MyFilename_Copy_2011_10_12.accdb" if the scheduled job (I'm using scheduler to run my batch files) runs on Oct 12, for example |
|
|
|
Oct 12 2011, 12:44 PM
Post
#7
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
Hi Steve,
Pardon me for jumping in... If I recall correctly, your OS is Windows and not MS DOS. If so, maybe you could consider using VBScript instead of a Batch file because what you want to do is a little bit complicated for what DOS commands alone can do. Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Oct 12 2011, 12:51 PM
Post
#8
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
consider using VBScript instead of a Batch file because what you want to do is a little bit complicated for what DOS commands alone can do. Wow, I wouldn't have a CLUE where to start on that - I know that DOS can be dicey at times - although I have a co-worker that does this and it works fine for him - I just set up a batch file to copy an .mdb and an .accdb, and it worked fine - doesn't mean something couldn't go wrong in the future |
|
|
|
Oct 12 2011, 12:58 PM
Post
#9
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
Hi,
Wow, I wouldn't have a CLUE where to start on that - I know that DOS can be dicey at times - although I have a co-worker that does this and it works fine for him - I just set up a batch file to copy an .mdb and an .accdb, and it worked fine - doesn't mean something couldn't go wrong in the future What I meant about "complicated" was that most DOS commands are geared to work towards files - not strings. In your requirement, you want to use the "current date" with the file name. Unfortunately, the DOS command to return the current date will give you an invalid file name because it includes "invalid" characters. I am not sure which combination of DOS commands you can use to parse the result of the current date to make it valid as a file name. That's all I'm saying... On the other hand, VBScript includes functions similar to VBA functions that you are already familiar with. You will be able to use functions like Left() or Mid() to construct a valid file name from the current date. Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Oct 12 2011, 01:07 PM
Post
#10
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
dbGuy - I realize that, but I wouldn't know where to start with VBscript
|
|
|
|
Oct 12 2011, 01:27 PM
Post
#11
|
|
|
UtterAccess VIP Posts: 8,106 From: CT |
I had to dig thru some stuff but found a basis to start with.
to test this. create a text file on the C:\ drive called test.txt just put some random words in it. create a new text file with the below in it and give it an extension of .bat run it and then see if that is what you are looking for as a starting point. the key is the line that says..set newname= CODE CD /D %homedrive%\%homepath%\Desktop
Set CURRDATE=%TEMP%\CURRDATE.TMP Set CURRTIME=%TEMP%\CURRTIME.TMP DATE /T > %CURRDATE% TIME /T > %CURRTIME% Set PARSEARG="eol=; tokens=1,2,3,4* delims=/, " For /F %PARSEARG% %%i in (%CURRDATE%) Do SET YYYYMMDD=%%l%%k%%j Set PARSEARG="eol=; tokens=1,2,3* delims=:, " For /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k set newname=test_%YYYYMMDD%-%HHMM%.txt copy c:\test.txt c:\%newname% exit |
|
|
|
Oct 12 2011, 01:38 PM
Post
#12
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
dbGuy - I realize that, but I wouldn't know where to start with VBscript Okay, think of it as a hybrid between a batch file and VBA. You can use Notepad to enter the commands and save the file with a .vbs extension. To see what commands are available for you, take a look at this list. For the basics of creating a script file, take a look at this YouTube video. (Sorry, I couldn't find a better example for you.) Cheers (IMG:style_emoticons/default/cheers.gif) |
|
|
|
Oct 12 2011, 01:46 PM
Post
#13
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
|
|
|
|
Oct 12 2011, 01:46 PM
Post
#14
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
dbGuy and BobG - I will try all that, but in the meantime, I found this - you CAN do it in DOS
ClickHere I came close enough to where I know it can be done here is my batch file code, which I got from this site (the part in BOLD): copy "C:\Documents and Settings\E115812\My Documents\ProjectTracker.mdb" A:\ProjectTracker_copy.mdb echo %date:~10,4%%date:~7,2%%date:~4,2% ren A:ProjectTracker_Copy.mdb ProjectTracker_Copy_%date:~10,4%%date:~7,2%%date:~4,2%.mdb When I ran it, it produced this: ProjectTracker_Copy_012-.mdb so I'm close - I just want ProjectTracker_Copy_2011_10_012.mdb if ran on October 12th.... so at least I know it is possible - I will try your deal though - I'm always interested in learning new things |
|
|
|
Oct 12 2011, 02:00 PM
Post
#15
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
OK, I found this:
set mydate=%date:~4,2%%date:~7,2%%date:~10,4% set datestr=%d:~6,4%%d:~3,2%%d:~0,2% set y=%d:~6,4% set m=%d:~3,2% set d=%d:~0,2% echo %date:~10,4%%date:~7,2%%date:~4,2% apparently, it works like a mid function - for the Y, return a 4 character string starting with the 6th character, for the m, return a 2 character string starting with the 3rd character, for the d, return a 2 character string starting with the first position - but I can't get the syntax right ! |
|
|
|
Oct 12 2011, 02:13 PM
Post
#16
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
Hi,
dbGuy and BobG - I will try all that, but in the meantime, I found this - you CAN do it in DOS ClickHere I don't think I ever said it was not possible - just a bit complicated. As you and Bob have pointed out, the FOR command can be used to parse the date into separate sections for the filename. But I still think that using functions built for this purpose would be a little bit easier, especially since your users (or you) are not really working in a DOS only environment. Just my 2 cents... (IMG:style_emoticons/default/2cents.gif) |
|
|
|
Oct 12 2011, 02:34 PM
Post
#17
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
Hi,
OK, I found this: set mydate=%date:~4,2%%date:~7,2%%date:~10,4% set datestr=%d:~6,4%%d:~3,2%%d:~0,2% set y=%d:~6,4% set m=%d:~3,2% set d=%d:~0,2% echo %date:~10,4%%date:~7,2%%date:~4,2% apparently, it works like a mid function - for the Y, return a 4 character string starting with the 6th character, for the m, return a 2 character string starting with the 3rd character, for the d, return a 2 character string starting with the first position - but I can't get the syntax right ! I would try something like set myfile=ProjectTracker_Copy set myfile=%myfile%_%date:~10,4% set myfile=%myfile%_%date:~7,2% set myfile=%myfile%_%date:~4,2% set myfile=%myfile%.mdb ren ProjectTracker_Copy.mdb %myfile% (untested) Hope that helps... |
|
|
|
Oct 13 2011, 08:47 AM
Post
#18
|
|
|
UdderAccess Admin + UA Ruler Posts: 15,650 From: Upper MI |
Here's a copy of the vbScript I use to back up data with time stamped file names. It also logs the the amount of time it took to conduct the back up in a text file named: "c:\Scripts\MonthlyTiming.txt"
Please note that this code is executed from the Task Scheduler on the server . . . . please feel free to alter as you wish. I learned vbscripting from the Scripting Guy CODE '// VBScript. hope this helpsOption Explicit Dim oFSO, oSHL, ln '// FileSystemObject, Shell, Source, Destination Dim StTime, EndTime, TotTime dim filetxt, getname, path Dim flnm ' Filename: C:\Scripts\PCIData_Monthly_Inv.vbs '// On Error Statement On Error Resume Next Err.clear StTime = Timer Set oFSO = CreateObject("Scripting.FileSystemObject") Set oSHL = CreateObject("WScript.Shell") Set filetxt = oFSO.CreateTextFile("c:\Scripts\MonthlyTiming.txt", True) path = oFSO.GetAbsolutePathName("c:\Scripts\MonthlyPCI_Data_BU_Timing.txt") getname = oFSO.GetFileName(path) flnm = "H:\PCIData\pci_data_" & Left(Date,InStr(1,Date,"/")-1) & "-" & Right(Date,4) & ".mdb" ' COPY DATA From 'myServerName\Access Data' to SmelterH (ext drive) ln = 100 oFSO.CopyFile "D:\Access Data\pci_data.mdb",flnm ln = 5555 'Wscript.echo "PCIData Copying Complete!" EndTime = Timer TotTime = (EndTime-StTime)/60 filetxt.WriteLine("Monthly, " & Date & " backup runtime in Minutes: " & TotTime) filetxt.Close WScript.Quit '// Debug: If Err <> 0 Then MsgBox "Error:" & CStr(Err.Number) & " - " & Err.Description & " - Ooops! - at line " & ln Else '// Show MsgBox: ("Text", NumberOfSeconds, "Title" and vbOK(0) button + vbInformation(64) icon) oSHL.Popup "Backup complete...", 10 ,"Confirmation...", 0 + 64 End If |
|
|
|
Oct 13 2011, 10:28 AM
Post
#19
|
|
|
UtterAccess Ruler Posts: 4,237 From: Downey, CA |
Here's a copy of the vbScript I use to back up data with time stamped file names. I still don't understand where you PUT this code (IMG:style_emoticons/default/confused.gif) |
|
|
|
Oct 13 2011, 10:34 AM
Post
#20
|
|
|
Access Wiki and Forums Moderator Posts: 47,919 From: SoCal, USA |
Hi,
I still don't understand where you PUT this code (IMG:style_emoticons/default/confused.gif) Do you mean the YouTube video I showed you didn't help? Basically, you copy and paste Moo's code into a Text file using Notepad. But when you save it, give it a .vbs extension. You can then double-click on that file or execute it in the Task Scheduler like Moo did. Hope that helps... |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 20th May 2013 - 02:39 AM |