0

How to create a batch file, which will make a folder with date and time as folder name in each 1 hrs

I have a folder name in c drive 'test', my instrument will generate a txt file in the same folder. data will update and accumulate in the txt file in each 2 min.

I want to copy the same txt file from the location and paste in another location in every 1 Hr.

each time copying I want to create a new folder with name as current date and current time.

  • Create a batch file containing [MD](http://ss64.com/nt/md.html) [TIME](http://ss64.com/nt/time.html) and set it as a scheduled task. – Tim Aug 05 '16 at 19:23
  • please check, if [this](http://stackoverflow.com/a/18024049/2152082) may help you. – Stephan Aug 05 '16 at 19:28
  • OK. But how to copy paste the txt files from c:\test\ to the new generated folder. can you please give me the commands – vineeth c l Aug 05 '16 at 19:40
  • `copy`, `xcopy`, `robocopy`. [here](http://ss64.com/nt/) you can find a list of all available commands and a description of each.. – Stephan Aug 05 '16 at 20:06
  • Stephan, actually I used all, but how to copy the file into the newly created folder which I created using command – vineeth c l Aug 06 '16 at 02:44

1 Answers1

3

the trick is: don't create your folder with something like md %time% (wouldn't work anyway), but create a variable and use that for md AND copy:

@echo off
REM change to the destination folder:
cd /D c:\destination
REM generate datetime-string:
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
REM format to the desired format (example):
set string=%datetime:~0,12%
REM create a subfolder:
md %string%
REM xopy the file:
copy "c:\test\myfile.txt" "c:\destination\%string%\"

Instead of creating a new folder with a single file inside, I would just rename the textfiles to include the date-time-string:
forget the md command and replace the copy line with:
copy "c:\test\myfile.txt" "c:\destination\%string%-myfile.txt"

To execute it regularily, I recommend using a scheduled task.

Stephan
  • 47,723
  • 10
  • 50
  • 81