Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-08-23
8
337 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

789 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