Solved

New to BAT files - Minor change to this BAT file

Posted on 2011-09-27
20
386 Views
Last Modified: 2013-11-09
The BAT file in the attached related question works but I need to tweak it and I dont know how.

I copy a file called "PF.txt". It renames it to PF20112709.txt
I copy another file with the same name "PF.txt" but the renamed file gets renamed to "PF2011270920112709.txt"

1. How can I add "time" at the end of the file?
2. I need to rename the one that doesnt have date/time attached to it?

So, I can have the same filename and i need to rename the one that hasnt been changed by the BAT file...

I've posted the file again here
echo off
cls

set ManifestDir=c:\manifest\test

for /r %ManifestDir% %%i in (*.*) do (
call :doitnow %%i %%~ni %%~dpi %%~xi
)
goto eof

:doitnow
set ManifestFile=%1
set ManifestFileNameOnly=%2
set ManifestFilePath=%3
set ManifestFileExt=%4
RENAME %ManifestFile% %ManifestFileNameOnly%%date:~-4,4%%date:~-7,2%%date:~-10,2%%ManifestFileExt%
:eof

Open in new window

0
Comment
Question by:Camillia
  • 8
  • 5
  • 3
  • +2
20 Comments
 
LVL 9

Expert Comment

by:jsdray
ID: 36710881
Hello again...
Use this to add time to the end...

%time:~,2%%time:~3,2%%time:~6,2%

Change the rename line to

RENAME %ManifestFile% %ManifestFileNameOnly%%date:~-4,4%%date:~-7,2%%date:~-10,2%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
0
 
LVL 9

Expert Comment

by:jsdray
ID: 36710903
if I understand correctly, you're wanting to redo the ones already with the date and just add the time too...correct?

If so, change the rename line to this and run it...


RENAME %ManifestFile% %ManifestFileNameOnly%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
0
 
LVL 7

Author Comment

by:Camillia
ID: 36711239
let me try, thanks. Will post back.
0
 
LVL 7

Author Comment

by:Camillia
ID: 36712301
They both work but the thing is ...it keeps adding to the same file.
For example...i have "PF.txt". I run it and it's PF134112.txt.
This is correct. I copy another file with the same name "PF.txt". Run it again and now I see:

PF134112134358.txt
and PF134358.txt

It keeps adding the time stamp to the first file as well. Files can be the same name when copied to the folder but timestamp should be attached to the one that doesnt have a timestamp aleady. The way it works now...i'll have HUGE file names :)
0
 
LVL 9

Accepted Solution

by:
jsdray earned 167 total points
ID: 36717267
echo on

cls

set ManifestDir=c:\manifest\test

for /r %ManifestDir% %%i in (*.*) do (
call :doitnow %%i %%~ni %%~dpi %%~xi
)
goto eof

:doitnow
set ManifestFile=%1
set ManifestFileNameOnly=%2
set ManifestFilePath=%3
set ManifestFileExt=%4

echo.%ManifestFileNameOnly:~-5%|findstr [0-9][0-9][0-9][0-9][0-9]
if %errorlevel%==1 (
RENAME %ManifestFile% %ManifestFileNameOnly%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
)
:eof
0
 
LVL 9

Expert Comment

by:jsdray
ID: 36717302
i only check for 5 digits since i'm not sure how your system handles earlier than 10am times... 09 or (space)9  so 5 didgits should be adequate to determin if you already ran this or not on that file...
0
 
LVL 37

Assisted Solution

by:Neil Russell
Neil Russell earned 166 total points
ID: 36717311
Your easiest solution would be have two directories. One for inbound files and one for completed.
Process the files in your Inbound directory with your script but add a MOVE command after the RENAME to move it to the new COMPLETED folder.
simples :D


set TimeStamp=%time:~,2%%time:~3,2%%time:~6,2%
RENAME %ManifestFile% %ManifestFileNameOnly%%TimeStamp%%ManifestFileExt%
MOVE %ManifestFileNameOnly%%TimeStamp%%ManifestFileExt%  c:\COMPLETED
0
 
LVL 7

Author Comment

by:Camillia
ID: 36717411
Yes, I will try the move.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36719885
+1 to @Neilsr comment, the best way to avoid issues is to move them.

But it looks like they could be nested in subfolders and that could cause a bit of a problem.

As an alternative consider changing the extension to something different like BAK, and then on the FOR line only look for TXT files, like this:

echo off
set ManifestDir=c:\manifest\test
for /R %ManifestDir% %%I in (*.txt) do (
  ren "%%~i" "%%~ni%DATE:~-4,4%%DATE:~-7,2%%DATE:~-10,2%%TIME:~,2%%TIME:~3,2%%TIME:~6,2%.bak"
)

Open in new window

~bp
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:Camillia
ID: 36719908
I will test this as well..
0
 
LVL 9

Expert Comment

by:jsdray
ID: 36812091
how many different file types do you plan to run this against? nested folders?
I understand what neilsr is doing, but as posted won't work unless script is in same dir.  MOVE will also allow rename.
 I like Billprew's idea better, but you'll end up running multiple times for desired results if you plan to run against multiple extensions.  Other wise, my last script posted will do exactly as you've asked with any extension and sub directories.  
Been fun scripting this for you...enjoy and hope you get what you need.
0
 
LVL 7

Author Comment

by:Camillia
ID: 36814191
let me get the file names. It's 4 files. I will post back when i get to work.
0
 
LVL 7

Author Comment

by:Camillia
ID: 36814467
There are 6 files: PF.txt , PFDelta.txt, BN.txt and BNDelta.txt, WR.txt and WRDelta.txt

So, use bill's method?
0
 
LVL 52

Assisted Solution

by:Bill Prew
Bill Prew earned 167 total points
ID: 36814509
Based on that, give this a try. It will rename only the files with those names adding the date time.

echo off
set ManifestDir=c:\manifest\test
for /R %ManifestDir% %%I in (pf.txt* pfdelta.txt* bn.txt* bndelta.txt* wr.txt* wrdelta.txt*) do (
  ren "%%~I" "%%~nI%DATE:~-4,4%%DATE:~-7,2%%DATE:~-10,2%%TIME:~,2%%TIME:~3,2%%TIME:~6,2%%%~xI"
)

Open in new window

~bp
0
 
LVL 7

Author Comment

by:Camillia
ID: 36814838
yes, that works, thanks so much for all your help.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36815252
Welcome.

~bp
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36815473
According to your accepted solution, you may end up having problems if your FOR loop finds files with spaces, because of the CALL command. Here is what I suggest.

Cheers,
Rene
echo on

cls

set ManifestDir=c:\manifest\test

for /r %ManifestDir% %%i in (*.*) do (
call :doitnow "%%i" "%%~ni" "%%~dpi" "%%~xi"
)
goto eof

:doitnow
set ManifestFile=%~1
set ManifestFileNameOnly=%~2
set ManifestFilePath=%~3
set ManifestFileExt=%~4

echo.%ManifestFileNameOnly:~-5%|findstr [0-9][0-9][0-9][0-9][0-9]
if %errorlevel%==1 (
RENAME %ManifestFile% %ManifestFileNameOnly%%time:~,2%%time:~3,2%%time:~6,2%%ManifestFileExt%
)
:eof

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 36815506
I will try this one as well. I have a related question and i will open one later. thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 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

10 Experts available now in Live!

Get 1:1 Help Now