Solved

Rename multiple files with dos or vb script

Posted on 2013-12-09
13
489 Views
Last Modified: 2013-12-10
Hi all, I have a task to do some FTP file moves that will get appended with a job class id and date and time stamp. My issue is that I don't want to have to rename these files manually before each upload. Scenario:
Files are pulled from site using a wild card of mget xyz*
sample list of files:
xyx123
xyzrst
xyzhug
The files then have to be sent to another location as such
12062013_xyz123.jobclassid.txt
12062013 is the date time that gets appended to the front of the file
xyz* is the actual file name
jobclassid can be something like bhby123
txt is the extension of course
So, is there is way in dos or vb scripting to look at the list of files and append the variables to produce the files with the MMDDYYYY_HHMMSS_filename_jobclassid.txt?
The actual filename may or may not change, but the date and time obviously will.
0
Comment
Question by:xzay1967
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39707020
Is this the date/time stamp based on the time the file was last modified, or 'now' the time the script runs?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39707032
Quick batch script to do it (name it FTPRename.cmd, for example).
the ECHO can be removed if it appears to work as desired.
The only difference may be in your date display (if it shows the 3 letter abbreviation of the day), in which case the first line would need to be replaced with
set myDate=%date:~0,2%%date:~3,2%%date:~-4%

@echo off
set myDate=%date:~4,2%%date:~7,2%%date:~-4%
set jobID=bhby123
for /f %%a in ('dir /b *.txt') do ECHO ren "%%a" "%myDate%_%%~na.%jobID%

Open in new window

0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 150 total points
ID: 39707036
And additionally, you can setup the batch script to use a parameter for the job id, by altering it as:

@echo off
set myDate=%date:~4,2%%date:~7,2%%date:~-4%
set jobID=%1
for /f %%a in ('dir /b *.txt') do ECHO ren "%%a" "%myDate%_%%~na.%jobID%

Open in new window


FTPRename bhby123
0
 

Author Comment

by:xzay1967
ID: 39707188
Thanks for the response, I already have the date time variable setup
set hh=%time:~0,2%
set mm=%time:~3,2%
set ss=%time:~6,2%
set newtime=%hh%%mm%%ss%
for /f "tokens=2" %%d in ('date /t') do set dte=%%d
for /f "delims=/ tokens=1-3" %%a in ('echo %dte%') do set ymd=%%a%%b%%c

Open in new window

With that said, I am not catching your logic if the file names are same only on the first part of their name, (see attached for file list). Looking at the attachment, how would I use the script to rename them so MMDDYYYY_filename.jobclassID.txt is appended?
eg. 12092013_wawacard1.wawacnv1.txt. You will notice that the only part of the files that are the same is waw . In my job I have the ftp looking for waw*, and that works cause it pulls all the files. So I would expect something maybe looks at a list and renames as such.
screenshot.png
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39707396
I don't quite understand...how are you assigning a jobclass id?  The above should work, assuming you know what that value is....

Additionally, if you're simply combining the first variables into newtime, you can simply use

set newtime=%time:~0,2%%time:~3,2%%time:~6,2%
0
 

Author Comment

by:xzay1967
ID: 39707541
Sorry for the confusion, the jobclassid I do know. That is the wawacnv1 that gets appended the file name. You are correct, I did set a new variable called NewTime which combined all the time variables for hh, mm, ss.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Accepted Solution

by:
Bill Prew earned 350 total points
ID: 39707563
I think this is how I would approach it, let me know if this works for you, or if you have questions.

@echo off
setlocal

REM Define folder where files to rename reside, and job class id
set BaseDir=c:\temp
set Class=bhby123

REM Build a date/time stamp in YYYYMMDD_hhmmss format
set Stamp=%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,8%
set Stamp=%Stamp::=%
set Stamp=%Stamp: =0%

REM Process each file and rename it
for %%A in ("%BaseDir%\xyz*.txt") do ren "%%A" "%Stamp%_%%~nA_%Class%%%~xA"

Open in new window

~bp
0
 

Author Comment

by:xzay1967
ID: 39708417
This script worked like a charm, thanks for the quick responses.
0
 

Author Comment

by:xzay1967
ID: 39708422
Oh my, my apologies, I did not pay attention to who the solution came from, I thought it was from sirbounty. I did not realize that Bill jumped in. Please accept my apology sirbounty for not awarding points to you for your assistance. Admin, is there any way I can amend this to award some points to sirbounty? While Bill provided the solution I used, I just want to be fair.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39708496
I unaccepted your solution, feel free to re close the question with a different distribution.

~bp
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39708751
Thanks guys - glad I could contribute and that you found your solution.
0
 

Author Comment

by:xzay1967
ID: 39708954
Thanks for your flexibility Bill, as always you have provided superb assistance. By the way, do you do any tutoring? I would love to become better at this scripting stuff.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 39709184
Welcome, happy to help and sirbounty certainly added contribution as well.

I don't really formally tutor, but happy to answer questions here or share other useful info with you. I suspect geographic locations would prevent true face to face tutoring, at least that usually the case on sites like this, they attract folks from literally around the world.

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now