开发者

How do I copy a directory that has a date stamp

I'm trying to copy the contents of a directory using a DOS batch file that begins with the computer name followed by an underscore and a date stamp. My first impulse was some variation of:

copy D:\%Computername%_\*\\*.* C:\WhateverPath

Of course I could not get this to work. Seems like a simple problem but I d开发者_开发百科on't have much experience with batch files or DOS.


Try:

FOR /d %d IN (D:\%COMPUTERNAME%_*) DO xcopy %d C:\WhateverPath /E

This iterates over all directories (hence the /d) with the pattern %COMPUTERNAME%_* under D:\, and copies the contents of these directories into C:\WhateverPath. /Eis for copying all files and directories, also the empty ones.

For documentation of xcopy, type xcopy /? in a DOS shell (cmd).

Note: If you put this in a batch-file (something.bat), you must replace %d with %%d in the code above.


If you have multiple folders labeled C:\%computername%_%random_time_stamp%\ and you need to access each of them then move all of their contents to a single folder, you can do this:

Given the only underscore in the path is the one between %computername% and your timestamp

FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /b /a:d "C:\" ^| FIND /I "%computername%_"`) DO (
 COPY /y "%%~fF\*" "C:\WhateverPath\"
)

That states for every result that comes from the command DIR, /b switch meaning no header information, /a:d meaning only returning directories, I want to find only folders with the computername_ in it, and I want to copy the contents of each of those folders to C:\WhateverPath\ folder.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜