开发者

How to put a time stamp while copying files in the for loop inside the batch file

Help please! I am new to the batch files and have a very specific question. I am trying to copy many files in multiple subdirectories into a single directory via a for loop and in the meantime attache a timestamp to each name (because all the files have the same name). I am using the system variable time and parsing it inside the loop, but the local variables inside the loop get assigned garbage. I already know about delayed expansion and using ! sign instead of %, but that doesnt help me. 开发者_StackOverflowhere is the code:

@echo off
SetLocal EnableDelayedExpansion
set counter=1
echo in the beginning the counter is "%counter%"
:loop
for /r "c:\users\wimdu\dropbox\wimdu CRM\emarsys reports - campaigns" %%f in (bounce*.*) do (
set a = %time::=%
echo in loop a equals "!a!"
echo the time is !time!
set b=!a:,=!

copy /y "%%f" c:\users\wimdu\documents\bouncehandling\bouncecsvfiles\%b%.csv
)
EndLocal

So, basically the names of the files would be 10151821.csv, for instance (time stamp including milliseconds). ideally it would be the original filename (bounces) concatenated with time stamp with .csv extension. I have tried everything but with the timestamp nothing seems to work, a just does not get assigned correctly. And then b as well . I do not know how to parse a and then assign it to b within !! signs. Please help!!


There are multiple problems.
You didn't set "a" as you append a space, you set "a ".
And you always use the same time, as %time% will be expanded only once while parsing the block:.
The same problem you get with %b%.
Change both to delayed expansion and it could work.


Agree with @jeb on all the points, however there may be another problem. Depending on your locale, the result of %time% may contain a leading space when the current time is before 10 in the morning. If that is so, the target path in your COPY command gets split, since it is not enclosed in double quotes. As a result, the command becomes syntactically incorrect.

So, if you fix all the issues mentioned so far, your script should work without any problem. At least a simplified version of your script worked for me. Here's how I tested it:

First, I created a simple setup on my computer:

MKDIR C:\tests\source
MKDIR C:\tests\target

then copied several random files of moderate size to the C:\tests\source folder and ran the following script:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "C:\tests\source" %%f IN (*) DO (
  SET a=!time::=!
  SET a=!a:,=!
  COPY /Y "%%f" "\!a!"
)

And this is what appeared in C:\tests\target as a result:

_1395459
_1395470
_1395639

I should point out that _ is actually a space, because on my computer the result of %time% indeed contains a leading space at this hour, which is between midnight and 10 a.m. in this part of the world. Anyway, my main point is, the script I posted above (which, I stress, is basically the same as yours) has worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜