1.

Solve : Renaming a file with yesterday's date.?

Answer» <html><body><p>Hi folks, I need a little bit of help.  I have a file that I need to rename with <a href="https://interviewquestions.tuteehub.com/tag/yesterdays-3298687" style="font-weight:bold;" target="_blank" title="Click to know more about YESTERDAYS">YESTERDAYS</a> date in the filename.  I will be running this script on a Windows Server 2008 machine.<br/><br/>Currently I've <a href="https://interviewquestions.tuteehub.com/tag/got-23540" style="font-weight:bold;" target="_blank" title="Click to know more about GOT">GOT</a> this:<br/><br/>::echo off<br/>for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set month=%%a&amp; set day=%%b&amp; set year=%%c&amp; set blank=%%d)<br/><br/>ren "timeclock.csv" "%month%%day%%year% timeclock.csv"<br/>set date=<br/>pause<br/><br/>I suspect I am close, but could use a little help getting over the finish line.  It has been years since I've written a batch file, so please take it easy on me, I'm re-new at this.  <br/><em>This is a limited comment.</em><br/><br/>What you are asking appears to be a breach of security. It may not be possible due that on the server without extensive exceptions made to the security system. So for that reason you may not get much help on this forum because he uses here are very sensitive about things that would lead to serious security breaches. <br/><br/>What you are attempting to do could be the basis for a very serious fraud and use of the system. I don't mean to accuse yum  of wrongdoing, but if we were to publish an answer here in this forum it would be seen by a lot of people who would quickly realize such a <a href="https://interviewquestions.tuteehub.com/tag/trick-1427567" style="font-weight:bold;" target="_blank" title="Click to know more about TRICK">TRICK</a> could be used for evil purposes.<br/><br/>Could you explain why such a procedure would be necessary? Perhaps someone here can offer a practical workaround to your problem.<br/>We have a time clock that gets polled every night after the last shift quits (1:30am).  The punch information is stored in a .CSV file which we then add a date to the file name.  Idealy this date would be yesterday's date, since this is when the punches start coming in.<br/><br/>I'm not entirely sure how this could be used for evil, but then my evil skills are sadly lacking.<br/><br/>Edit:  We previously did this with 3rd party software, but we upgraded our server and this software no longer works, and I'm loath to shell out another $250 for a network capable version when I'm sure this can be done with a script.Can be done with vbscript. Not knowing your local date format, the date string may (probably does) contain slashes, which are not allowed in filenames, so must be replaced, in this <a href="https://interviewquestions.tuteehub.com/tag/example-239971" style="font-weight:bold;" target="_blank" title="Click to know more about EXAMPLE">EXAMPLE</a> with dashes. Remove echo when happy. Your local date format may differ from mine, which is dd/mm/yyyy, so test first. I know in N America you may get the day of the week (e.g. Wed)<br/><br/> Code: <a>[Select]</a>echo off<br/>&gt;temp$$$.vbs echo Wscript.echo eval(WScript.Arguments(<a href="https://interviewquestions.tuteehub.com/tag/0-242464" style="font-weight:bold;" target="_blank" title="Click to know more about 0">0</a>))<br/>For /f "delims=" %%Y in ( 'cscript //nologo temp$$$.vbs "date-1" ' ) do set yesterdate=%%Y<br/>set yesterdate=%yesterdate:/=-%<br/>echo ren "timeclock.csv" "%yesterdate% timeclock.csv"<br/>Thanks Salmon Trout, that nearly did it, but it isn't actually renaming the file.  The last command that runs looks like this:<br/> Quote</p><blockquote>C:\batch files&gt;echo ren "timeclock.csv" "7-11-10 timeclock.csv"<br/>ren "timeclock.csv" "7-11-10 timeclock.csv"</blockquote> <br/>Any thoughts?See my post.<br/><br/> Quote<blockquote>Remove echo when happy.</blockquote> <br/>That is, the batch script, as posted, just shows (echos) the command on screen and does not make any changes to files. When you have run it and you are happy with what you see, change "echo ren" to "ren" and save the script. <br/><br/>You do realise that commenting out echo off like this --<br/><br/>::echo off<br/><br/>-- means that every line is shown on the screen? And that you don't have to have this if you don't need it?<br/><br/><br/>Ah there we go, thanks very much.  I totally gapped and missed the echo in front of the ren statement, that is all.<br/><br/>Yeah I know that commenting out the echo shows everything, I was going to remove that once everything was working correctly.<br/><br/>Thanks again, you have no idea how much headache you've saved me.</body></html>


Discussion

No Comment Found