?
Solved

Copying and renaming file

Posted on 2012-08-20
7
Medium Priority
?
503 Views
Last Modified: 2012-08-27
We want to create a batch file which can do following

1) It looks for lastest modified file (that file is text file) in directory. That filename is in format of   "Retail Operations16Aug2012083512" obviously with latest date everyday
2) Copy that file it to a sub folder.
3) Then after renaming the original file to POSJNL.txt

Can above be doable? Even if you can send the rough batch file and will modify accordingly our environment.
0
Comment
Question by:ArcIT
7 Comments
 
LVL 22

Expert Comment

by:David Atkin
ID: 38315123
This may start you off:
http://stackoverflow.com/questions/97371/how-do-i-write-a-windows-batch-script-to-copy-the-newest-file-from-a-directory

Extract:
"Windows shell, one liner:

FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT"

It does not rename the old file though.
0
 
LVL 12

Expert Comment

by:Sandeep
ID: 38315130
Assuming above is a Script executed via cScript. You can call this call from a bat file and after calling this file, on next line you can use ren command to rename the file which is a simple dos command.

Hope it helps.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38315159
Well to get the latest file in a dir that matches the easiest way is to ignore the actual filename and gets the first entry in a dir listing:

@Echo off
set sourcedir=c:\mysource\dir\name
set destfile=C:\mydest\dir\name\posjnl.txt
pushd "%sourcedir%
for /f "tokens=*" %%f in ('dir /b /a-d /o-d *.txt') do copy /y "%%~f" "%destfile%" & goto :end
:End
echo Completed
popd

Open in new window


The for command looks at a dir listing - /b = bare list, /a-d = only files, /o-d = date order newest first.
It takes the first entry runs the copy command into your destfile name then ends the loop.

hth

Steve
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 38315160
Hmm delayed in hitting submit there!

Steve
0
 

Author Comment

by:ArcIT
ID: 38315174
@ -Scorpeo-

FOR /F %%I IN ('DIR *.* /B /O:-D') DO COPY %%I <<NewDir>> & EXIT"

whats %%I ? and do we run above code simply in windows command prompt?

e.g file name is Retail Operations19Aug2012083511.txt and location is "\\SERVERNAME\Infogenesis\Export\Great Plains"

and want to copy to folder called "\\SERVERNAME\Infogenesis\Export\AUG 2012"
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 38315196
See my example.  The For command in this way takes the output of the DIR command (in his case it will also contain sub directory names and all files not just text files) and each name gets pushed into the variable %%I one at a time which can then be run by the bit after the "DO".

This one won't work as it stands because:

1. It will not work with filenames with spaces, commas etc. in as the FOR command will split the filename up at it's default delimiters
2. It may pick a directory name not a filename for you

Please save my example as "something.cmd" in notepad - When you save change it from "Text files" to "All files" in the file type to make sure it doesn't helpfully add ".txt" onto the end of the name.  Then you can run from explorer, command prompt (cmd.exe), scheduled,  or shortcut on desktop etc.

Steve
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1500 total points
ID: 38321555
Based on what you've described I think this should do the whole job.  The only thing I'm not sure of (since you didn't specify) is if the destination folder with the month and year in it, needs to automatically be chosen based on the file name?  Save this as a BAT file.

@echo off
set "BaseDir=\\SERVERNAME\Infogenesis\Export\Great Plains"
set "DestDir=\\SERVERNAME\Infogenesis\Export\AUG 2012"
set "NewName=posjnl.txt"

for /F "tokens=*" %%F in ('dir /B /A-D /O-D "%BaseDir%"') do (
  copy /Y "%%~F" "%DestDir%"
  ren /Y "%%~F" "%NewName%"
  goto :End
)

:End

Open in new window

~bp
0

Featured Post

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!

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

809 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