• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Rename multiple files with dos or vb script

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
xzay1967
Asked:
xzay1967
  • 5
  • 4
  • 3
  • +1
2 Solutions
 
Steve KnightIT ConsultancyCommented:
Is this the date/time stamp based on the time the file was last modified, or 'now' the time the script runs?
0
 
sirbountyCommented:
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
 
sirbountyCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
xzay1967Author Commented:
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
 
sirbountyCommented:
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
 
xzay1967Author Commented:
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
 
Bill PrewCommented:
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
 
xzay1967Author Commented:
This script worked like a charm, thanks for the quick responses.
0
 
xzay1967Author Commented:
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
 
Bill PrewCommented:
I unaccepted your solution, feel free to re close the question with a different distribution.

~bp
0
 
sirbountyCommented:
Thanks guys - glad I could contribute and that you found your solution.
0
 
xzay1967Author Commented:
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
 
Bill PrewCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now