Solved

Bat file for copy, move and rename proccess

Posted on 2016-08-24
7
29 Views
Last Modified: 2016-09-01
I'm looking for (2) processes that would make use of a .bat file or Windows Shell processing script which I plan to schedule daily :

Script 1, should fetch (copy) a specific file over a shared network which will be in a specific datetime format :-
1- Copy from \\10.10.5.x\Folder\filename_08242016.txt,
2- paste locally c:\Folder\Daily and
3- rename to file.txt

There will be a 3rd party application that will pick this file up for processing at a certain time.  Script 2 below will follow to rename and move if at a certain time hereafter.

Script 2, should
1-rename this file.txt to file_CurrentDateTime.txt
2-move it to c:\Folder
0
Comment
Question by:John-S Pretorius
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Ben Personick
ID: 41769516
Easy Peasy if my assumption is correct that you want to take the newest file from the dource folder?

if so (and I can make you a nicer version of each when I have 10 minutes of not being mobile)

:: Script: Copy_Daily_Local.cmd
:: Version 0.1

Set "SrcFolder=\\10.x.y.z\Share\Folder\folder" &REM Always leave no trailing "\"
Set "DstFolder=C:\Folder\folder"
Set "DstFile=whatever"

for /f "tokens=*" %%A in (' Dir /b /a:-D %SrcFolder%') DO (
  Copy "%SrcFolder%\%%~A" "%DstFolder%\%DstFile%" /F /Y
  GOTO :EOF
)

GOTO :EOD
:: Script End

Second script is also pretty simple but I don't want to tap it all out on my phone at the moment.

what format did you want the date in for that script?  I suggest the IOS date format (YYYY-MM-DD) but I can output Something else if you prefer.

Thanks.

Q
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 41769520
also what time format?  (I generally use HH.mm.ss or HH.mm.ss.ms)

furthermore, is there any reason this needs to be two separate scripts?

  Do you need to delete the current file in the folder to make sure it doesn't re-process it?

  If the only reason is because you copy a new file in, we can just make one script which copies the file to both locations and simply deletes the daily file each day
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 41769521
Sorry for the Double post, I'm on the train
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:John-S Pretorius
ID: 41769594
Great feedback thank you. You can use whatever time format you prefer. the reason there is 2 scripts is as I'm working in 2 different PC's I have a 3rd party application processing a text file and then pushing/appends the results into SQL. I also want to keep a local copy when the proccess is done. I could also be over thinking the proccess and may change it as I get closer.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41770414
Okay, here's my shot at it.  I decided to just use one BAT file, with a command line parm that determines what mode it is working in (step 1, step 2).  I think this makes more sense since you can see all the logic in one place and when you have to maintain the scripts in the future to change folder names, or add logic it will be easier and less error prone editing a single file.

Save as a BAT file and run passing it either GET or PUT on the command line.  GET is the first step, fetching the most recent file from the server.  PUT is the second step, renaming and archiving the file.

Adjust the SET lines near the to to your specific folder locations, these were mine for testing.

@echo off
setlocal EnableDelayedExpansion

set ServerDir=B:\ee\EE28965471\server\folder
set ArchiveDir=B:\ee\EE28965471\folder
set DailyDir=B:\ee\EE28965471\folder\daily
set Filename=file.txt

rem Get the processing mode from the command line ("GET" or "PUT")
if "%~1" EQU "" (
  echo USAGE : %~nx0 [GET^|PUT]
  exit /b
) else (
  set Mode=%~1
)

rem Get file if GET mode
if /i "%Mode%" EQU "GET" (

  rem Find newest file to process
  set NewFile=
  for /f "tokens=*" %%A in ('dir /b /a:-d "%ServerDir%"') do (
    if not defined NewFile (
      set NewFile=%%~A
    )
  )

  rem Copy to processing folder
  copy "%ServerDir%\!NewFile!" "%DailyDir%\%Filename%" >NUL

  rem Exit script
  exit /b

)

rem Archive file if PUT mode
if /i "%Mode%" EQU "PUT" (

  rem Get current date time into variable for file naming
  call :GetStamp

  rem Rename with todays date / time
  ren "%DailyDir%\%Filename%" "file_!Stamp!.txt"

  rem Move to archive location
  move "%DailyDir%\file_!Stamp!.txt" "%ArchiveDir%\" >NUL

  rem Exit script
  exit /b

)

rem Mode was not GET or PUT, show usage and end
echo *ERROR* Invalid mode "%Mode%"
echo USAGE : %~nx0 [GET^|PUT]
exit /b


:GetStamp
  rem Get current date in YYYYMMDD_hhmmss format
  set Stamp=
  for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined Stamp (
      set Stamp=%%A
      set Stamp=!Stamp:~0,8!_!Stamp:~8,6!
      exit /b
    )
  )
  exit /b

Open in new window

~bp
1
 

Author Comment

by:John-S Pretorius
ID: 41773194
I get it working and realize I may need some additional work. When my application creating this original .log file completes it dumps it in the server dir folder and gives it a specific datetime name for example (see below) :-
filename_08272016_000202.log how can I have the .bat file / script detect the most recent file for that day ?
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41773961
Yes, just make this small change:
for /f "tokens=*" %%A in ('dir /b /a:-d /o-d "%ServerDir%"') do (

Open in new window

~bp
1

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

705 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

15 Experts available now in Live!

Get 1:1 Help Now