Solved

rename file using bat

Posted on 2014-11-05
4
300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 84

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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