Thursday, 15 September 2011

Use Dos commands to copy file and preserve date in file name -



Use Dos commands to copy file and preserve date in file name -

i'm having problem trying re-create , rename file using dos commands. have file of format myfile20130218 , want re-create , rename some_other_file_20130218.

i know can utilize copy source dest i'm having problem how isolate date , preserve it. cannot guarantee date today's date ruled out, source file same name.

i can run either series of commands or batch script, thing that having problem with, after find match need copy, using myfile????????, how can file names pull dates off them?

edit: clarification looking @ files in known directory, above, know format of file name, , checking specific directory it. process checks directory connectdirect file watcher, when file found matching myfile20130218 can fire off commands, don't know how check directory , name of file present.

something should work:

%oldname:~-8% extracts lastly 8 characters %oldname% appended new filename.

update: if can identify file external programme , phone call batch script file name

copyfile.cmd c:\path\to\myfile20130218

you this:

set oldname=%~nx1 set newname=%~dp1some_other_file_%oldname:~-8% re-create "%~f1" "%newname%"

update 2: if know folder , format phone call script folder

copyfile.cmd c:\folder

and this:

@echo off setlocal enabledelayedexpansion /f %%f in ( 'dir /b "%~f1" ^| findstr /r "myfile[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"' ) ( set oldname=%~f1\%%f set newname=%~f1\my_other_name_!oldname:~-8! re-create "!oldname!" "!newname!" ) endlocal

edit: script breakdown.

setlocal enabledelayedexpansion enables variable expansion within loops , conditionals. for /f %%f in ('...') executes command between single quotes , loops on output of command. dir /b "%~f1" lists content of given directory (%~f1 expands total path of first argument passed script) in simple mode (no header, no summary). findstr /r "myfile[0-9]...[0-9]$" filters input strings end substring "myfile" followed 8 digits. circumflex before pipe (^|) escapes pipe, because otherwise take precedence on for command, split for command in half, resulting in invalid command-line. set oldname=%~f1\%%f assign total path matching file variable oldname. set newname=%~f1\my_other_name_!oldname:~-8! assign total path new filename ("my_other_name_" followed trailing 8 digits oldname) variable newname. copy "!oldname!" "!newname!" don't need explain this, i?

batch-file copy cmd

No comments:

Post a Comment