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

x
?
Solved

New to BAT files - Minor change to this BAT file

Posted on 2011-09-27
20
Medium Priority
?
394 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
[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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
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 668 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 664 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 58

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

Assisted Solution

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

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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