Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

rename file using bat

Posted on 2014-11-05
4
Medium Priority
?
318 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 85

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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