?
Solved

New to BAT files - Minor change to this BAT file

Posted on 2011-09-27
20
Medium Priority
?
393 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
Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

 
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 56

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 56

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 56

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

801 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