Solved

Save a file with a particular name using a .bat file

Posted on 2004-08-23
8
336 Views
Last Modified: 2010-04-17
I want to save a file with a particular name using a .bat file
Something like this:

copy \\comp1\directory\*.*  \\server\date

where date can be A1082004  (A=0, B=1,C=2, D=3)
So I must save in a particular directory coresponding to the partcular day(day= today -1)

Thanks
0
Comment
Question by:omegabeta
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:malharone
ID: 11873963
The sample below writes the file as "OrdersReport_8-23-2004_1.09.10_AM.xml"

Sub FileSave( sContents )
      dim FSO
      dim fOut
      dim fDir
      dim fOutPath
      dim fName
      dim fDate
      On Error Resume Next
 
      fOutPath = "c:\temp\"
      fDate = Replace( Now(), "/", "-" )
      fDate = Replace( fDate, ":", "." )
      fDate = Replace( fDate, " ", "_" )
      fName = "OrdersReport_" & fDate & ".xml"

      set FSO = CreateObject ("Scripting.FileSystemObject")
      set fOut = FSO.CreateTextFile(fOutPath & fName )
      set fOut = FSO.GetFile(fOutPath & fName )

      ' open output file for writing as ANSI
      set sOut = fOut.OpenAsTextStream(2, 0 )

      ' write the file out and delete the input file
      sOut.write sContents
      sOut.Close
      set fOut = nothing
      set fDir = nothing
      set FSO = nothing
End Sub
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11874296
hi,
put this in your batch file. I hope you can customize it to your needs :).
Almost similar question is also here http://www.experts-exchange.com/Programming/Q_21102859.html

###############copyfiles.bat####
@echo off
set date1=%date:~-10%
set date2=%date1:/=%

set time1=%time: =0%
set time2=%time1::=%
set time3=%time2:~0,4%

set dirname=%date2%_%time3%
mkdir c:\images\%dirname%

copy h:\dcim\101msdcf\* c:\images\%dirname%\
##############
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11874309
remove the mkdir and time part if you already have the directory with name equal to date only.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:omegabeta
ID: 11915638
For kumvjuec,

Please explain me the following:
set date1=%date:~-10% extract the last 10 charactes from date (~ what reprezent ?)
set date2=%date1:/=% ?

set time1=%time: =0% ?
set time2=%time1::=% ?
set time3=%time2:~0,4%  extract first 4 characters

Thanks
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11957601
hi omega
sorry, i was out on vacation :)

All the set statements just define a new variable which act as intermediate. I have used them just for the sake of clarity. If you want to see their values, you can use
echo %time1%
for printing the values on the screen. The above statement for instance will print the value of variable "time1"
For help on ~, use
set /? on the command prompt. just pasting some of it...

    %PATH:~10,5%
would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.  If the length is not specified, then it defaults to the
remainder of the variable value.  If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

    %PATH:~-10%
would extract the last 10 characters of the PATH variable.

    %PATH:~0,-2%
would extract all but the last 2 characters of the PATH variable.
0
 

Author Comment

by:omegabeta
ID: 11976065
OK kumvjuec . I made: set day=%date:~-10,2%
But how can I make a comparition such as:

f %day% <10 set day=A
if %day% <20 and %day% >10 set day=B
if %day% <30 and %day% >20 set day=C
if %day% >30 set day=D

Thanks
0
 
LVL 3

Accepted Solution

by:
kumvjuec earned 125 total points
ID: 11979504
hi omega

use this

if %day% lss 10 (set day=A)
if %day% lss 20 ( if %day% gtr 10 (set day=B))

btw, you should also take care of the conditions where day=10.
for <=, the symbol is leq
for >=, the symbol is geq.

Hope your work will get done now:)
0
 

Author Comment

by:omegabeta
ID: 12013192
Thanks kumvjuec. Maybe you can tell can can I delay a message on the screen. Neither TIMEOUTn nor SLEEP n doesn't work. How can I use a FOR loop for example ?
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question