Solved

rename file using bat

Posted on 2014-11-05
4
306 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 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 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

621 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