

InterviewSolution
Saved Bookmarks
1. |
Solve : Batch renaming files? |
Answer» <html><body><p>Hello,<br/><br/>I have created 365 excel files, one for each day of 2018. I want to rename them in such a way that the <a href="https://interviewquestions.tuteehub.com/tag/date-11316" style="font-weight:bold;" target="_blank" title="Click to know more about DATE">DATE</a> of each day of the year is part of the <a href="https://interviewquestions.tuteehub.com/tag/filename-987949" style="font-weight:bold;" target="_blank" title="Click to know more about FILENAME">FILENAME</a>. For example:<br/><br/>Some_file 01-01-2018<br/>Some_file 01-02-2018<br/>Some_file 01-03-2018<br/>.<br/>.<br/>.<br/>.<br/>Some_file 12-31-2018<br/><br/>I guess I'm looking for a script to accomplish that. I have two renaming programs that accept scripts, but I could also use the command line. <a href="https://interviewquestions.tuteehub.com/tag/thanks-665909" style="font-weight:bold;" target="_blank" title="Click to know more about THANKS">THANKS</a> for any help.If the contents of the files are the same until edited in 2018, then a script with incremental counters could be used to make a copy of one master file and save it as the some_file 01-01-2018 then increments to 2 for the day, logic would be needed to reset the day <a href="https://interviewquestions.tuteehub.com/tag/counter-249187" style="font-weight:bold;" target="_blank" title="Click to know more about COUNTER">COUNTER</a> to 1 when the 31rst day of January 2018 is reached and increment month to 2 and then continue then as 02-01-2018 to which it resets the day counter again for each month as the days in the month are different month to month and so. You would need 12 rules to <a href="https://interviewquestions.tuteehub.com/tag/govern-468972" style="font-weight:bold;" target="_blank" title="Click to know more about GOVERN">GOVERN</a> what the counter will increment to for each month before resetting back to 1 and incrementing month to next month, then end at December 31, 2018<br/><br/>I know how to do this in other languages using system calls to interact with command shell to copy and rename the copied file and go to the next, but not sure if batch alone can do this.<br/><br/>If the contents of each file are different then my above suggestion wouldnt work, but if date is within the file itself it could be read in on a line and then used to rename itself by making a copy with the date appended to the name, and then deletion of the original file that it came from since you cant rename a file that is in use.Off the top of my head, in VBScript you can just start with a date, 01/01/2018, and add 1 day to it 364 times, using the DateAdd function, listing out the date string for each, or performing the rename, etc. I am going to bed now but if this is still unanswered when I come back from work tomorrow, I'll give it a try. ...Without knowing anything about what the 365 files are, and how any renaming scheme is hoped for (what file gets renamed to what?) it is not really possible to offer any script ideas.<br/>+ 1 ... Quote from: Salmon Trout on December 11, 2017, 12:34:19 PM</p><blockquote>it is not really possible to offer any script ideas.</blockquote> Never stopped me before:<br/><br/>Myscript.vbs<br/><br/>ds = wscript.arguments(0)<br/>dj = wscript.arguments(1)<br/>nd = DateAdd("d", dj, ds)<br/>mm = month(nd)<br/>dd = day (nd)<br/>yyyy = year (nd)<br/>if mm < 10 then mm = "0" & mm : end if<br/>if dd < 10 then dd = "0" & dd : end if<br/>nd2 = mm & "-" & dd & "-" & yyyy<br/>dn = WeekdayName(Weekday(nd),true)<br/>wscript.echo nd2 & " " & dn <br/><br/>In the last line, omit everything after <strong>wscript.echo nd2</strong> if you don't want the short day name appended.<br/><br/>Run it with cscript.exe //nologo and feed it 2 quoted parameters: the start date in the format shown, and the number of days forward you want a date to be generated.<br/><br/>C:\>cscript //nologo myscript.vbs "01-Jan-2018" "6"<br/>01-07-2018 Sun<br/><br/>C:\>for /l %N in (0,1,30) do cscript //nologo myscript.vbs "01-Jan-2018" "%N"<br/>01-01-2018 Mon<br/>01-02-2018 Tue<br/>01-03-2018 Wed<br/>01-04-2018 Thu<br/>01-05-2018 Fri<br/>01-06-2018 Sat<br/>01-07-2018 Sun<br/>01-08-2018 Mon<br/>01-09-2018 Tue<br/>01-10-2018 Wed<br/>01-11-2018 Thu<br/>01-12-2018 Fri<br/>01-13-2018 Sat<br/>01-14-2018 Sun<br/>01-15-2018 Mon<br/>01-16-2018 Tue<br/>01-17-2018 Wed<br/>01-18-2018 Thu<br/>01-19-2018 Fri<br/>01-20-2018 Sat<br/>01-21-2018 Sun<br/>01-22-2018 Mon<br/>01-23-2018 Tue<br/>01-24-2018 Wed<br/>01-25-2018 Thu<br/>01-26-2018 Fri<br/>01-27-2018 Sat<br/>01-28-2018 Sun<br/>01-29-2018 Mon<br/>01-30-2018 Tue<br/>01-31-2018 Wed <br/></body></html> | |