Copying multiple files and adding date to the name bat

10.5k views Asked by At

I have to make .bat file who does this:

Copies from O:\siirto all files that name starts with "ls". To C:\siirto. And the name of the output would be same as in the source. But it would add current date to the end of file name.

I tried following just for test and it didnt work of course :D. But it propably explains better what im trying to do than explanation above.

echo off
xcopy O:\siirto\ls* C:\siirto\ls%date.txt
pause

Of course its not working. But is that possible to do with one .bat file. Or do i have to do all ls.txt-files their own .bat-files or lines.

Like one for LS1.txt, LS2.txt LS3.txt

echo off
xcopy O:\siirto\LS1.txt C:\siirto\ls1%date
pause

I have no idea how the %date should add to code and does it need something else to code also?

Im still on child shoes in programming...

thanks

4

There are 4 answers

1
PA. On BEST ANSWER

you need to combine the iteration over the files with the FOR command and the %DATE% environment variable. Read HELP FOR, paying attention to the %~ expansion syntax, and try the following code.

for %%a in (o:\siirto\ls*) do (
   echo copy "%%a" "c:\siirto\%%~na-%date%%%~xa"
)

after careful testing, remove the echo command.


You might generalize a bit your code, by moving some configurable information out of the loop

set src=o:\siirto\ls*
set dest=c:\siirto
for %%a in (%src%) do (
  copy "%%a" "%dest%\%%~na-%date%%%~xa"
)

and, in case the %date% command returns invalid characters, read my accepted answer to this SO question Batch script date into variable and then change the variable to the one that contains the current date with the appropiate format.

0
JosefZ On

The %date% could contain characters that are not allowed in file names. Try next locale-independent approach:

@ECHO OFF
SETLOCAL enableextensions

for /F %%g in ('
  wmic OS get LocalDateTime /value^|findstr "="
  ') do for /F %%G in ("%%g") do set "_%%G"

set "_LocalDateTime=%_LocalDateTime:~0,8%"

for /F "delims=" %%G in ('
  dir /b O:\siirto\ls*.txt
  ') do echo copy /B "O:\siirto\%%G" "C:\siirto\%%~nG_%_LocalDateTime%%%~xG"

Resources (required reading):

0
Magoo On

The variable %date% by default contains the current date - but the format of the date depends on user-settings, so it may contain separators such as /-. may have a dayname and space-separator and may have the day-number and month-number with suppressed-leading-zero.

There are many articles on SO about formatting date-strings to a usable format - the format yyyymmdd is recommended since that format is automatically by name into a chronological sequence.

Note that it's not a good idea to use the variable date since that date is established and maintained by the system and any user-setting will override the assumed system-seting. Use virtually any other valid variable name (the same comment applies to random and time amongst others).

So, assuming you assign the date in the format that you want to a variable today then

for %%a in (o:\siirto\ls*.txt) do (
   echo(copy "%%a" "c:\siirto\%%~na%today%%%~xa"
)

would perform the required copy. %%~na means the name part of the filename in %%a and %%~xa the extension part. Variables should normally be accessed by using %varname%. The filenames are "enclosed in quotes" to ensure that separators within the filenames (such as Space) are regarded as regular characters not having the special meaning of a separator.

The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)

0
Elly G On

You can do it all in one line using forfiles1

    forfiles /P O:\siirto /M ls*.txt /C "cmd /c copy @path C:\siirto\@fname-%date:~6,4%-%date:~3,2%-%date:~0,2%.txt"

Here forfiles looks at all the files in the specified path /P that match the mask /M, then runs a command on each file found @path to copy it to the new location with the date appended. @fname here just gives you the file name without the path or extension.

The %date% variable generally yields something along the lines of 25/06/2015, or even Thu 25/06/2015, depending on your settings. This is why you need to specify parts of it.

%date:~6,4% selects from the seventh to the 11th digit in the date (the counting starts from 0), which yields the year. %date:~3,2% gives you the month (or the day depending on where in the world you are from) and %date:~0,1% gives you the day (or month, again depending on where you are).

You might have to play around with this a little bit to get the result you want.