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

Posted on 2004-08-23
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)

Question by:omegabeta
  • 4
  • 3

Expert Comment

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
      set fOut = nothing
      set fDir = nothing
      set FSO = nothing
End Sub

Expert Comment

ID: 11874296
put this in your batch file. I hope you can customize it to your needs :).
Almost similar question is also here

@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%\

Expert Comment

ID: 11874309
remove the mkdir and time part if you already have the directory with name equal to date only.

Author Comment

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.


Expert Comment

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...

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.

would extract the last 10 characters of the PATH variable.

would extract all but the last 2 characters of the PATH variable.

Author Comment

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


Accepted Solution

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:)

Author Comment

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 ?

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now