Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename multiple files with dos or vb script

Posted on 2013-12-09
13
Medium Priority
?
504 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
[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
  • 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 600 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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
 
LVL 58

Accepted Solution

by:
Bill Prew earned 1400 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 58

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 58

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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