Solved

rename file using bat

Posted on 2014-11-05
4
291 Views
Last Modified: 2014-11-06
Hi,

At around 10am daily I get a csv file saved to a network drive called 01rev.csv, I would like to create a batch file to rename the file to the previous days date in the format yyyymmdd, how would I do this?
0
Comment
Question by:hellblazeruk
  • 2
4 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 40424172
This uses a temporary VB script to do the date calculation; it's currently in test mode and will only display the "ren" command it would normally run. Remove the uppercase ECHO in line 11 to run it for real:
@echo off
setlocal enabledelayedexpansion
set File=C:\Temp\01rev.csv
set AddDays=-1
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[VBScript]"') do set VBStart=%%a
(for /f "skip=%VBStart% delims=" %%a in ('type "%~f0"') do (echo %%a)) >"%Temp%\GetTimeStamp.vbs"
for /f "delims=" %%a in ('cscript.exe /nologo "%Temp%\GetTimeStamp.vbs" %AddDays%') do (set TimeStamp=%%a)
del "%Temp%\GetTimeStamp.vbs"
echo Time stamp: %TimeStamp%
for %%a in (%File%) do set Extension=%%~xa
ECHO ren "%File%" "%TimeStamp%%Extension%"
echo Done.

goto :eof
REM ===== No batch below this line! =====
[VBScript]
NewDate = DateAdd("d", WScript.Arguments(0), Now)
NewYear = Year(NewDate)
NewMonth = Month(NewDate)
NewDay = Day(NewDate)
If NewMonth < 10 Then
	NewMonth = "0" & NewMonth
End If
If NewDay < 10 Then
	NewDay = "0" & NewDay
End If
Wscript.Echo NewYear & NewMonth & NewDay

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40424197
I like that approach for being more generalised but would this do for just yesterdays date  (from article of mine here:)

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html - specifically the yesterday-yyyymmdd.cmd one:

@echo off
REM Create temporary VBS file to make yesterday's date:
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a

cd /d C:\somedir
ECHO rename 01rev.csv "%yesterday%.csv"

Open in new window

0
 

Author Comment

by:hellblazeruk
ID: 40424386
Hi Steve,

Thanks for the link,
so my batch file is now

REM Generate File
d:
cd \Export\
dbisql.exe -datasource sales -c Server=sqlsalessrv01;UID=custom;PWD=custom -q SalesExport1.sql

echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a

ECHO rename 01rev.csv "%yesterday%.csv"

move D:\export\csv\*.csv* Z:\

REM End of Script

This creates the file but its still called 01rev.csv ?
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40424893
Sorry should have said, you need to remove the upper case word ECHO like with the previous example.  That means it just echoes to the screen the command rather than doing it (so you could see it was correct)... have adjusted it a bit what I think you are trying to do:

i.e.

@echo off
REM Get yesterdays date
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a

REM Generate File
cd /d D:\export
dbisql.exe -datasource sales -c Server=sqlsalessrv01;UID=custom;PWD=custom -q SalesExport1.sql

REM Rename file and move to z: drive, presume this is what you want to rename?
cd /d D:\export\csv
if exist 01rev.csv (
  echo Renaming file to %yesterday%
  rename 01rev.csv "%yesterday%.csv"
  move *.csv* Z:\
) ELSE (
  echo 01rev.csv not found
  pause
)

REM End of Script

Open in new window


Steve
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question