Copying a file from a folder with today's date to another folder.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chachie
Posts: 1
Joined: 10 Jun 2019 12:52

Copying a file from a folder with today's date to another folder.

#1 Post by chachie » 10 Jun 2019 12:54

Hello everyone,

I'm hoping someone can help as I'm not the best a creating batch files.

I would like to copy a csv file from a folder and save it in another folder. The source folder is named with today's date (i.e. 06102019) and the destination folder does not change. The CSV will have to overwrite the previous file.

For example:
The batch file will run and look for the folder with today's date
Copy hg200T.csv from C:\monitoring\06102019
Paste (and overwrite existing file) to C:\warning\excel\stream

Thanks in advance

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Copying a file from a folder with today's date to another folder.

#2 Post by penpen » 10 Jun 2019 16:25

You could just use the copy command with the /y-switch (to overwrite the target file without prompting), and use the date environment variable inside a for/f-loop to build the source name. Due to the fact that the date is localized you may have to modify that code , so the following hopefully may helo you (untested; make sure the target is correct and you also might consider to backup the actual target file before running this batch script):

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion

:: Only works if the date format on your computer matches the german one "11.06.2019",
:: else you have to modifiy which variable is used or which delimiter to choose.
:: To display the format of your date just remove the colons in front of the following command.
:: echo(%date%
for /f "tokens=1-3 delims=." %%a in ("%date%") do (
	copy /y  "C:\monitoring\%%~b%%~a%%~c\hg200T.csv" "C:\warning\excel\stream"
)
penpen

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copying a file from a folder with today's date to another folder.

#3 Post by aGerman » 10 Jun 2019 16:29

You can use the wmic command to work around localized date formats.

Code: Select all

@echo off &setlocal
for /f %%i in ('wmic os get localdatetime /value') do for /f %%j in ("%%i") do set %%j
copy /y "C:\monitoring\%localdatetime:~4,4%%localdatetime:~,4%\hg200T.csv" "C:\warning\excel\stream\"
Steffen

Post Reply